Hi all,
I am having a dimension mismatch error when trying to perform out-of-sample prediction.
My code is the following:
import pymc as pm
import numpy as np
def feature_test(x,N,M):
idx = np.arange(M)
matrix = np.tile(idx, (N,1))
return pm.math.sin(x @ matrix)
class CPD:
def __init__(self,X,y,sqrt_prior_covariance,R,sigma_noise):
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*self.R*self.D
self.CPD_model = pm.Model()
with self.CPD_model:
X = pm.MutableData ('X', X)
# Define prior
factors = np.empty(self.D, dtype=object)
for d in range(self.D):
factors[d] = (sqrt_prior_covariance / np.power(self.R,1/(2*self.D))) @ pm.Normal(f'factor_{d}',mu=0, sigma = 1, shape = (self.M,self.R))
# Model output
output = np.ones((self.N,self.R))
for d in range(self.D):
output = output*pm.math.matmul(feature_test(X[:,d],self.N,self.M),factors[d])
output = pm.Deterministic('CPD_output',pm.math.sum(output,axis=1))
# Likelihood (observed data)
likelihood = pm.Normal('y', mu=output, sigma=self.sigma_noise, observed=self.y)
def sample_prior(self,n_samples):
with self.CPD_model:
samples = pm.sample_prior_predictive(samples=n_samples,var_names=['CPD_output']).prior.CPD_output
return samples
def fit(self,n_samples):
with self.CPD_model:
self.trace = pm.sample(n_samples, tune=1000)
def predict(self,X_test):
with self.CPD_model:
self.N = np.shape(X_test)[0]
pm.set_data({"X": X_test})
pm.sample_posterior_predictive(self.trace,predictions=True)
I get the error when I try to run:
N = 2
D = 3
X = np.random.normal(0,1, size=(N,D))
y = 10+np.random.normal(0,1, size=(N,))
M = 4
prior_cov = np.identity(M)
X_test = np.random.normal(0,1, size=(3,D))
cpd_model = CPD(X,y,prior_cov,4,0.1)
if __name__ == "__main__":
cpd_model.fit(10)
print("FITTED")
cpd_model.predict(X_test)
How can I solve this? Do I have to get rid of the self.N variable? Thank you in advance.