mse - Half mean squared error - MATLAB (original) (raw)
Syntax
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
where Xi is the network prediction, 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
l2loss is recommended over mse
because it provides control for weights, masking, and normalization.
To train a network using the trainnet function with mean square error loss, set the loss function to"mse"
.
[loss](#mw%5F2d181a47-d2dc-44a7-9382-970cf017ee55) = mse([Y](#mw%5F6ade87c5-4448-4c45-a289-6859ffcc1630%5Fsep%5Fmw%5F72d38514-0d45-4bcf-9259-997edf0cb0c8),[targets](#mw%5F6ade87c5-4448-4c45-a289-6859ffcc1630%5Fsep%5Fmw%5F7d699de8-af5e-4d74-8756-c0027247b11e))
computes the half mean squared error loss between the predictions Y
and the target values targets
for regression problems. The inputY
must be a formatted dlarray
. The outputloss
is an unformatted dlarray
scalar.
[loss](#mw%5F2d181a47-d2dc-44a7-9382-970cf017ee55) = mse([Y](#mw%5F6ade87c5-4448-4c45-a289-6859ffcc1630%5Fsep%5Fmw%5F72d38514-0d45-4bcf-9259-997edf0cb0c8),[targets](#mw%5F6ade87c5-4448-4c45-a289-6859ffcc1630%5Fsep%5Fmw%5F7d699de8-af5e-4d74-8756-c0027247b11e),'DataFormat',[FMT](#mw%5F6ade87c5-4448-4c45-a289-6859ffcc1630%5Fsep%5Fmw%5F30d6528f-469b-47fb-a774-e80032eca842))
also specifies the dimension format FMT
when Y
is not a formatted dlarray
.
Examples
Find Half Mean Squared Error Between Predicted and Target Values
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;
Y = rand(height,width,channels,observations); Y = dlarray(Y,'SSCB')
Create the target values as a numeric array with the same dimension order as the input data Y
.
targets = ones(height,width,channels,observations);
Compute the half mean squared error between the predictions and the targets.
loss =
1x1 dlarray
5.2061
Input Arguments
Y
— Predictions
dlarray
object | numeric array
Predictions, specified as a formatted or unformatted dlarray
object, or a numeric array. When Y
is not a formatteddlarray
, you must specify the dimension format using theDataFormat
argument.
If Y
is a numeric array, targets must be adlarray
object.
targets
— Target responses
dlarray
| numeric array
Target responses, specified as a formatted or unformatted dlarray
or a numeric array.
The size of each dimension of targets
must match the size of the corresponding dimension of Y.
If targets
is a formatted dlarray
, then its format must be the same as the format of Y
, or the same asDataFormat
if Y
is unformatted.
If targets
is an unformatted dlarray
or a numeric array, then the function applies the format of Y
or the value ofDataFormat
to targets
.
Tip
Formatted dlarray
objects automatically permute the dimensions of the underlying data to have the order "S"
(spatial), "C"
(channel), "B"
(batch), "T"
(time), then"U"
(unspecified). To ensure that the dimensions ofY
and targets
are consistent, whenY
is a formatted dlarray
, also specifytargets
as a formatted dlarray
.
FMT
— Description of data dimensions
character vector | string scalar
Description of the data dimensions, specified as a character vector or string scalar.
A data format is a string of characters, where each character describes the type of the corresponding data dimension.
The characters are:
"S"
— Spatial"C"
— Channel"B"
— Batch"T"
— Time"U"
— Unspecified
For example, consider an array containing a batch of sequences where the first, second, and third dimensions correspond to channels, observations, and time steps, respectively. You can specify that this array has the format "CBT"
(channel, batch, time).
You can specify multiple dimensions labeled "S"
or "U"
. You can use the labels "C"
, "B"
, and"T"
once each, at most. The software ignores singleton trailing"U"
dimensions after the second dimension.
If the input data is not a formatted dlarray
object, then you must specify the FMT
option.
For more information, see Deep Learning Data Formats.
Data Types: char
| string
Output Arguments
loss
— Half mean squared error loss
dlarray
scalar
Half mean squared error loss, returned as an unformatted dlarray
scalar. The output loss
has the same underlying data type as the input Y
.
Algorithms
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.
Deep Learning Array Formats
Most deep learning networks and functions operate on different dimensions of the input data in different ways.
For example, an LSTM operation iterates over the time dimension of the input data, and a batch normalization operation normalizes over the batch dimension of the input data.
To provide input data with labeled dimensions or input data with additional layout information, you can use data formats.
A data format is a string of characters, where each character describes the type of the corresponding data dimension.
The characters are:
"S"
— Spatial"C"
— Channel"B"
— Batch"T"
— Time"U"
— Unspecified
For example, consider an array containing a batch of sequences where the first, second, and third dimensions correspond to channels, observations, and time steps, respectively. You can specify that this array has the format "CBT"
(channel, batch, time).
To create formatted input data, create a dlarray object and specify the format using the second argument.
To provide additional layout information with unformatted data, specify the format using the FMT argument.
For more information, see Deep Learning Data Formats.
Extended Capabilities
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The mse
function supports GPU array input with these usage notes and limitations:
- When at least one of the following input arguments is a
gpuArray
or adlarray
with underlying data of typegpuArray
, this function runs on the GPU:Y
targets
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2019b