represents a loss layer that computes the mean absolute loss between the "Input" port and "Target" port.
MeanAbsoluteLossLayer
represents a loss layer that computes the mean absolute loss between the "Input" port and "Target" port.
Details and Options
- MeanAbsoluteLossLayer 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 - MeanAbsoluteLossLayer[…][<|"Input" -> in, "Target"target|>] explicitly computes the output from applying the layer.
- MeanAbsoluteLossLayer[…][<|"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.
- MeanAbsoluteLossLayer is typically used inside NetGraph to construct a training network.
- A MeanAbsoluteLossLayer[…] can be provided as the third argument to NetTrain when training a specific network.
- MeanAbsoluteLossLayer["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[MeanAbsoluteLossLayer] gives the list of default options to construct the layer. Options[MeanAbsoluteLossLayer[…]] gives the list of default options to evaluate the layer on some data.
- Information[MeanAbsoluteLossLayer[…]] gives a report about the layer.
- Information[MeanAbsoluteLossLayer[…],prop] gives the value of the property prop of MeanAbsoluteLossLayer[…]. Possible properties are the same as for NetGraph.
Examples
open all close allBasic Examples (3)
Create a MeanAbsoluteLossLayer layer:
MeanAbsoluteLossLayer[]Create a MeanAbsoluteLossLayer that takes length-3 vectors:
loss = MeanAbsoluteLossLayer["Input" -> {3}]loss[<|"Input" -> {1, 2, 3}, "Target" -> {3, 2, 1}|>]Create a NetGraph containing a MeanAbsoluteLossLayer:
net = NetGraph[{ElementwiseLayer[Tanh], MeanAbsoluteLossLayer[]}, {1 -> NetPort[2, "Input"]}]net[<|"Input" -> {{1, 2}, {3, 5}, {2, 2}}, "Target" -> {{1, 3}, {5, 6}, {4, 2}}|>]Scope (4)
Arguments (1)
Create a MeanAbsoluteLossLayer:
loss = MeanAbsoluteLossLayer[]Apply the MeanAbsoluteLossLayer to a pair of matrices:
loss[<|"Input" -> {{1, 2, 3}, {4, 5, 6}}, "Target" -> {{1, 1, 0}, {0, 1, 1}}|>]Apply the MeanAbsoluteLossLayer to a pair of vectors:
loss[<|"Input" -> {1, 2, 3}, "Target" -> {1, 1, 0}|>]Apply the MeanAbsoluteLossLayer to a pair of numbers:
loss[<|"Input" -> 3, "Target" -> 2|>]Ports (3)
Create a MeanAbsoluteLossLayer that assumes the input data are vectors of length 2:
loss = MeanAbsoluteLossLayer["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 MeanAbsoluteLossLayer that takes two variable-length vectors:
loss = MeanAbsoluteLossLayer["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 MeanAbsoluteLossLayer that takes two images as input:
loss = MeanAbsoluteLossLayer["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 MeanAbsoluteLossLayer:
trained = NetTrain[LinearLayer[], {1 -> 1.9, 2 -> 4.1, 3 -> 6.0, 4 -> 8.1}, LossFunction -> MeanAbsoluteLossLayer[]]Predict the value of a new input:
trained[2.1]Properties & Relations (2)
MeanAbsoluteLossLayer computes:
meanAbsoluteLoss = N[Mean[Flatten[Abs[#Input - #Target]]]]&;Compare the output of the layer and the definition on an example:
data = <|"Input" -> {1, 2.23, 3}, "Target" -> {3, 2, 1}|>;
MeanAbsoluteLossLayer[][data]
meanAbsoluteLoss[data]MeanAbsoluteLossLayer effectively computes a normalized version of ManhattanDistance:
ManhattanDistance[{1, 2, 3}, {3, 2, 5}] / 3.MeanAbsoluteLossLayer[][<|"Input" -> {1, 2, 3}, "Target" -> {3, 2, 5}|>]See Also
MeanSquaredLossLayer CrossEntropyLossLayer NetGraph NetTrain ManhattanDistance
Function Repository: CosDiceLossLayer
Tech Notes
Related Guides
Text
Wolfram Research (2016), MeanAbsoluteLossLayer, Wolfram Language function, https://reference.wolfram.com/language/ref/MeanAbsoluteLossLayer.html (updated 2019).
CMS
Wolfram Language. 2016. "MeanAbsoluteLossLayer." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2019. https://reference.wolfram.com/language/ref/MeanAbsoluteLossLayer.html.
APA
Wolfram Language. (2016). MeanAbsoluteLossLayer. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/MeanAbsoluteLossLayer.html
BibTeX
@misc{reference.wolfram_2026_meanabsolutelosslayer, author="Wolfram Research", title="{MeanAbsoluteLossLayer}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/MeanAbsoluteLossLayer.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_meanabsolutelosslayer, organization={Wolfram Research}, title={MeanAbsoluteLossLayer}, year={2019}, url={https://reference.wolfram.com/language/ref/MeanAbsoluteLossLayer.html}, note=[Accessed: 12-June-2026]}