gpt4 book ai didi

python - 如何在 Theano 中进行 0/1 子集化?

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

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

import theano
import theano.tensor as T

a = T.vector('X', dtype='int64')
b = T.vector('Y', dtype='int64')
c = a[b]
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)

这会打印

[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)

希望对你有帮助

关于python - 如何在 Theano 中进行 0/1 子集化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36500777/

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