根据文档,该Reduction参数有 3 个值 - SUM_OVER_BATCH_SIZE、SUM和NONE。
y_true = [[0., 2.], [0., 0.]]
y_pred = [[3., 1.], [2., 5.]]
mae = tf.keras.losses.MeanAbsoluteError(reduction=tf.keras.losses.Reduction.SUM)
mae(y_true, y_pred).numpy()
> 5.5
mae = tf.keras.losses.MeanAbsoluteError()
mae(y_true, y_pred).numpy()
> 2.75
经过各种试验后我可以推断出的计算结果是:-
当REDUCTION = SUM,
Loss = Sum over all samples {(Sum of differences between y_pred and y_target vector of each sample / No of element in y_target of the sample )} = { (abs(3-0) + abs(1-2))/2 } + { (abs(2-0) + abs(5-0))/2 } = {4/2} + {7/2} = 5.5.
当REDUCTION = SUM_OVER_BATCH_SIZE,
Loss = [Sum over all samples {(Sum of differences between y_pred and y_target vector of each sample / No of element in y_target of the sample )}] / Batch_size or No of Samples = [ { (abs(3-0)} + abs(1-2))/2 } + { (abs(2-0) + abs(5-0))/2 } ]/2 = [ {4/2} + {7/2} ]/2 = [5.5]/2 = 2.75.
结果,SUM_OVER_BATCH_SIZE无非是SUM/batch_size。那么,为什么要调用它呢SUM_OVER_BATCH_SIZE?实际上是SUM将整个批次的损失相加,同时SUM_OVER_BATCH_SIZE计算该批次的平均损失。
SUM_OVER_BATCH_SIZE我关于和的运作的假设是否SUM正确?
回首忆惘然
犯罪嫌疑人X
随时随地看视频慕课网APP
相关分类