我目前遇到了这个问题:在使用Theano编写的循环神经网络中,我无法评估我的梯度符号变量。以下是代码:
W_x = theano.shared(init_W_x, name='W_x') W_h = theano.shared(init_W_h, name='W_h') W_y = theano.shared(init_W_y, name='W_y') [self.y, self.h], _ = theano.scan(self.step, sequences=self.x, outputs_info=[None, self.h0]) error = ((self.y - self.t) ** 2).sum() gW_x, gW_y, gW_h = T.grad(self.error, [W_x, W_h, W_y]) [...] def step(self, x_t, h_tm1): h_t = T.nnet.sigmoid(T.dot(self.W_x, x_t) + T.dot(h_tm1, self.W_h)) y_t = T.dot(self.W_y, h_t) return y_t, h_t
我只保留了我认为合适的部分。
我想计算例如’gW_x’,但当我尝试将其嵌入到theano函数中时,它不起作用,因为它的依赖项(W_x, W_h, W_y)是共享变量。
非常感谢
回答:
我认为在这种情况下,您需要将共享变量传递给self.step
函数中的non_sequences
参数,在theano.scan
中使用。
因此,您需要更改self.step
的签名,以接受三个额外的参数,对应于共享变量,然后在theano.scan
中添加参数non_sequences=[W_x, W_h, W_y]
。
另外,我怀疑您在倒数第二行可能有拼写错误 – 应该是error = ((self.y - t) ** 2).sum()
吗?