【问题标题】:Select index of a 2D Tensor with exact values选择具有精确值的二维张量的索引
【发布时间】:2023-02-06 20:13:48
【问题描述】:

很抱歉问了这么一个微不足道的问题,但我是 Tensorflow 的新手。 我有两个张量。 y_true = [[1,0], [0,1], [1,0], [1,0], [0,1], [0,1], [1,0], [0,1] , [1,0], [0,1]] y_pred = [[0.6,0.4], [0.3,0.7], [0.8,0.2], [0.8,0.2], [0.3,0.7],[0.1,0.9],[0.9, 0.1],[0.4,0.6] ,[0.6,0.4],[0.2,0.8]] 此外,我想根据每个 [1,0] 或 [0,1] 值过滤 y_true 。

我有以下概念,我认为它不是很有效。例如,在 [0,1] 上过滤 y_true 时:

ind_zero   = tf.math.equal(y_true,[1,0])
index_zero = tf.math.logical_and(ind_zero[:,0],ind_zero[:,1])
zeros      = tf.gather_nd(y_pred,tf.where(index_zero))

是否存在另一种更有效的想法? 提前致谢。

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    您可以在 [1,0] 上过滤 y_true:

    zeros = tf.gather_nd(y_pred,tf.where(tf.argmin(y_true, axis = 1)))
    

    [0,1] 同样使用 argmax 而不是 argmin:

    zeros = tf.gather_nd(y_pred,tf.where(tf.argmax(y_true, axis = 1)))
    

    【讨论】:

      猜你喜欢
      • 2019-09-15
      • 1970-01-01
      • 2020-09-26
      • 2017-10-12
      • 2020-05-11
      • 2017-12-20
      • 1970-01-01
      • 2021-06-27
      • 2021-11-03
      相关资源
      最近更新 更多