Theano中变量形状有什么区别
arr = np.array([1,2,3])
import theano.tensor as T
from theano import shared
import numpy as np
from theano import function
class hiddenLayer():
""" Hidden Layer class
"""
def __init__(self, inputs, n_in, n_out, act_func):
rng = np.random
self.W = shared(np.asarray(rng.uniform(low=-4*np.sqrt(6. / (n_in + n_out)),
high=4*np.sqrt(6. / (n_in + n_out)),
size=(n_in, n_out)),
dtype=T.config.floatX),
name='W')
self.inputs = inputs
self.b = shared(np.zeros(n_out, dtype=T.config.floatX), name='b')
self.x = T.dvector('x')
self.z = T.dot(self.x, self.W) + self.b
self.ac = function([self.x], self.z)
a = hiddenLayer(np.asarray([1, 2, 3], dtype=T.config.floatX), 3, 3, T.tanh)
print a.ac(a.inputs, a.z)
'Expected an array-like object, but found a Variable: '
TypeError: ('Bad input argument to theano function at index 1(0-based)', 'Expected an array-like object, but found a Variable: maybe you are trying to call a function on a (possibly shared) variable instead of a numeric array?')
你能告诉我在Theano中,形状0、(?,)、(1,?)和(?,?)有什么区别吗?
我定义的数组为什么是(3,)的形状?我该如何定义一个(3,1)的数组呢?
另外,我写的代码如下:
为什么会报错:
非常感谢你!
1 个回答
2
你试图把 a.z
传给 a.ac()
,但其实 a.z
是 a.ac(x)
的一个结果!
其实,你可能想这样做:
a.ac(a.inputs)
# array([ 8.61379147, -13.0183053 , -4.41056323])
在 a.x
、a.W
和 a.b
都能被计算出来之前,符号变量 a.z
的值是无法确定的。theano.function
的用法是这样的:
find_x = theano.function([<inputs needed to compute x>], <output x>)
当你真正想调用 find_x()
时,只需要把方括号里的东西传给它,theano.function
的第二个参数就是 find_x()
的返回值。