Keras:如何从张量中找到与numpy.where()类似的特定值的索引

时间:2017-04-21 23:52:10

标签: keras

我正在搜索类似于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:不允许使用tf.Tensor作为Python bool。如果t,请使用   不是无:而不是if t:来测试是否定义了张量,并使用   TensorFlow操作如tf.cond来执行以子条件为条件的子图   张量值。

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 ]

1 个答案:

答案 0 :(得分:4)

你应该尝试类似的东西:

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

当你的后端是张量流时。

希望有所帮助