【问题标题】:Remove values from a tensor that are within a given range从给定范围内的张量中删除值
【发布时间】:2019-10-30 03:39:38
【问题描述】:

当预测高于或低于某个阈值时,我希望能够知道我的神经网络的舍入精度。例如,我希望它仅在预测高于 0.55 或低于 0.45 时计算准确度,以便过滤掉接近 50/50 的情况。

我尝试在 stackoverflow 上使用 soft_acc 函数,并在开头添加一个 if else 以过滤掉接近 50/50 秒。

def soft_acc(y_true, y_pred):
    if y_pred > 0.55 or y_pred < 0.45:
        return K.mean(K.equal(K.round(y_true), K.round(y_pred)))

我收到以下错误消息。

TypeError:不允许将tf.Tensor 用作Python bool。使用if t is not None: 而不是if t: 来测试是否定义了张量,并使用 TensorFlow 操作(例如 tf.cond)来执行以张量值为条件的子图。

【问题讨论】:

  • y_pred[0.45, 0.55]之间时你想做什么?
  • 我希望它对 soft_acc 度量值不做任何事情。 @bluesummers

标签: python tensorflow tensor


【解决方案1】:

使用tf.boolean_mask 过滤掉不满足所需阈值的索引处的值。

# remove values from `X` in interval (lo, hi)
mask = tf.math.logical_or(tf.lesser(X, lo), tf.greater(X, hi))
X = tf.boolean_mask(X, mask)

在您的情况下,您可以将soft_acc 定义为

def soft_acc(y_true, y_pred):
    mask = tf.math.logical_or(tf.greater(y_pred, 0.55), tf.lesser(y_pred, 0.45))
    y_true2 = tf.boolean_mask(y_true, mask)
    y_pred2 = tf.boolean_mask(y_pred, mask)

    return K.mean(K.equal(K.round(y_true2), K.round(y_pred2)))

【讨论】:

    猜你喜欢
    • 2017-11-09
    • 2022-06-24
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多