【问题标题】:How to iterate over the rows of a theano matrix using the scan function in theano?如何使用theano中的scan函数遍历theano矩阵的行?
【发布时间】:2017-06-19 00:50:20
【问题描述】:

我正在编写一个简单的代码来计算索引列表的单热编码。 例如:[1,2,3] => [[0,1,0,0],[0,0,1,0],[0,0,0,1]]

我写了一个函数来对单个向量做同样的事情:

n_val =4
def encoding(x_t):
    z = T.zeros((x_t.shape[0], n_val))
    one_hot = T.set_subtensor(z[T.arange(x_t.shape[0]), x_t], 1)
    return one_hot

要在矩阵的行上重复相同的功能,我执行以下操作,

x = T.imatrix()
[m],_ = theano.scan(fn = encoding, sequences = x)

Y = T.stacklists(m)
f= theano.function([x],Y)

我期待一个 3D 张量,每个切片对应于矩阵行的 one-hot 编码。

编译函数时出现以下错误,

/Library/Python/2.7/site-packages/theano/tensor/var.pyc in __iter__(self)
594         except TypeError:
595             # This prevents accidental iteration via builtin.sum(self)
--> 596             raise TypeError(('TensorType does not support iteration. '
    597                              'Maybe you are using builtin.sum instead of '
598                              'theano.tensor.sum? (Maybe .max?)'))

TypeError: TensorType does not support iteration. Maybe you are using builtin.sum instead of theano.tensor.sum? (Maybe .max?)

有人可以帮我理解我哪里出错了,以及如何修改代码以获得我需要的东西吗?

提前致谢。

【问题讨论】:

  • fn scan 的参数需要返回一个列表

标签: python theano theano.scan


【解决方案1】:

这是有效的代码

# input a matrix, expect scan to work with each row of matrix
my_matrix = np.asarray([[1,2,3],[1,3,2],[1,1,1]])

x = T.imatrix()

def encoding(idx):
    z = theano.tensor.zeros((idx.shape[0], 4))
    one_hot = theano.tensor.set_subtensor(z[theano.tensor.arange(idx.shape[0]), idx], 1)
    return one_hot

m, update = theano.scan(fn=encoding,
                        sequences=x)


f = theano.function([x], m)

##########3
result = f(my_matrix)
print (result)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    相关资源
    最近更新 更多