gpt4 book ai didi

python - 返回 Theano 向量中值的索引

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

在 Theano 中返回 Vector 中特定值的索引的过程是什么?在 NumPy 中,这将是 numpy.where(my_array==x)。 Theano的Tensor.where是一个switch语句。

最佳答案

numpy.where(condition, [x ,y]) 有两种行为。 Theano 始终支持您向 where() 提供 3 个参数。正如 NumPy 文档 [1] 中所述,numpy.where(cond) 等同于 nonzero()。

你可以在 Theano 中这样做:

import theano
import numpy as np
v = np.arange(10)
var = theano.tensor.vector()
out = theano.tensor.eq(var, 2).nonzero()[0]
print out.eval({var: v})

检查第 5 行。NumPy nonzero() 返回一个元组。 Theano 也是这样做的。在 nonzero() 的输入中,该元组中的每一维都有一个向量。

[1] http://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html

关于python - 返回 Theano 向量中值的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20590909/

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