Theano 中的切片和索引

Slicing and indexing in Theano

我想在 Theano 中索引张量变量:

我想得到[[1,2],[4,5],[7,8]][[2,3],[5,6],[8,9]]

对于 numpy 变量,我会简单地分别执行 x[:,0:-1]x[:,1:x.shape[0]],但我不知道如何在 Theano 中获得我想要的结果。

你在 Theano 中的操作方式与在 numpy 中的操作方式相同:

import theano
import theano.tensor as T

x = T.imatrix('x')
y = x[:, 0: -1]
z = x[:, 1: x.shape[0]]

f = theano.function([x], y)
g = theano.function([x], z)

x_ = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(f(x_))
print(g(x_))