Problem with shared variable for means

These two lines of my model construction code:

            # prior_means[I] = prior mean(output(I))
            prior_means = shared(np.where(np.array(self.outputs()),
                                          prior_dict['prior means'][1],
                                          prior_dict['prior means'][0]))
            means = TruncatedNormal("%s Output"%gate,
                                    mu=prior_means, sd=3.0, lower=0.0,
                                    upper=MAX_GFP, shape=num_inputs)

…cause PyMC3 to error out with the following error:

  File "/Users/rpg/src/pymc3/pymc3/theanof.py", line 65, in floatX
    return X.astype(theano.config.floatX)
AttributeError: 'SharedVariable' object has no attribute 'astype'

This is also printed:

ValueError: setting an array element with a sequence.

The traceback:

Traceback (most recent call last):
  File "/Users/rpg/src/pymc3/pymc3/theanof.py", line 65, in floatX
    return X.astype(theano.config.floatX)
AttributeError: 'SharedVariable' object has no attribute 'astype'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/rpg/projects/xplan/xplan-experiment-analysis/src/xplan_experiment_analysis/continuous_models.py", line 341, in __init__
    parent=self)
  File "/Users/rpg/projects/xplan/xplan-experiment-analysis/src/xplan_experiment_analysis/continuous_models.py", line 156, in __init__
    upper=MAX_GFP, shape=num_inputs)
  File "/Users/rpg/projects/xplan/xplan-experiment-analysis/src/xplan_experiment_analysis/continuous_models.py", line 129, in TruncatedNormal
    return pm.TruncatedNormal(self.prefixed_name(name), *args, **kwargs)
  File "/Users/rpg/src/pymc3/pymc3/distributions/distribution.py", line 46, in __new__
    dist = cls.dist(*args, **kwargs)
  File "/Users/rpg/src/pymc3/pymc3/distributions/distribution.py", line 57, in dist
    dist.__init__(*args, **kwargs)
  File "/Users/rpg/src/pymc3/pymc3/distributions/continuous.py", line 633, in __init__
    self.mu = tt.as_tensor_variable(floatX(mu))
  File "/Users/rpg/src/pymc3/pymc3/theanof.py", line 68, in floatX
    return np.asarray(X, dtype=theano.config.floatX)
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numpy/core/numeric.py", line 538, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.

Thanks for any suggestions.

P.S. Wrapping this expression in a pm.Deterministic gives me a different error:

AttributeError: 'SharedVariable' object has no attribute 'copy'

…from the Deterministic function in model.py.

What I am trying to create here is a vector of four distributions that will be used as the prior for another distribution. The priors correspond to outputs for logic gates, so for each of the four possible inputs, I look up whether its output is 1 or 0, and based on that, grab either the random variable that’s the prior for positive output, or the random variable for negative output. That should give me a vector of four random variables, that I will index into later.

1 Like

Cargo-culting seems to give me the right answer to this question. The magical code version is:

            output_vals = np.where(np.array(self.outputs()), 1, 0)
            prior_means = tt.as_tensor_variable(prior_dict['prior means'][output_vals])

TBQH, I don’t understand at all the different variations of theano tensorness here, nor why one works and the other two don’t.