# Problem with shared variable for means

**URL:** <https://discourse.pymc.io/t/problem-with-shared-variable-for-means/3435>\
**Category:** Questions\
**Tags:** theano\
**Created:** [June 14, 2019, 5:55pm UTC](https://discourse.pymc.io/t/problem-with-shared-variable-for-means/3435 "2019-06-14T17:55:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpgoldman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/rpgoldman/32/1800_2.png) [@rpgoldman](https://discourse.pymc.io/u/rpgoldman)\
**Post date:** [June 14, 2019, 5:55pm UTC](https://discourse.pymc.io/t/problem-with-shared-variable-for-means/3435/1 "2019-06-14T17:55:53Z")

</div>

These two lines of my model construction code:

```auto
            # 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:

```auto
  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:

```auto
ValueError: setting an array element with a sequence.

```

The traceback:

```auto
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.

---

<div class="post-metadata">

**Author:** ![rpgoldman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/rpgoldman/32/1800_2.png) [@rpgoldman](https://discourse.pymc.io/u/rpgoldman)\
**Post date:** [June 14, 2019, 6:04pm UTC](https://discourse.pymc.io/t/problem-with-shared-variable-for-means/3435/2 "2019-06-14T18:04:24Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![rpgoldman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/rpgoldman/32/1800_2.png) [@rpgoldman](https://discourse.pymc.io/u/rpgoldman)\
**Post date:** [June 14, 2019, 6:28pm UTC](https://discourse.pymc.io/t/problem-with-shared-variable-for-means/3435/3 "2019-06-14T18:28:54Z")

</div>

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

```auto
            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.
