Theano中变量形状之间的区别是什么

时间:2014-05-13 14:48:14

标签: python numpy theano

你能告诉我Theano中形状0,(?,),(1,?),(?,?)的区别是什么? 为什么我定义的数组为

arr = np.array([1,2,3])

是(3,)的数组?我怎么能定义一个(3,1)的数组?

此外,我编写如下代码:

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?')

非常感谢!

1 个答案:

答案 0 :(得分:2)

您正试图将a.z传递给a.ac(),而a.z实际上是a.ac(x)结果

相反,你可能想要这样做:

a.ac(a.inputs)
# array([  8.61379147, -13.0183053 ,  -4.41056323])

a.za.xa.W都可以评估之前,符号变量a.b的值不确定。 theano.function的语法如下:

find_x = theano.function([<inputs needed to compute x>], <output x>)

当你真的想要调用find_x()时,你只需要在方括号中给它一些东西,而theano.function的第二个参数将是find_x()的返回值。