Main Content

l2loss

L2loss for regression tasks

    Description

    The L2loss operation computes the L2loss (based on the squared L2norm) given network predictions and target values. When theReductionoption is"sum"and theNormalizationFactoroption is"batch-size", the computed value is known as the mean squared error (MSE).

    Thel2lossfunction calculates the L2loss usingdlarraydata.Usingdlarrayobjects makes working with high dimensional data easier by allowing you to label the dimensions. For example, you can label which dimensions correspond to spatial, time, channel, and batch dimensions using the"S","T","C", and"B"labels, respectively. For unspecified and other dimensions, use the"U"label. Fordlarrayobject functions that operate over particular dimensions, you can specify the dimension labels by formatting thedlarrayobject directly, or by using theDataFormatoption.

    example

    loss= l2loss(Y,targets)computes MSE loss for the predictionsYand the target valuestargets. The inputYmust be a formatteddlarray. The outputlossis an unformatteddlarrayscalar.

    loss= l2loss(Y,targets,weights)computes the weighted L2loss using the weight valuesweights. The outputlossis an unformatteddlarrayscalar.

    loss= l2loss(___,DataFormat=FMT)computes the loss for the unformatteddlarrayobjectYand the target values with the format specified byFMT. Use this syntax with any of the input arguments in previous syntaxes.

    example

    loss= l2loss(___,Name=Value)specifies additional options using one or more name-value arguments. For example,l2loss(Y,targets,Reduction="none")computes the L2loss without reducing the output to a scalar.

    Examples

    collapse all

    Create an array of predictions for 12 observations over 10 responses.

    numResponses = 10; numObservations = 12; Y = rand(numResponses,numObservations); dlY = dlarray(Y,'CB');

    View the size and format of the predictions.

    size(dlY)
    ans =1×210 12
    dims(dlY)
    ans = 'CB'

    Create an array of random targets.

    targets = rand(numResponses,numObservations);

    View the size of the targets.

    size(targets)
    ans =1×210 12

    Compute the mean squared error (MSE) loss between the predictions and the targets using thel2lossfunction.

    loss = l2loss(dlY,targets)
    loss = 1x1 dlarray 1.4748

    Create arrays of predictions and targets for 12 sequences of varying lengths over 10 responses.

    numResponses = 10; numObservations = 12; maxSequenceLength = 15; sequenceLengths = randi(maxSequenceLength,[1 numObservations]); Y = cell(numObservations,1); targets = cell(numObservations,1);fori = 1:numObservations Y{i} = rand(numResponses,sequenceLengths(i)); targets{i} = rand(numResponses,sequenceLengths(i));end

    View the cell arrays of predictions and targets.

    Y
    Y=12×1 cell array{10x13 double} {10x14 double} {10x2 double} {10x14 double} {10x10 double} {10x2 double} {10x5 double} {10x9 double} {10x15 double} {10x15 double} {10x3 double} {10x15 double}
    targets
    targets=12×1 cell array{10x13 double} {10x14 double} {10x2 double} {10x14 double} {10x10 double} {10x2 double} {10x5 double} {10x9 double} {10x15 double} {10x15 double} {10x3 double} {10x15 double}

    Pad the prediction and target sequences in the second dimension using thepadsequencesfunction and also return the corresponding mask.

    [Y,mask] = padsequences(Y,2); targets = padsequences(targets,2);

    Convert the padded sequences todlarraywith the format"CTB"(channel, time, batch). Because formatteddlarrayobjects automatically permute the dimensions of the underlying data, keep the order consistent by also converting the targets and mask to formatteddlarrayobjects with the format"CTB"(channel, batch, time).

    dlY = dlarray(Y,"CTB"); targets = dlarray(targets,"CTB"); mask = dlarray(mask,"CTB");

    View the sizes of the prediction scores, targets, and mask.

    size(dlY)
    ans =1×310 12 15
    size(targets)
    ans =1×310 12 15
    size(mask)
    ans =1×310 12 15

    Compute the mean squared error (MSE) between the predictions and the targets. To prevent the loss values calculated from padding from contributing to the loss, set theMaskoption to the mask returned by thepadsequencesfunction.

    loss = l2loss(dlY,targets,Mask=mask)
    loss = 1x1 dlarray 16.3668

    Input Arguments

    collapse all

    Predictions, specified as a formatteddlarray, an unformatteddlarray或数字数组。WhenYis not a formatteddlarray, you must specify the dimension format using theDataFormatoption.

    IfYis a numeric array,targetsmust be adlarray.

    Target responses, specified as a formatted or unformatteddlarrayor a numeric array.

    的大小each dimension oftargetsmust match the size of the corresponding dimension ofY.

    Iftargetsis a formatteddlarray, then its format must be the same as the format ofY, or the same asDataFormatifYis unformatted.

    Iftargetsis an unformatteddlarrayor a numeric array, then the function applies the format ofYor the value ofDataFormattotargets.

    Tip

    Formatteddlarrayobjects automatically permute the dimensions of the underlying data to have order"S"(spatial),"C"(channel),"B"(batch),"T"(time), then"U"(unspecified). To ensure that the dimensions ofYandtargetsare consistent, whenYis a formatteddlarray, also specifytargetsas a formatteddlarray.

    Weights, specified as a formatted or unformatteddlarrayor a numeric array.

    Ifweightsis a vector andYhas two or more nonsingleton dimensions, thenweightsmust be a formatteddlarray, where the dimension label of the nonsingleton dimension is either"C"(channel) or"B"(batch) and has a size that matches the size of the corresponding dimension inY.

    Ifweightsis a formatteddlarraywith two or more nonsingleton dimensions, then its format must match the format ofY.

    Ifweightsis not a formatteddlarrayand has two or more nonsingleton dimensions, then its size must match the size ofYand the function uses the same format asY. Alternatively, to specify the weights format, use theWeightsFormatoption.

    Name-Value Arguments

    Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example:loss = l2loss(Y,targets,Reduction="none")specifies to compute the L2loss without reducing the output to a scalar

    Mask indicating which elements to include for loss computation, specified as adlarrayobject, a logical array, or a numeric array with the same size asY.

    The function includes and excludes elements of the input data for loss computation when the corresponding value in the mask is 1 and 0, respectively.

    IfMaskis a formatteddlarrayobject, then its format must match that ofY. IfMaskis not a formatteddlarrayobject, then the function uses the same format asY.

    If you specify theDataFormatoption, then the function also uses the specified format for the mask.

    的大小each dimension ofMaskmust match the size of the corresponding dimension inY. The default value is a logical array of ones.

    Tip

    Formatteddlarrayobjects automatically permute the dimensions of the underlying data to have this order:"S"(spatial),"C"(channel),"B"(batch),"T"(time), and"U"(unspecified). For example,dlarrayobjects automatically permute the dimensions of data with format"TSCSBS"to have format"SSSCBT".

    To ensure that the dimensions ofYand the mask are consistent, whenYis a formatteddlarray, also specify the mask as a formatteddlarray.

    Mode for reducing the array of loss values, specified as one of the following:

    • "sum"— Sum all of the elements in the array of loss values. In this case, the outputlossis scalar.

    • "none"— Do not reduce the array of loss values. In this case, the outputlossis an unformatteddlarrayobject with the same size asY.

    Divisor for normalizing the reduced loss whenReductionis"sum", specified as one of the following:

    • "batch-size"— Normalize the loss by dividing it by the number of observations inX.

    • "all-elements"— Normalize the loss by dividing it by the number of elements ofX.

    • "mask-included"——正常损失损失值除以by the number of included elements specified by the mask for each observation independently. To use this option, you must specify a mask using theMaskoption.

    • "none"— Do not normalize the loss.

    Dimension order of unformatted input data, specified as a character vector or string scalarFMTthat provides a label for each dimension of the data.

    When you specify the format of adlarrayobject, each character provides a label for each dimension of the data and must be one of the following:

    • "S"— Spatial

    • "C"— Channel

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

    • "T"— Time (for example, time steps of 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 specifyDataFormatwhen the input data is not a formatteddlarray.

    Data Types:char|string

    Dimension order of the weights, specified as a character vector or string scalar that provides a label for each dimension of the weights.

    When you specify the format of adlarrayobject, each character provides a label for each dimension of the data and must be one of the following:

    • "S"— Spatial

    • "C"— Channel

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

    • "T"— Time (for example, time steps of 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 specifyWeightsFormatwhenweightsis a numeric vector andYhas two or more nonsingleton dimensions.

    Ifweightsis not a vector, or bothweightsandYare vectors, then default value ofWeightsFormatis the same as the format ofY.

    Data Types:char|string

    Output Arguments

    collapse all

    L2loss, returned as an unformatteddlarray. The outputlossis an unformatteddlarraywith the same underlying data type as the inputY.

    的大小lossdepends on theReductionoption.

    Algorithms

    collapse all

    L2 Loss

    The L2loss operation computes the L2loss (based on the squared L2norm) given network predictions and target values. When theReductionoption is"sum"and theNormalizationFactoroption is"batch-size", the computed value is known as the mean squared error (MSE).

    For each elementYjof the input, thel2lossfunction computes the corresponding element-wise loss values using

    loss j = ( Y j T j ) 2 ,

    whereYjis a predicted value andTjis the corresponding target value.

    减少loss values to a scalar, the function then reduces the element-wise loss using the formula

    loss = 1 N j m j w j loss j ,

    whereNis the normalization factor,mjis the mask value for elementj, andwjis the weight value for elementj.

    If you do not opt to reduce the loss, then the function applies the mask and the weights to the loss values directly:

    loss j * = m j w j loss j

    Extended Capabilities

    Version History

    Introduced in R2021b