【问题标题】:Keras : How to find indices from a tensor for a particular value similar to numpy.where()Keras:如何从张量中找到类似于 numpy.where() 的特定值的索引
【发布时间】:2017-09-19 02:26:42
【问题描述】:

我正在寻找一个类似于 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 is not None: 而不是 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 ]

【问题讨论】:

  • 如果满足if条件你想做什么?
  • 我想找到等于数字的索引。例如: y_true = [0 1 1 0 0 2 3 ] for i in range(0,7) a=[] if y_true(i)==1 a.append(i) then a=[1,2] I想用 keras 来做。但是,keras 布尔张量不支持 if 命令

标签: keras


【解决方案1】:

您应该尝试以下方法:

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

当你的后端是 tensorflow 时。

希望有帮助

【讨论】:

  • 有什么类似的东西适用于其他后端吗?
  • 这行不通,tf.where 返回坐标而不是索引
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 2023-04-03
  • 2018-07-28
  • 1970-01-01
  • 2016-03-03
相关资源
最近更新 更多