【问题标题】:Using Theano to compute forward_propagation使用 Theano 计算 forward_propagation
【发布时间】:2014-05-15 01:29:04
【问题描述】:

我尝试用 Theano 编写前向传播代码。我定义了一个类名 hiddenLayer 如下:

将 theano.tensor 导入为 T 从 theano 导入共享 将 numpy 导入为 np 从theano导入函数

class hiddenLayer():
    """ Hidden Layer class
    """
    def __init__(self, n_in, n_out):
        rng = np.random
        self.W = shared(np.asarray(rng.uniform(low=-np.sqrt(6. / (n_in + n_out)),
                                               high=np.sqrt(6. / (n_in + n_out)),
                                               size=(n_in, n_out)),
                                   dtype=T.config.floatX),
                        name='W')
        self.b = shared(np.zeros(n_out, dtype=T.config.floatX), name='b')
        self.x = T.dvector('x')
        self.a = T.tanh(T.dot(self.x, self.W) + self.b)
        self.W_sum = shared(np.zeros([n_in, n_out]), name='W_sum')
        self.gw = 0
        self.gb = 0

我想设置一个 hiddenLayer 对象列表,当前的 hiddenLayer 是下一个 hiddenLayer 的输入。最后我定义了一个名为 forward buy 的函数,它会引发错误,代码如下:

def init_network(n_in, n_out, sl, x, y):
    l = []
    for i in range(sl):
        l.append(hiddenLayer(n_in, n_out))
    for i in range(sl):
        if i == 0:
            l[i].x = x
        elif i < sl-1:
            l[i].x = l[i-1].a
        else:
            l[i].x = l[i-1].a
            y = l[i].a
    return x, y, l

x = T.dvector('x')
y = T.dvector('y')
x, y, l = init_network(3, 3, 3, x, y)
forward = function(inputs=[x], outputs=y)

错误信息是:

theano.compile.function_module.UnusedInputError: theano.function was asked to create a function computing outputs given certain inputs, but the provided input variable at index 0 is not part of the computational graph needed to compute the outputs: x.
To make this error into a warning, you can pass the parameter on_unused_input='warn' to theano.function. To disable it completely, use on_unused_input='ignore'.

您能告诉我为什么会出现问题以及如何解决吗?谢谢

【问题讨论】:

    标签: python theano


    【解决方案1】:

    问题是您在第二个循环中覆盖了 l.x。你不能那样做。在 init 中使用 self.x 后,基于它的结果将基于 self.x 的当前实例。因此,当您覆盖它时,它不会在新 x 上重新创建其他内容。

    您应该将 x 作为输入传递给 init。如果没有,创建一个。这是第一层。另一个应该是上一层输出。

    def __init__(self, n_in, n_out, x=None):
       if x is not None:
          self.x = x
       else:
          x = T.dvector('x')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-31
      • 2015-10-24
      • 2015-09-16
      • 2016-03-22
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 2014-09-08
      相关资源
      最近更新 更多