【发布时间】:2021-10-13 13:56:59
【问题描述】:
这是目前的功能。在这里,它会从 MSE 中删除 y_true 小于阈值(此处为 0.1)的任何值。
def my_loss(y_true,y_pred):
loss = tf.square(y_true-y_pred)
# if any y_true is less than a threshold (say 0.1)
# the element is removed from loss, and does not affect MSE
loss = tf.where(y_true<0.1)
# return mean of losses
return tf.reduce_mean(loss)
这个可以编译,但是网络从来没有学会很好地预测 0。相反,我只想消除那些 y_true 和 y_pred 都小于某个阈值的值。这是因为它需要首先学习如何预测 0,然后在稍后的训练中忽略这些点。
然而,这并不能编译。
def my_better_loss(y_true,y_pred):
loss = tf.square(y_true-y_pred)
# remove all elements where BOTH y_true & y_pred < threshold
loss = tf.where(y_true<0.1 and y_pred<0.1)
# return mean of losses
return tf.reduce_mean(loss)
导致如下错误。
(0) Invalid argument: The second input must be a scalar, but it has shape [25,60,60]
[[{{node replica_1/customMSE/cond/switch_pred/_51}}]]
(1) Invalid argument: The second input must be a scalar, but it has shape [25,60,60]
[[{{node replica_1/customMSE/cond/switch_pred/_51}}]]
[[customMSE/cond/Squeeze/_59]]
(2) Invalid argument: The second input must be a scalar, but it has shape [25,60,60]
[[{{node replica_1/customMSE/cond/replica_1/customMSE/Less/_55}}]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_4715]
Function call stack:
train_function -> train_function -> train_function
编辑:
更具体一点。假设我们的阈值是 0.5:
y_true = [0.3, 0.4, 0.6, 0.7]
y_pred = [0.2, 0.7, 0.5, 1]
然后损失函数将在删除第一个元素的情况下计算 mse,因为 y_pred[0] 和 y_true[0] 都小于阈值。
# MSE would be computed between
y_true = [0.4, 0.6, 0.7]
#and
y_pred = [0.7, 0.5, 1]
【问题讨论】:
-
你可以轻松做到这一点,但如果批次中 y_pred 和 y_true 的所有元素都小于阈值,则损失将是 nan
-
真的!有什么办法吗?我可以检查
if loss == np.nan然后loss = 0这是可以接受的,因为它可以正确预测该图像。您想到了什么实现方式? -
您可以简单地将所有低于等于零的阈值(如 relu 实现)这样计算损失时没有 nan 问题
-
@MarcoCerliani 但是,我希望
truth <= threshold但pred > threshold时有非零损失。没有它,网络不会因为在任何地方都放置非零值而受到惩罚
标签: python tensorflow keras neural-network