与Theano扫描功能简单积累的麻烦

时间:2015-11-02 10:26:36

标签: theano

我正在尝试累积矩阵值,同时乘以步骤: res = sum_i(i * a)。我的代码如下所示:

import numpy as np
from theano import function, scan
import theano.tensor as T

x = T.lmatrix()
results, updates = scan(
    lambda res, step, x: res + step * x, 
    non_sequences=x,
    sequences=T.arange(2),
    outputs_info=T.zeros_like(x))

f = function([x], results)
a = np.array([[0, 0], [2, 2]], 'int64')
print(f(a))

输出:

[[[0 0]
  [0 0]]

[[1 1]
 [1 1]]]

虽然我希望如此:

[[[0 0]
  [0 0]]

[[0 0]
 [2 2]]]

1 个答案:

答案 0 :(得分:1)

输出(也许并不令人惊讶?)正确。获得此输出的原因如下:

在第一次迭代中,

res = 0
step = [[0, 0], [0, 0]]
x = [[0, 0], [2, 2]]

等等

res + step * x = 0 + [[0, 0], [0, 0]] * [[0, 0], [2, 2]]
               = 0 + [[0, 0], [0, 0]]
               = [[0, 0], [0, 0]]

在第二次迭代中,

res = 1
step = [[0, 0], [0, 0]]
x = [[0, 0], [2, 2]]

等等

res + step * x = 1 + [[0, 0], [0, 0]] * [[0, 0], [2, 2]]
               = 1 + [[0, 0], [0, 0]]
               = [[1, 1], [1, 1]]

请注意,1被广播为与stepx的元素乘法产生的矩阵相同的形状。