Keras:如何从张量中找到与numpy.where()类似的特定值的索引 [英] Keras : How to find indices from a tensor for a particular value similar to numpy.where()

查看:613
本文介绍了Keras:如何从张量中找到与numpy.where()类似的特定值的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am searching for a Keras command which is similar to python "numpy.where()" command. Basically, my idea is to extract the indices from a tensor. In python I can do simply f_j=(np.where(X==j)) which gives me specific indices(f_j) for the value j.

例如:

X= [0 1 1 0 0 2 3 ]

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

f_j= [1 2]

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

Is there is any similar function which I can use for this purpose ?

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

I tried to write array search inside a tensor. However, I end up with error when calling "if K.equal():" line as

TypeError:不允许将tf.Tensor用作Python bool.如果t 不为None:代替if t:测试是否定义了张量并使用 TensorFlow操作(例如tf.cond)执行以 张量的值.

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

当您的后端是张量流时.

when your backend is tensorflow.

希望有帮助

这篇关于Keras:如何从张量中找到与numpy.where()类似的特定值的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆