[Python] Mean squared error computed by TensorFlow

Discussão em 'Python' iniciado por Stack, Junho 4, 2025.

  1. Stack

    Stack Membro Participativo

    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):

    [​IMG]

    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 .

    Continue reading...

Compartilhe esta Página