我是 Theano 的初学者,我正在使用另一个代码的示例,该代码可能在某个时刻起作用(但是,我已经修改了它......但我很确定我的修改与此无关)以及目前出了什么问题)。
无论如何,我正在尝试调试 Theano Scan...并且我认为我观察到的是扫描功能中的基本错误。
U, V, W = self.U, self.V, self.W
x = T.ivector('x')
y = T.ivector('y')
def forward_prop_step(x_t, s_t_prev, U, V, W):
s_t = T.tanh(U.dot(x_t) + V.dot(s_t_prev))
o_t = T.tanh(W.dot(s_t))
return [o_t,s_t]
[o,s], updates = theano.scan(
forward_prop_step,
sequences=x,
outputs_info=[None, dict(initial=T.zeros(self.hidden_dim))],
non_sequences=[U, V, W],
truncate_gradient=self.bptt_truncate,
strict=True)
U
是一个 m x n
矩阵,V
是一个 n x n
矩阵,W
code> 是一个 n x o 矩阵...而 self.bptt_truncate 是一个标量 (4)。但我不认为我的函数的内部结构目前正在失败。
我得到的错误是:
ValueError: When compiling the inner function of scan the following error has been encountered: The initial state (
outputs_info
in scan nomenclature) of variable IncSubtensor{Set;:int64:}.0 (argument number 1) has 2 dimension(s), while the result of the inner function (fn
) has 2 dimension(s) (should be one less than the initial state).
我尝试更改outputs_info的尺寸和forward_prop_step
的返回尺寸,但到目前为止似乎没有任何效果。
我目前正在查看文档...但是,从文档来看,我所做的似乎是正确的(下面是文档中的示例):
def oneStep(u_tm4, u_t, x_tm3, x_tm1, y_tm1, W, W_in_1, W_in_2, W_feedback, W_out):
x_t = T.tanh(theano.dot(x_tm1, W) + \
theano.dot(u_t, W_in_1) + \
theano.dot(u_tm4, W_in_2) + \
theano.dot(y_tm1, W_feedback))
y_t = theano.dot(x_tm3, W_out)
return [x_t, y_t]
这是文档扫描:
W = T.matrix()
W_in_1 = T.matrix()
W_in_2 = T.matrix()
W_feedback = T.matrix()
W_out = T.matrix()
u = T.matrix() # it is a sequence of vectors
x0 = T.matrix() # initial state of x has to be a matrix, since
# it has to cover x[-3]
y0 = T.vector() # y0 is just a vector since scan has only to provide
# y[-1]
([x_vals, y_vals], updates) = theano.scan(fn=oneStep,
sequences=dict(input=u, taps=[-4,-0]),
outputs_info=[dict(initial=x0, taps=[-3,-1]), y0],
non_sequences=[W, W_in_1, W_in_2, W_feedback, W_out],
strict=True)
# for second input y, scan adds -1 in output_taps by default
函数的返回值为:'[x_t,y_t]',outputs_info
为[dict(initial=x0, taps=[-3,-1]), y0 ]
...
在我的实现中,函数的返回是:[o_t,s_t]
,outputs_info
是[None, dict(initial=T. Zeros(self.hidden_dim))]
...这是有道理的,因为我没有理由将输出传递到函数中...
最佳答案
我在将 RNN 应用于 NLP 任务时遇到了完全相同的问题。出现此错误的原因是 x_t
的类型forward_prop_step
的参数函数,由于迭代 ivector x
,因此它是标量 。
这里的解决方案是使用向量。例如,x_tv
是一个在 x_t
处全有 0 和 1 的向量索引。
def forward_prop_step(x_t, s_t_prev, U, V, W):
x_tv = T.eye(1, m=input_size, k=x_t)[0]
s_t = T.tanh(U.dot(x_tv) + V.dot(s_t_prev))
o_t = T.tanh(W.dot(s_t))
return [o_t, s_t]
关于python - Theano 循环错误 : Outputs_info dim and fn output dim mismatch in scan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34581230/