Model doesn't contain any free variables error

This my first work with Pymc3 and I’m trying to run the follwoing model in Pymc3 to estimate the value of b (the upper bound of a truncated normal) :

with pm.Model() as sleep_model:
    low= pm.Normal('low', mu= b_lower, sigma= 0.001, shape= (0,1) )
    high= pm.Normal('high', mu= b_upper, sigma= 0.001, shape= (0,1))
    b= pm.Triangular('b', lower= low, upper= high, c= b_mode, shape= (0,1))
    #p= pm.TruncatedNormal('p', mu= mu1, sigma= sg1, upper= b)
    observed= pm.TruncatedNormal('obs', mu= mu1, sigma= sg1, upper= b, observed= np.array(seq_x))
    step= pm.Metropolis()
    sleep_trace= pm.sample(N_samples, step= step, njobs= 2)

However, I get an error that The model does not contain any free variables

appreciate your help about how to solve this problem

Hi, just remove the shape argument from your random variables:

low= pm.Normal('low', mu= b_lower, sigma= 0.001)

Is there a reason for you to be using Metropolis?

Hi luisroque,
thanks for your reply.
I actually added the shape arguments because I was getting this 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 0, got 1 with shape (1,).

Concerning using Metropolis, I’m actually using it for a particular reason, do you think it is what is causing the problem?

No, I don’t think so. The model runs for me, removing the shapes and also the njobs. What version of PyMC3 are you running?

Can you also share what seq_x looks like?

I’m running version 3.8

array([1.670e+01, 1.440e+01, 7.600e+00, 1.080e+01, 1.000e-02, 9.000e+00,
       1.310e+01, 5.500e+00, 6.600e+00, 1.000e-03, 1.210e+01, 1.060e+01,
       1.050e+01, 1.000e-02, 3.000e+00, 6.000e+00, 5.400e+00, 1.360e+01,
       1.290e+01, 9.700e+00, 1.110e+01, 1.310e+01, 1.000e-03, 1.760e+01,
       1.000e-02, 3.600e+00, 1.555e+01, 1.190e+01, 1.800e+01, 3.600e+00,
       3.700e+00, 8.950e+00, 7.300e+00, 1.000e-03, 1.930e+01, 8.000e+00,
       1.190e+01, 1.900e+01, 8.300e+00, 1.060e+01])

It runs for me with version 3.9.3

I’ve updated pymc3 to version 3.10.0 but still getting the same error!

Then, the only thing that it is left are the values that you are supplying as arguments to the RV, such as b_lower, b_upper, etc. You might be supplying a data type that messes with shape, such as a list. You can’t do that. It should be something like:

b_lower = 0.
low= pm.Normal('low', mu= b_lower, sigma= 0.001)

+1 to remove the shape, as shape=(0, 1) create a zero size random variable which is another way to say no variable.

also, when you see error like Wrong number of dimensions: expected 0, got 1 with shape (1,)., it actually means some of the input is (1, ) shape instead of a scalar - I cannot quite reproduce your error but usually you can wrap the random variables in question with tt.squeeze.

1 Like