• Github
Table of Contents
0.18.0.0+649
  • Welcome to PsyNeuLink
  • Basics and Primer
  • Quick Reference
  • Core
  • Library
  • Contributors Guide
  • Docs >
  • Library >
  • Components >
  • Mechanisms >
  • Processing Mechanisms >
  • Objective Mechanisms >
  • LossMechanism
Shortcuts

LossMechanism¶

Contents¶

  • Overview

  • Creating a LossMechanism

  • Structure

  • Execution

  • LossMechanism_Example

  • Class Reference

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 sample and a target used for training in an AutodiffComposition.

Parameters:
  • sample (OutputPort, Mechanism, value, or string) – specifies the value for which to compute the loss with respect to the target.

  • target (OutputPort, Mechanism, value, or string) – specifies the value with respect to which the loss is computed for the sample.

  • 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 sample and target InputPorts; 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 function used to compute the loss for sample with respect to the target.

  • function (function or method) –

    specifies a function used to compute the loss for the sample with respect to the target. 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 the loss_spec attribute of the AutodiffComposition in which the LossMechanism is contained.

sample¶

determines the value for which the loss is computed with respect to the target (see Sample for additional information).

Type:

OutputPort

target¶

determines the value with respect to which the loss is computed for the sample (see AutodiffComposition_Target> for additional information).

Type:

OutputPort

input_ports¶

contains the two InputPorts named, by default, SAMPLE and TARGET, each of which receives a MappingProjection from the OutputPorts referenced by the sample and target attributes (see Structure for additional details).

Type:

ContentAddressableList[InputPort, InputPort]

loss¶

specifies the form of Loss or function used to compute the loss for the sample with respect to the target.

Type:

Loss or PyTorch loss function : default torch.nn.MSELoss(reduction=’mean’)

function¶

used to compute the loss for the sample with respect to the target; 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 value attribute of the LossMechanism.

Type:

OutputPort

output_ports¶

contains, by default, only the OUTCOME (primary) OutputPort of the LossMechanism.

Type:

ContentAddressableList[OutputPort]

output_values¶

contains one item that is the value of the OUTCOME OutputPort.

Type:

2d np.array

Next Previous

© Copyright 2016, Jonathan D. Cohen.

Built with Sphinx using a theme provided by Read the Docs.
  • LossMechanism
    • Contents
    • Overview
    • Creating a LossMechanism
    • Structure
    • Execution
    • Class Reference
    • LossMechanism
      • LossMechanism.sample
      • LossMechanism.target
      • LossMechanism.input_ports
      • LossMechanism.loss
      • LossMechanism.function
      • LossMechanism.output_port
      • LossMechanism.output_ports
      • LossMechanism.output_values
  • Github