我是 Theano 的初学者,我正在使用另一个代码的示例,大概在某个时候可以工作(但是,我修改了它......但我很确定我的修改无关目前出了什么问题)。
无论如何,我正在尝试调试 Theano 扫描...我认为我观察到的是扫描函数中的一个基本错误。
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 是一个 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_infoin 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 任务时遇到了完全相同的问题。发生此错误的原因是forward_prop_step 函数的x_t 参数类型,它是标量,因为遍历了 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]
