gpt4 book ai didi

python - Theano 中的切片和索引

转载 作者:太空宇宙 更新时间:2023-11-04 00:44:34 25 4
gpt4 key购买 nike

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

  • x 的类型为 theano.tensor.var.TensorVariable(例如 [[1,2,3],[4,5,6],[7,8,9]])<

我想得到 [[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_))

关于python - Theano 中的切片和索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40310464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com