擅长:python、mysql、java
<p>因为<code>testxx</code>使用<code>sum()</code>from <code>theano.tensor</code>,而不是<code>numpy</code>,所以它可能需要<code>TensorVariable</code>作为输入,而不是numpy数组。</p>
<p>=>;用<code>a = T.matrix(dtype=theano.config.floatX)</code>替换<code>a = np.array(...)</code>。</p>
<p>在最后一行之前,<code>outxx</code>将是一个依赖于<code>a</code>的<code>TensorVariable</code>。所以可以通过给出<code>a</code>的值来计算它。</p>
<p>=>;将最后一行<code>outxx = np.asarray(...)</code>替换为以下两行。</p>
<pre><code>f = theano.function([a], outxx)
outxx = f(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype = np.float32))
</code></pre>
<p>以下代码运行时没有错误。</p>
<pre><code>import theano
import theano.tensor as T
import numpy as np
class testxx(object):
def __init__(self, input):
self.input = input
self.output = T.sum(input)
a = T.matrix(dtype=theano.config.floatX)
classfier = testxx(a)
outxx = classfier.output
f = theano.function([a], outxx)
outxx = f(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype = np.float32))
</code></pre>
<p>关于<a href="http://deeplearning.net/software/theano/tutorial/adding.html" rel="nofollow">adding scalars</a>的no文档提供了其他类似的示例。</p>