Skip to content
class

MseLossBackward

MseLossBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for mean-squared-error loss.

Computes

L=1N∑i=1N(y^i−yi)2 \mathcal{L} = \frac{1}{N} \sum_{i=1}^{N} (\hat{y}_i - y_i)^2

with Mean reduction; for Sum the leading 1/N1/N factor is dropped, and for None the full per-element squared error (y^i−yi)2(\hat{y}_i - y_i)^2 is returned without reduction. Both input and target are saved so that the backward pass can re-emit ∇y^=2N(y^−y)⋅g\nabla_{\hat y} = \frac{2}{N} (\hat y - y) \cdot g (or 2(y^−y)g2 (\hat y - y) g for Sum, or 2(y^−y)⊙g2 (\hat y - y) \odot g for None).

Math

Lmse(y^,y)={1N∥y^−y∥22Mean∥y^−y∥22Sum(y^−y)2None \mathcal{L}_{\text{mse}}(\hat{y}, y) = \begin{cases} \frac{1}{N} \lVert \hat{y} - y \rVert_2^2 & \text{Mean} \\ \lVert \hat{y} - y \rVert_2^2 & \text{Sum} \\ (\hat{y} - y)^2 & \text{None} \end{cases}

Attributes

schema_v1OpSchema
"mse_loss", AmpPolicy::ForceFP32.
reduction_Reduction
Reduction mode applied by the forward and unwound by the backward.
orig_shape_Shape
Element-wise shape before reduction; needed to broadcast grad_out back to the input shape during apply.

Notes

Gradient magnitude scales linearly with the residual, so MSE is sensitive to outliers. Prefer HuberLoss when robustness matters.

Static methods

1
static

forward

→TensorImplPtr
int forward(const int & input, const int & target, Reduction reduction)

Compute the MSE loss with autograd wiring.

Parameters

inputTensorImplPtr
Predicted tensor of arbitrary shape.
targetTensorImplPtr
Ground-truth tensor. Must share input's shape and dtype.
reductionReduction
Mean, Sum, or None.

Returns

TensorImplPtr

Scalar (Mean / Sum) or per-element (None) loss tensor.

Raises

ShapeMismatch
If input.shape() != target.shape().
DtypeMismatch
If input.dtype() != target.dtype().

Methods

2
fn

apply

→std::vector<Storage>
int apply(int grad_out)

Backward pass: emits gradients w.r.t. input and target.

Returns

std::vector<Storage>

{grad_input, grad_target} of the same shape as the originals.

fn

apply_for_graph

int apply_for_graph(const int & grad_out)

The same gradients, recorded, for create_graph=True.