Skip to content

mean_squared_error

MeanSquaredError

Bases: LossOp

Calculate the mean squared error loss between two tensors.

Parameters:

Name Type Description Default
inputs Union[Tuple[str, str], List[str]]

A tuple or list like: [, ].

required
outputs str

String key under which to store the computed loss.

required
mode Union[None, str, Iterable[str]]

What mode(s) to execute this Op in. For example, "train", "eval", "test", or "infer". To execute regardless of mode, pass None. To execute in all modes except for a particular one, you can pass an argument like "!infer" or "!train".

'!infer'
ds_id Union[None, str, Iterable[str]]

What dataset id(s) to execute this Op in. To execute regardless of ds_id, pass None. To execute in all ds_ids except for a particular one, you can pass an argument like "!ds1".

None
average_loss bool

Whether to average the element-wise loss after the Loss Op.

True
Source code in fastestimator/fastestimator/op/tensorop/loss/mean_squared_error.py
@traceable()
class MeanSquaredError(LossOp):
    """Calculate the mean squared error loss between two tensors.

    Args:
        inputs: A tuple or list like: [<y_pred>, <y_true>].
        outputs: String key under which to store the computed loss.
        mode: What mode(s) to execute this Op in. For example, "train", "eval", "test", or "infer". To execute
            regardless of mode, pass None. To execute in all modes except for a particular one, you can pass an argument
            like "!infer" or "!train".
        ds_id: What dataset id(s) to execute this Op in. To execute regardless of ds_id, pass None. To execute in all
            ds_ids except for a particular one, you can pass an argument like "!ds1".
        average_loss: Whether to average the element-wise loss after the Loss Op.
    """
    def __init__(self,
                 inputs: Union[Tuple[str, str], List[str]],
                 outputs: str,
                 mode: Union[None, str, Iterable[str]] = "!infer",
                 ds_id: Union[None, str, Iterable[str]] = None,
                 average_loss: bool = True):
        self.average_loss = average_loss
        super().__init__(inputs=inputs, outputs=outputs, mode=mode, ds_id=ds_id)

    def forward(self, data: List[Tensor], state: Dict[str, Any]) -> Tensor:
        y_pred, y_true = data
        loss = mean_squared_error(y_true=y_true, y_pred=y_pred)
        if self.average_loss:
            loss = reduce_mean(loss)
        return loss