@ricardoV94 I managed! Did not know about the basic module.
from pytensor.tensor import basic
def feature_test_2(x,M):
idx = np.arange(M)
matrix = basic.tile(idx, (x.shape[0],1))
a = x[:, np.newaxis]*matrix
return pm.math.sin(a)
Thanks a lot for the help!! Can I offer you a coffe donation or so? I have one last problem:
class TT:
def __init__(self,X,y,sqrt_prior_covariance,R,sigma_noise):
self.X = X
self.y = y
self.sqrt_prior_covariance = sqrt_prior_covariance
self.R = R
self.sigma_noise = sigma_noise
self.N = np.shape(X)[0]
self.D = np.shape(X)[1]
self.M = np.shape(sqrt_prior_covariance)[0]
self.P = self.M* np.sum(self.R[:-1] * self.R[1:])
self.TT_model = pm.Model()
with self.TT_model:
A = pm.MutableData('N',X.shape[0])
B = A.astype("int")
X = pm.MutableData ('X', X)
# Define prior
cores = np.empty(self.D, dtype=object)
for d in range(self.D):
cores[d] = (sqrt_prior_covariance / np.sqrt(np.sqrt(R[d]*R[d+1]))) @ pm.Normal(f'factor_{d}',mu=0, sigma = 1, shape = (self.M,self.R[d]*self.R[d+1]))
# Model output
output = []
for n in range(B.eval()):
temp = pm.math.matmul(feature_TT(X[n,0],self.M),cores[0]).reshape((self.R[0],self.R[1]))
for d in range(1,self.D,1):
temp = temp @ pm.math.matmul(feature_TT(X[n,d],self.M),cores[d]).reshape((self.R[d],self.R[d+1]))
output = pm.math.concatenate([output,temp.flatten()])
output = pm.Deterministic('TT_output',output)
# Likelihood (observed data)
likelihood = pm.Normal('likelihood', mu=output, sigma=self.sigma_noise, observed=self.y,shape=output.shape)
def fit(self,n_samples):
with self.TT_model:
self.trace = pm.sample(n_samples, tune=1000)
def predict(self,X_test):
with self.TT_model:
self.N = np.shape(X_test)[0]
pm.set_data({"X": X_test,"N":self.N})
return pm.sample_posterior_predictive(self.trace,predictions=True)
I essentially have the same issue as before. However here it appears just because I have the for loop over the whole dataset X
for n in range(B.eval()):
temp = pm.math.matmul(feature_TT(X[n,0],self.M),cores[0]).reshape((self.R[0],self.R[1]))
for d in range(1,self.D,1):
temp = temp @ pm.math.matmul(feature_TT(X[n,d],self.M),cores[d]).reshape((self.R[d],self.R[d+1]))
output = pm.math.concatenate([output,temp.flatten()])
output = pm.Deterministic('TT_output',output)
This causes me the same problem as before.
The for loop over the dataset is necessary as there is no (easy) way to vectorize the operations.