Reputation: 173
When using losses MeanSquaredError like below the ouput is coming as exepected
y_true = tf.constant([[1., 9.], [2., 5.]])
y_pred = tf.constant([[4., 8.], [12., 3.]])
sample_weight = tf.constant([1.2, 0.5])
mse = tf.keras.losses.MeanSquaredError()
print("mse w/o weights", mse(y_true, y_pred).numpy())
print("mse w weights", mse(y_true, y_pred, sample_weight=sample_weight).numpy())
output -
mse w/o weights 28.5
mse w weights 16.0
Expected -
# mse = [((4 - 1)^2 + (8 - 9)^2) / 2, ((12 - 2)^2 + (3 - 5)^2) / 2]
# mse = [5, 52]
# weighted_mse = [5 * 1.2, 52 * 0.5] = [6, 26]
# reduced_weighted_mse = (6 + 26) / 2 = 16
Where as when metrics MeanSquaredError is used, the result with weights is coming different
mse = tf.keras.metrics.MeanSquaredError()
mse.update_state(y_true, y_pred)
print("mse w/o weights", mse.result().numpy())
mse = tf.keras.metrics.MeanSquaredError()
mse.update_state(y_true, y_pred, sample_weight=sample_weight)
print("mse w weights", mse.result().numpy())
output -
mse w/o weights 28.5
mse w weights 18.823528
Why the metrics output with weights is different from loss output?
Upvotes: 2
Views: 604
Reputation: 16876
tf.keras.losses.MeanSquaredError
: sample_weight
acts as a
coefficient for the loss. Hence, it is mean of [5 * 1.2, 52 * 0.5]=16.0
.tf.keras.metrics.MeanSquaredError
: sample_weight
does weighted average of samples. Hence it is (5 * 1.2 + 52 * 0.5)/(1.2+0.5)=32/1.7=18.823
Upvotes: 4