首页 > 解决方案 > TensorVariable to Array

问题描述

I'm trying to evaluate a theano TensorValue expression:

import pymc3
import numpy as np
with pymc3.Model():
    growth = pymc3.Normal('growth_%s' % 'some_name', 0, 10)

x = np.arange(4)
(x * growth).eval()

but get the error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/danna/.virtualenvs/lib/python2.7/site-packages/theano/gof/graph.py", line 522, in eval
    self._fn_cache[inputs] = theano.function(inputs, self)
  File "/home/danna/.virtualenvs/lib/python2.7/site-packages/theano/compile/function.py", line 317, in function
    output_keys=output_keys)
  File "/home/danna/.virtualenvs/lib/python2.7/site-packages/theano/compile/pfunc.py", line 486, in pfunc
    output_keys=output_keys)
  File "/home/danna/.virtualenvs/lib/python2.7/site-packages/theano/compile/function_module.py", line 1839, in orig_function
    name=name)
  File "/home/danna/.virtualenvs/lib/python2.7/site-packages/theano/compile/function_module.py", line 1487, in __init__
    accept_inplace)
  File "/home/danna/.virtualenvs/lib/python2.7/site-packages/theano/compile/function_module.py", line 181, in std_fgraph
    update_mapping=update_mapping)
  File "/home/danna/.virtualenvs/lib/python2.7/site-packages/theano/gof/fg.py", line 175, in __init__
    self.__import_r__(output, reason="init")
  File "/home/danna/.virtualenvs/lib/python2.7/site-packages/theano/gof/fg.py", line 346, in __import_r__
    self.__import__(variable.owner, reason=reason)
  File "/home/danna/.virtualenvs/lib/python2.7/site-packages/theano/gof/fg.py", line 391, in __import__
    raise MissingInputError(error_msg, variable=r)
theano.gof.fg.MissingInputError: Input 0 of the graph (indices start from 0), used to compute InplaceDimShuffle{x}(growth_some_name), was not provided and not given a value. Use the Theano flag exception_verbosity='high', for more information on this error.

I tried Can someone please help me see what the theano variables actually output? Thank you! I'm using Python 2.7 and theano 1.0.3

标签: python-2.7theanopymc3

解决方案


虽然 PyMC3 分布是TensorVariable对象,但它们在技术上没有任何值可以在采样之外进行评估。如果你想要值,你必须至少在模型上运行采样:

with pymc3.Model():
    growth = pymc3.Normal('growth', 0, 10)

    trace = pymc3.sample(10)

x = np.arange(4)
x[:, np.newaxis]*trace['growth'] 

如果要在采样期间查看节点值,则需要使用theano.tensor.printing.Print对象。有关详细信息,请参阅PyMC3 调试提示


推荐阅读