represents a loss layer that computes the mean squared loss between its "Input" port and "Target" port.
MeanSquaredLossLayer
represents a loss layer that computes the mean squared loss between its "Input" port and "Target" port.
Details and Options
- MeanSquaredLossLayer exposes the following ports for use in NetGraph etc.:
-
"Input" an array of arbitrary rank "Target" an array of the same rank as "Input" "Loss" a real number - MeanSquaredLossLayer[…][<|"Input"->in,"Target"target|>] explicitly computes the output from applying the layer.
- MeanSquaredLossLayer[…][<|"Input"->{in1,in2,…},"Target"->{target1,target2,…}|>] explicitly computes outputs for each of the ini and targeti.
- When given a NumericArray as input, the output will be a NumericArray.
- MeanSquaredLossLayer is typically used inside NetGraph to construct a training network.
- A MeanSquaredLossLayer[…] can be provided as the third argument to NetTrain when training a specific network.
- When appropriate, MeanSquaredLossLayer is automatically used by NetTrain if an explicit loss specification is not provided.
- MeanSquaredLossLayer["port"->shape] allows the shape of the given input "port" to be specified. Possible forms for shape include:
-
"Real" a single real number n a vector of length n {n1,n2,…} an array of dimensions n1×n2×… "Varying" a vector whose length is variable {"Varying",n2,n3,…} an array whose first dimension is variable and whose remaining dimensions are n2×n3×… NetEncoder[…] an encoder NetEncoder[{…,"Dimensions"{n1,…}}] an encoder mapped over an array of dimensions n1×… - Options[MeanSquaredLossLayer] gives the list of default options to construct the layer. Options[MeanSquaredLossLayer[…]] gives the list of default options to evaluate the layer on some data.
- Information[MeanSquaredLossLayer[…]] gives a report about the layer.
- Information[MeanSquaredLossLayer[…],prop] gives the value of the property prop of MeanSquaredLossLayer[…]. Possible properties are the same as for NetGraph.
Examples
open all close allBasic Examples (3)
Create a MeanSquaredLossLayer:
MeanSquaredLossLayer[]Create a MeanSquaredLossLayer that takes length-3 vectors:
loss = MeanSquaredLossLayer["Input" -> {3}]loss[<|"Input" -> {1, 2, 3}, "Target" -> {3, 2, 1}|>]Create a NetGraph containing a MeanSquaredLossLayer:
net = NetGraph[{ElementwiseLayer[Tanh], MeanSquaredLossLayer[]}, {1 -> NetPort[2, "Input"]}]net[<|"Input" -> {{1, 2}, {3, 5}, {2, 2}}, "Target" -> {{1, 3}, {5, 6}, {4, 2}}|>]Scope (4)
Create a MeanSquaredLossLayer:
loss = MeanSquaredLossLayer[]Apply the MeanSquaredLossLayer to a pair of matrices:
loss[<|"Input" -> {{1, 2, 3}, {4, 5, 6}}, "Target" -> {{1, 1, 0}, {0, 1, 1}}|>]Apply the MeanSquaredLossLayer to a pair of vectors:
loss[<|"Input" -> {1, 2, 3}, "Target" -> {1, 1, 0}|>]Apply the MeanSquaredLossLayer to a pair of numbers:
loss[<|"Input" -> 3, "Target" -> 2|>]Create a MeanSquaredLossLayer that assumes the input data are vectors of length 2:
loss = MeanSquaredLossLayer["Input" -> {2}]Thread the layer across a batch of inputs:
loss[<|"Input" -> {{1, 2}, {3, 5}, {2, 2}}, "Target" -> {{1, 3}, {5, 6}, {4, 2}}|>]Create a MeanSquaredLossLayer that takes two variable-length vectors:
loss = MeanSquaredLossLayer["Input" -> "Varying"]Apply the layer to an input and target vector:
loss[<|"Input" -> {1, 3, 5}, "Target" -> {2, 3, 4}|>]Thread the layer over a batch of input and target vectors:
loss[<|"Input" -> {{1, 3, 5}, {2, 2}}, "Target" -> {{2, 3, 4}, {2, 4}}|>]Create a MeanSquaredLossLayer that takes two images as input:
loss = MeanSquaredLossLayer["Input" -> NetEncoder["Image"], "Target" -> NetEncoder["Image"]]Apply the layer to two dissimilar images:
loss[<|"Input" -> [image], "Target" -> [image]|>]Apply the layer to two dissimilar images:
loss[<|"Input" -> [image], "Target" -> [image]|>]Applications (1)
Define a single-layer neural network that takes in scalar numeric values and produces scalar numeric values, and train this network using a MeanSquaredLossLayer:
trained = NetTrain[LinearLayer[], {1 -> 1.9, 2 -> 4.1, 3 -> 6.0, 4 -> 8.1}, LossFunction -> MeanSquaredLossLayer[]]Predict the value of a new input:
trained[2.1]Properties & Relations (2)
MeanSquaredLossLayer computes:
meanSquaredLoss = N[Mean[Flatten[(#Input - #Target) ^ 2]]]&;Compare the output of the layer and the definition on an example:
data = <|"Input" -> {1, 2, 3}, "Target" -> {3, 2, 1}|>;
MeanSquaredLossLayer[][data]
meanSquaredLoss[data]//NMeanSquaredLossLayer effectively computes a normalized version of SquaredEuclideanDistance:
SquaredEuclideanDistance[{1, 2, 3}, {3, 2, 5}] / 3.MeanSquaredLossLayer[][<|"Input" -> {1, 2, 3}, "Target" -> {3, 2, 5}|>]See Also
MeanAbsoluteLossLayer CrossEntropyLossLayer NetGraph NetTrain SquaredEuclideanDistance
Function Repository: CosDiceLossLayer
Tech Notes
Related Guides
Text
Wolfram Research (2016), MeanSquaredLossLayer, Wolfram Language function, https://reference.wolfram.com/language/ref/MeanSquaredLossLayer.html (updated 2019).
CMS
Wolfram Language. 2016. "MeanSquaredLossLayer." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2019. https://reference.wolfram.com/language/ref/MeanSquaredLossLayer.html.
APA
Wolfram Language. (2016). MeanSquaredLossLayer. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/MeanSquaredLossLayer.html
BibTeX
@misc{reference.wolfram_2026_meansquaredlosslayer, author="Wolfram Research", title="{MeanSquaredLossLayer}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/MeanSquaredLossLayer.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_meansquaredlosslayer, organization={Wolfram Research}, title={MeanSquaredLossLayer}, year={2019}, url={https://reference.wolfram.com/language/ref/MeanSquaredLossLayer.html}, note=[Accessed: 12-June-2026]}