当代码几乎相同时,为什么theano扫描的工作方式不同?

时间:2015-09-11 17:30:40

标签: theano

以下代码:

import theano
import numpy as np
from theano import tensor as T
h1=T.as_tensor_variable(np.zeros((1, 20), dtype=theano.config.floatX))
s1=T.as_tensor_variable(np.zeros((1, 20), dtype=theano.config.floatX))

def forward(input, h, s):
    return h, s
result, update=theano.scan(fn=forward, sequences=[T.arange(10)], outputs_info=[h1, s1], go_backwards=False)
print result[0].shape.eval()

有错误:

TypeError: Cannot convert Type TensorType(float32, 3D) (of Variable IncSubtensor{Set;:int64:}.0) into Type TensorType(float32, (False, True, False)). You can try to manually convert IncSubtensor{Set;:int64:}.0 into a TensorType(float32, (False, True, False)).

但是,当我将1更改为任何其他数字时,例如:

h1=T.as_tensor_variable(np.zeros((2, 20), dtype=theano.config.floatX))
s1=T.as_tensor_variable(np.zeros((2, 20), dtype=theano.config.floatX))

工作正常。

我不知道这里发生了什么。有人能帮助我吗?

1 个答案:

答案 0 :(得分:2)

请关注此帖:https://github.com/Theano/Theano/issues/2985

当调用theano.scan时,传递形状包括1作为outputs_info的一部分的张量失败,除非使用tensor.unbroadcast手动取消形状1的那些轴。这是由于扫描内部函数的实际返回与通过outputs_info传递的相应函数之间的广播模式不同。

尝试:

h1=T.unbroadcast(T.as_tensor_variable(np.zeros((1, 20), dtype=theano.config.floatX)), 0)
s1=T.unbroadcast(T.as_tensor_variable(np.zeros((1, 20), dtype=theano.config.floatX)), 0)

使第一个维度不可广播。