gpt4 book ai didi

Keras : How to find indices from a tensor for a particular value similar to numpy. where()

转载 作者:行者123 更新时间:2023-12-04 14:55:24 24 4
gpt4 key购买 nike

我正在搜索类似于python "numpy.where()"命令的Keras命令。基本上,我的想法是从张量中提取索引。在python中,我可以简单地执行f_j=(np.where(X==j)),从而为indices(f_j)值提供特定的j

前任:

X= [0 1 1 0 0 2 3 ]

f_j=(np.where(X==1))

f_j= [1 2]

我可以为此目的使用任何类似的功能吗?

我试图在张量中编写数组搜索。但是,当调用 "if K.equal():"行为时,我最终会出错

TypeError: Using a tf.Tensor as a Python bool is not allowed. Use if t is not None: instead of if t: to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.


def loss(y_true, y_pred:

b=K.equal(y_true,0)

b=K.cast(b,dtype='float32')

for i in range(0,5):

if K.equal(b[i],1):

........

y_true = [0 1 1 0 0 2 3 ]

最佳答案

您应该尝试类似的方法:

from keras import backend as K
value = 5
wh = K.tf.where(K.tf.equal(x,value))

当您的后端是tensorflow时。

希望能有所帮助

关于Keras : How to find indices from a tensor for a particular value similar to numpy. where(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43553646/

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