Theano 循环中的错误:Outputs_info dim 和 fn 输出 dim 在扫描中不匹配
Error in Theano loop: Outputs_info dim and fn output dim mismatch in scan
我是 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_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
的 return 尺寸,但到目前为止似乎没有任何效果。
我目前正在查看文档...但是,从文档来看,我所做的似乎是正确的(下面是文档中的示例):
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
函数的return是:'[x_t,y_t]'而outputs_info
是[dict(initial=x0, taps=[-3,-1]), y0]
...
虽然在我的实现中,函数的 return 是:[o_t,s_t]
而 outputs_info
是 [None, dict(initial=T.zeros(self.hidden_dim))]
...这是有道理的,因为我没有将我的输出传递给函数的原因...
尝试以下方法?注意 (self.hidden_dim, )
和 (self.hidden_dim)
的区别
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]
我是 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_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
的 return 尺寸,但到目前为止似乎没有任何效果。
我目前正在查看文档...但是,从文档来看,我所做的似乎是正确的(下面是文档中的示例):
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
函数的return是:'[x_t,y_t]'而outputs_info
是[dict(initial=x0, taps=[-3,-1]), y0]
...
虽然在我的实现中,函数的 return 是:[o_t,s_t]
而 outputs_info
是 [None, dict(initial=T.zeros(self.hidden_dim))]
...这是有道理的,因为我没有将我的输出传递给函数的原因...
尝试以下方法?注意 (self.hidden_dim, )
和 (self.hidden_dim)
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]