Skip to content

shape attribute not updated when using it with a shared variable #2326

@denadai2

Description

@denadai2

I have a problem with the shape parameter, that doesn't get updated when I update a shared variable. This is a toy example: it doesn't make so sense by itself but it is to reproduce the error.

from pymc3.distributions import distribution
from theano import shared


class CAR2(distribution.Continuous):
	def __init__(self, mu, *args, **kwargs):
		super(CAR2, self).__init__(*args, **kwargs)

		self.mean = mu

	def logp(self, x):
		return tt.sum(continuous.Normal.dist(mu=0, tau=0.001).logp(x))


df = pd.read_csv('data_bug.csv')[['land_use_mix3', 'y']]

train_index = list(range(df.values.shape[0]-1))
test_index = [df.values.shape[0]-1]

X_shared = shared(df[['land_use_mix3']].values[train_index, :][:, 0])
y_shared = shared(df['y'].values[train_index])


with pm.Model() as pooled_model:
    coeffs = pm.Normal('asd', mu=0, tau=0.001)
    mu_phi = CAR2('mu_phi', shape=X_shared.get_value().shape[0], mu=tt.zeros(X_shared.get_value().shape[0]))
    
    mu = tt.exp(X_shared.ravel() * coeffs + mu_phi)
    
    obs = pm.Poisson('obs', mu=mu, observed=y_shared)
    
    pooled_trace = pm.sample(100, njobs=1, tune=100)
    
X_shared.set_value(df[['land_use_mix3']].values[test_index, :][:, 0])
y_shared.set_value(df['y'].values[test_index])
    
ppc = pm.sample_ppc(pooled_trace, samples=1000, model=pooled_model)

the problem is with shape parameter of CAR2, which stays = to 76 but it should be shape=1 after the update.

ValueError: Input dimension mis-match. (input[0].shape[0] = 1, input[2].shape[0] = 76)
Apply node that caused the error: Elemwise{Composite{exp(((i0 * i1) + i2))}}(<TensorType(float64, vector)>, InplaceDimShuffle{x}.0, mu_phi)
Toposort index: 1
Inputs types: [TensorType(float64, vector), TensorType(float64, (True,)), TensorType(float64, vector)]
Inputs shapes: [(1,), (1,), (76,)]
Inputs strides: [(8,), (8,), (8,)]
Inputs values: [array([-1.30698299]), array([-1.67981393]), 'not shown']
Outputs clients: [['output']]

file:

data_bug.csv.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions