Python 如何在Theano中创建0/1子集?

Python 如何在Theano中创建0/1子集?,python,syntax,subset,theano,matrix-indexing,Python,Syntax,Subset,Theano,Matrix Indexing,目标是通过另一个数组中提供的值获得元素数组的子集 import theano 将无张量导入为T a=T.vector('X',dtype='int64') b=T.vector('Y',dtype='int64') c=a[b] g=函数([a,b],c) x=np.数组([5,3,2,3,4,6],dtype=int) y=np.数组([0,0,1,0,0,1],dtype=int) 打印g(x,y) 这张照片 [5 5 3 5 5 3] 而不是 [2 6] 如何获得预期结果?尝试使用n

目标是通过另一个数组中提供的值获得元素数组的子集

import theano
将无张量导入为T
a=T.vector('X',dtype='int64')
b=T.vector('Y',dtype='int64')
c=a[b]
g=函数([a,b],c)
x=np.数组([5,3,2,3,4,6],dtype=int)
y=np.数组([0,0,1,0,0,1],dtype=int)
打印g(x,y)
这张照片

[5 5 3 5 5 3]
而不是

[2 6]
如何获得预期结果?

尝试使用
nonzero()
函数

你的例子是:

import theano
import theano.tensor as T

a = T.vector('X', dtype='int64')
b = T.vector('Y', dtype='int64')
c = a[b.nonzero()]
g = function([a,b],c)

x = np.array([5,3,2,3,4,6], dtype=int)
y = np.array([0,0,1,0,0,1], dtype=int)
print g(x,y)

希望对你有所帮助

太好了!这正是我要找的!通常,我会对
numpy
数组使用
astype(bool)
,但我担心我不能对张量这样做。我还注意到
nonzero()
也适用于
numpy
。非常感谢。这个问题快把我逼疯了。