LossMechanism¶
Contents¶
LossMechanism_Example
Overview¶
A LossMechanism is a subclass of ComparatorMechanism that receives two inputs (a sample and a target), compares
them using its function, and places the calculated discrepancy between the two in its
OUTCOME OutputPort. It is used to compute the loss (error) signal for learning
in the forward method of an AutodiffComposition.
Creating a LossMechanism¶
LossMechanisms are created automatically when specified in the constructor of an AutodiffComposition (see LossMechanism and Specifying sample-target pairs).
Note
A LossMechanism cannot be added as a standalone Node in a Composition; it can only be added to in the targets argument of an AutodiffComposition; adding it in amy other way raises an error.
It is important to recognize that the value of the SAMPLE and TARGET InputPorts must have the same length and
type, so that they can be compared using the LossMechanism’s function. They use the format
of the OutputPorts (or those of the Mechanisms) specified in the sample and target arguments, respectively,
and the MappingProjection to each preserves those dimensions. Therefore, the OutputPorts (or Mechanisms) specified
in the sample and target arguments must have values of the same length and type. If the input_ports
argument is used, then both the SAMPLE and TARGET InputPorts must be specified. Any of the formats for
specifying InputPorts can be used in the argument, however the number and use of these
must conform to the format of the variable for function of
the LossMechanism (see Structure). If values are assigned for the InputPorts, they must be of equal
length and type. Their types must also be compatible with the value of the OutputPorts specified in the sample
and target arguments and, again,of the same length.
Structure¶
A LossMechanism has, by default, two input_ports, each of which receives a
MappingProjection from a corresponding OutputPort specified in the sample and target arguments of its
constructor. The InputPorts are listed in the Mechanism’s input_ports attribute
and named, respectively, SAMPLE and TARGET. The OutputPorts from which they receive their projections (specified
in the sample and target arguments) are listed in the Mechanism’s sample and
target attributes as well as in its monitor attribute.
The LossMechanism’s function compares the value of the sample and target InputPorts
to compute the loss. By default, it uses the loss_spec attribute of the
AutodiffComposition in which it is contained. However, other LOSS specifications or torch functions can be used.
The latter must be able to take the number of arrays with the same format as its inputs, and must detach the tensor(s) used as the target values in
computing the loss. The result is assigned as the value of the Loss Mechanism’s OUTCOME (primary) OutputPort, and used in the AutodiffComposition to compute the loss for learning.
Execution¶
When a LossMechanism is executed, it updates its input_ports with the values of the OutputPorts (or Mechanisms)
specified in its sample and target arguments, and then uses its function to compute
the loss, detaching the target value(s). The
result is assigned to the value of its OUTCOME output_port,
and as the first item of the Mechanism’s output_values attribute.
Class Reference¶
- class psyneulink.library.components.mechanisms.processing.objective.lossmechanism.LossMechanism(default_variable=None, sample=None, target=None, function=None, loss=None, output_ports=None, params=None, name=None, prefs=None, context=None, **kwargs)¶
Subclass of ComparatorMechanism that computes the loss (error) between a
sampleand atargetused for training in an AutodiffComposition.- Parameters:
sample (OutputPort, Mechanism, value, or string) – specifies the value for which to compute the
losswith respect to thetarget.target (OutputPort, Mechanism, value, or string) – specifies the value with respect to which the
lossis computed for thesample.input_ports (List[InputPort, value, str or dict] or Dict[] : default [SAMPLE, TARGET]) – specifies the names and/or formats to use for the values of the
sampleandtargetInputPorts; by default they are named SAMPLE and TARGET, and their formats match the value of the OutputPorts specified in the sample and target arguments, respectively (see Structure for additional details).loss (Loss or PyTorch loss function : default torch.nn.MSELoss(reduction='mean')) – specifies the
functionused to compute the loss forsamplewith respect to thetarget.function (function or method) –
specifies a function used to compute the loss for the
samplewith respect to thetarget. It can be any function that takes two arrays as input arguments (the sample and target values) and returns a scalar, including a torch.nn loss function. It must also detach the tensor used as the target values in computing the loss. If both loss and function are specified, an error is raisedIf neither is specified, the default is determined by theloss_specattribute of the AutodiffComposition in which the LossMechanism is contained.
- sample¶
determines the value for which the
lossis computed with respect to thetarget(see Sample for additional information).- Type:
- target¶
determines the value with respect to which the
lossis computed for thesample(seeAutodiffComposition_Target>for additional information).- Type:
- input_ports¶
contains the two InputPorts named, by default, SAMPLE and TARGET, each of which receives a MappingProjection from the OutputPorts referenced by the
sampleandtargetattributes (see Structure for additional details).- Type:
- loss¶
specifies the form of
Lossorfunctionused to compute the loss for thesamplewith respect to thetarget.- Type:
Loss or PyTorch loss function : default torch.nn.MSELoss(reduction=’mean’)
- function¶
used to compute the loss for the
samplewith respect to thetarget; determined either by the loss or function* argument to the constructor.- Type:
function or method
- output_port¶
contains the primary OutputPort of the LossMechanism; the default is its OUTCOME OutputPort, the value of which is equal to the
valueattribute of the LossMechanism.- Type:
- output_ports¶
contains, by default, only the OUTCOME (primary) OutputPort of the LossMechanism.
- Type:
- output_values¶
contains one item that is the value of the OUTCOME OutputPort.
- Type:
2d np.array