I am trying to figure out how the mean squared error (MSE) is calculated by tensorflow
and was reading the post at https://www.tensorflow.org/api_docs/python/tf/keras/metrics/mean_squared_error.
First of all, MSE is defined as (see https://en.wikipedia.org/wiki/Mean_squared_error):
Suppose I have a single output and create true and predicted values.
import numpy as np
import random
y_true = np.random.randint(0, 10, size=(2, 1))
print(y_true,"\n")
y_pred = np.random.randint(0,5,size=(2, 1))
print(y_pred)
[[7]
[5]]
[[2]
[2]]
When I call tf.keras.losses.mean_squared_error(y_true, y_pred)
, what I expect to see is that [(7-2)^2 + (5-2)^2]/2 = 17
, however, it returns me array([25, 9])
. Why doesn't tensorflow compute the mean?
Then, I increase the column numbers.
y_true = np.random.randint(0, 10, size=(2, 3))
print(y_true,"\n")
y_pred = np.random.randint(0,5,size=(2, 3))
print(y_pred)
[[2 6 0]
[3 3 4]]
[[4 2 4]
[3 4 2]]
The answer returned by tensorflow
is array([12, 1])
. I'm not able to understand how these values are computed. What I was expecting was [(2-4)^2+ (6-2)^2+(0-4)^2]/2 + [(3-3)^2 + (3-4)^2+ (4-2)^2]/2
.
The second value is computed as
a = np.array([2,6,0])
b = np.array([4,2,4])
((b - a)**2).mean()
Or [(2-4)^2+ (6-2)^2+(0-4)^2]/3
According to their doc it is equivalent to np.mean(np.square(y_true - y_pred), axis=-1)
So it is computing the mse row-wise.