使用Theano计算前向传播

2024-10-02 02:32:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用Theano编码前向传播。我定义了一个类名hiddenLayer,如下所示:

进口张量作为T 从ano import shared 将numpy作为np导入 从no import功能

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 it raises error的函数,代码如下:

^{pr2}$

错误消息是:

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'.

你能告诉我为什么出了什么问题,怎么解决吗?谢谢


Tags: thetonameinimportselfinput定义
1条回答
网友
1楼 · 发布于 2024-10-02 02:32:14

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

您应该将x作为input传递给init。如果没有,创建一个。这是第一层的。另一方面,它应该是前一层的输出。在

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

相关问题 更多 >

    热门问题