Skip to content

_mean_squared_error

mean_squared_error

Calculate mean squared error between two tensors.

This method can be used with TensorFlow tensors:

true = tf.constant([[0,1,0,0], [0,0,0,1], [0,0,1,0], [1,0,0,0]])
pred = tf.constant([[0.1,0.9,0.05,0.05], [0.1,0.2,0.0,0.7], [0.0,0.15,0.8,0.05], [1.0,0.0,0.0,0.0]])
b = fe.backend.mean_squared_error(y_pred=pred, y_true=true)  # [0.0063, 0.035, 0.016, 0.0]
true = tf.constant([[1], [3], [2], [0]])
pred = tf.constant([[2.0], [0.0], [2.0], [1.0]])
b = fe.backend.mean_squared_error(y_pred=pred, y_true=true)  # [1.0, 9.0, 0.0, 1.0]

This method can be used with PyTorch tensors:

true = torch.tensor([[0,1,0,0], [0,0,0,1], [0,0,1,0], [1,0,0,0]])
pred = torch.tensor([[0.1,0.9,0.05,0.05], [0.1,0.2,0.0,0.7], [0.0,0.15,0.8,0.05], [1.0,0.0,0.0,0.0]])
b = fe.backend.mean_squared_error(y_pred=pred, y_true=true)  # [0.0063, 0.035, 0.016, 0.0]
true = torch.tensor([[1], [3], [2], [0]])
pred = torch.tensor([[2.0], [0.0], [2.0], [1.0]])
b = fe.backend.mean_squared_error(y_pred=pred, y_true=true)  # [1.0, 9.0, 0.0, 1.0]

Parameters:

Name Type Description Default
y_true Tensor

Ground truth class labels with a shape like (batch) or (batch, n_classes). dtype: int, float16, float32.

required
y_pred Tensor

Prediction score for each class, with a shape like y_true. dtype: float32 or float16.

required

Returns:

Type Description
Tensor

The MSE between y_true and y_pred

Raises:

Type Description
ValueError

If y_pred is an unacceptable data type.

Source code in fastestimator/fastestimator/backend/_mean_squared_error.py
def mean_squared_error(y_true: Tensor, y_pred: Tensor) -> Tensor:
    """Calculate mean squared error between two tensors.

    This method can be used with TensorFlow tensors:
    ```python
    true = tf.constant([[0,1,0,0], [0,0,0,1], [0,0,1,0], [1,0,0,0]])
    pred = tf.constant([[0.1,0.9,0.05,0.05], [0.1,0.2,0.0,0.7], [0.0,0.15,0.8,0.05], [1.0,0.0,0.0,0.0]])
    b = fe.backend.mean_squared_error(y_pred=pred, y_true=true)  # [0.0063, 0.035, 0.016, 0.0]
    true = tf.constant([[1], [3], [2], [0]])
    pred = tf.constant([[2.0], [0.0], [2.0], [1.0]])
    b = fe.backend.mean_squared_error(y_pred=pred, y_true=true)  # [1.0, 9.0, 0.0, 1.0]
    ```

    This method can be used with PyTorch tensors:
    ```python
    true = torch.tensor([[0,1,0,0], [0,0,0,1], [0,0,1,0], [1,0,0,0]])
    pred = torch.tensor([[0.1,0.9,0.05,0.05], [0.1,0.2,0.0,0.7], [0.0,0.15,0.8,0.05], [1.0,0.0,0.0,0.0]])
    b = fe.backend.mean_squared_error(y_pred=pred, y_true=true)  # [0.0063, 0.035, 0.016, 0.0]
    true = torch.tensor([[1], [3], [2], [0]])
    pred = torch.tensor([[2.0], [0.0], [2.0], [1.0]])
    b = fe.backend.mean_squared_error(y_pred=pred, y_true=true)  # [1.0, 9.0, 0.0, 1.0]
    ```

    Args:
        y_true: Ground truth class labels with a shape like (batch) or (batch, n_classes). dtype: int, float16, float32.
        y_pred: Prediction score for each class, with a shape like y_true. dtype: float32 or float16.

    Returns:
        The MSE between `y_true` and `y_pred`

    Raises:
        ValueError: If `y_pred` is an unacceptable data type.
    """
    if tf.is_tensor(y_pred):
        mse = tf.losses.MSE(y_true, y_pred)
    elif isinstance(y_pred, torch.Tensor):
        mse = reduce_mean(torch.nn.MSELoss(reduction="none")(y_pred, y_true), axis=-1)
    else:
        raise ValueError("Unrecognized tensor type {}".format(type(y_pred)))
    return mse