# mse

Half mean squared error

## Syntax

``dlY = mse(dlX,targets)``
``dlY = mse(dlX,targets,'DataFormat',FMT)``

## Description

The half mean squared error operation computes the half mean squared error loss between network predictions and target values for regression tasks.

The loss is calculated using the following formula

`$\text{loss}=\frac{1}{2N}\sum _{i=1}^{M}{\left({X}_{i}-{T}_{i}\right)}^{2}$`

where Xi is the network response, Ti is the target value, M is the total number of responses in X (across all observations), and N is the total number of observations in X.

Note

This function computes the half mean squared error loss between predictions and targets stored as `dlarray` data. If you want to calculate the half mean squared error loss within a `layerGraph` object or `Layer` array for use with `trainNetwork`, use the following layer:

example

````dlY = mse(dlX,targets)` computes the half mean squared error loss between the predictions `dlX` and the target values `targets` for regression problems. The input `dlX` is a formatted `dlarray` with dimension labels. The output `dlY` is an unformatted scalar `dlarray` with no dimension labels.```
````dlY = mse(dlX,targets,'DataFormat',FMT)` also specifies the dimension format `FMT` when `dlX` is not a formatted `dlarray`. ```

## Examples

collapse all

The half mean squared error evaluates how well the network predictions correspond to the target values.

Create the input predictions as a single observation of random values with a height and width of six and a single channel.

```height = 6; width = 6; channels = 1; observations = 1; X = rand(height,width,channels,observations); dlX = dlarray(X,'SSCB')```

Create the target values as a numeric array with the same dimension order as the input data `dlX`.

`targets = ones(height,width,channels,observations);`

Compute the half mean squared error between the predictions and the targets.

`dlY = mse(dlX,targets)`
```dlY = 1x1 dlarray 5.2061```

## Input Arguments

collapse all

Predictions, specified as a `dlarray` with or without dimension labels or a numeric array. When `dlX` is not a formatted `dlarray`, you must specify the dimension label format using `'DataFormat',FMT`. If `dlX` is a numeric array, `targets` must be a `dlarray`.

Data Types: `single` | `double`

Target values, specified as a formatted or unformatted `dlarray` or a numeric array.

If `targets` is a formatted `dlarray`, its dimension format must be the same as the format of `X`, or the same as `'DataFormat'` if `X` is unformatted

If `targets` is an unformatted `dlarray` or a numeric array, the size of `targets` must exactly match the size of `X`. The format of `X` or the value of `'DataFormat'` is implicitly applied to `targets`.

Data Types: `single` | `double`

Dimension order of unformatted input data, specified as the comma-separated pair consisting of `'DataFormat'` and a character array or string `FMT` that provides a label for each dimension of the data. Each character in `FMT` must be one of the following:

• `'S'` — Spatial

• `'C'` — Channel

• `'B'` — Batch (for example, samples and observations)

• `'T'` — Time (for example, sequences)

• `'U'` — Unspecified

You can specify multiple dimensions labeled `'S'` or `'U'`. You can use the labels `'C'`, `'B'`, and `'T'` at most once.

You must specify `'DataFormat',FMT` when the input data `dlX` is not a formatted `dlarray`.

Example: `'DataFormat','SSCB'`

Data Types: `char` | `string`

## Output Arguments

collapse all

Half mean squared error loss, returned as a `dlarray` scalar without dimension labels. The output `dlY` has the same underlying data type as the input `dlX`.

collapse all

### Half Mean Squared Error Loss

The `mse` function computes the half-mean-squared-error loss for regression problems. For more information, see the definition of Regression Output Layer on the `RegressionOutputLayer` reference page.