Shapes of shared variables

I wanted to jointly model some X, Y pairs and use shared variables so I can check how well the model fits on test data later on, leading to this code.

    X = np.random.randn(100, 5)
    Y = np.random.randn(100)

    X = theano.shared(X)
    Y = theano.shared(Y)
    with pm.Model() as model:
        x = pm.Normal('X', mu=0, sd=1, observed=X)
        w = pm.Normal('w', 0, 1, shape=[5, 1])
        muy = pm.math.dot(x, w)
        y = pm.Normal('y', mu=muy, sd=1, observed=Y)

The problem is that when I run this code it gives me the following error

TypeError: For compute_test_value, one input test value does not have the requested type.

The error when converting the test value to that variable type:
Wrong number of dimensions: expected 1, got 2 with shape (100, 5).

but I am not sure why this happens. When I don’t use shared variables, this works perfectly fine, and I’ve tried passing the correct shape to x explicitly and to make it a MvNormal to no avail.

Thanks for your help!

Oh this is a weird one… Even below does not work:

X_ = np.random.randn(100, 5)
X = theano.shared(X_)
with pm.Model() as model:
    x = pm.Normal('X', mu=0, sd=1, observed=X)
    
x.T
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 2910, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-7-ce24a95b4786>", line 1, in <module>
    x.T
  File "/usr/local/lib/python3.5/dist-packages/theano/tensor/var.py", line 248, in <lambda>
    T = property(lambda self: theano.tensor.basic.transpose(self))
  File "/usr/local/lib/python3.5/dist-packages/theano/tensor/basic.py", line 3691, in transpose
    ret = DimShuffle(x.broadcastable, axes)(x)
  File "/usr/local/lib/python3.5/dist-packages/theano/gof/op.py", line 625, in __call__
    storage_map[ins] = [self._get_test_value(ins)]
  File "/usr/local/lib/python3.5/dist-packages/theano/gof/op.py", line 562, in _get_test_value
    ret = v.type.filter(v.tag.test_value)
  File "/usr/local/lib/python3.5/dist-packages/theano/tensor/type.py", line 178, in filter
    data.shape))
TypeError: For compute_test_value, one input test value does not have the requested type.
The error when converting the test value to that variable type:
Wrong number of dimensions: expected 1, got 2 with shape (100, 5).

I have no solution :disappointed_relieved:

This is strange. For some reason theano thinks x has rank 1. The test_value is right though.

>>> x.type
TensorType(float64, vector)
>>> x.tag.test_value.shape
(100, 5)

The type should be TensorType(float64, matrix) (like X)

OK this is now fixed on master - https://github.com/pymc-devs/pymc3/pull/3067