LearningMechanism¶
Contents¶
- Creating a LearningMechanism
LearningMechanism_Automatic_Creation
Overview¶
A LearningMechanism is a ModulatoryMechanism that modifies the matrix
parameter of one or more MappingProjections. Its function takes one or more error_signals (usually the output of a ComparatorMechanism or one or more other
LearningMechanisms), as well as information about the MappingProjection(s) and activity that generated the error(s), and calculates a learning_signal
that is used to modify the MappingProjection(s) by way of its
LearningProjection(s). Typically, a LearningMechanism is used to “train” a single
MappingProjection (its primary_learned_projection
), using the output of the Mechanism to which that
MappingProjection projects (its output_source
) as the source of the error it attempts to reduce. A
LearningMechanism can be used to train multiple MappingProjections, by assigning it additional LearningProjections; however, these will all use the same learning_signal
, generated by the primary_learned_projection
and its associated output_source
.
All of the MappingProjection(s) modified by a LearningMechanism must project from one ProcessingMechanism to another in the same Composition. The learning components of a Composition can be
displayed using the Composition’s show_graph`show_graph
method with its
show_learning argument assigned True
or ALL.
A Note about the Implementation of Learning¶
The implementation of learning in PsyNeuLink was designed for exposition rather than efficiency. Unlike its
implementation in most other environments – where the learning algorithm is tightly integrated with the
elements of processing that it modifies – PsyNeuLink separates it into three constituent components, often including:
an ObjectiveMechanism used to evaluate the most proximal source of error; a LearningMechanism that uses that error
(or one derived from it by another LearningMechanism) to calculate a learning signal; and one or more
LearningProjections that use that learning signal to modify the weight matrix
of the MappingProjection(s) being learned. This has the advantage
of isolating and exposing the constituent computations, making it clearer what these are and how they operate, and
also making each individually accessible for reconfiguration. However, it comes at the cost of efficiency. For
efficient execution of supervised forms of learning (e.g., reinforcement learning and backpropagation),
an AutodiffComposition can be used, which allows the model to be specified using PsyNeuLink, but actually executes
learning either in a compiled form or using PyTorch
(see Compilation for additional details).
Creating a LearningMechanism¶
A LearningMechanism can be created in any of the ways used to create Mechanisms. More commonly, however, LearningMechanisms are created automatically.
Automatic Creation¶
A LearningMechanism is created automatically when a Composition’s learning method is
called. In that case, a LearningSignal, LearningProjection, a ComparatorMechanism (in the case of supervised
learning
), and any additional Projections required to implement learning that do not already exist are also
instantiated. This is described below, under Learning Configurations.
A LearningMechanism is also created automatically when either the tuple specification is used to specify learning for a MappingProjection, or a
LearningProjection is created without specifying its sender
attribute. However, this
is not advised, and should only used in special circumstances, as properly configuring learning generally requires
the instantiation of several other closely related Components, as described below.
Explicit Creation¶
If a LearningMechanism is created explicitly (using its constructor), then its variable and error_sources
arguments must be specified. The variable must have at leaset three items that are compatible (in number and type)
with the value
of the LearningMechanism’s InputPorts. Each
item in error_sources must be one of the following: a ComparatorMechanism, for single layer learning or for the last MappingProjection in a learning Pathway for multilayer learning; or a
LearningMechanism.
When a LearningMechanism is created explicitly, it can also be assigned existing LearningSignals and/or specified to create these, as well as LearningProjections from these to specified MappingProjections. These are specified in the learning_signals argument of the LearningMechanism’s constructor, using any of the forms allowed for specifying a LearningSignal.
Structure¶
A LearningMechanism has three types of InputPorts, a learning function
,
and two types of OutputPorts. These are used, respectively, to receive, compute, and transmit the
information needed to modify the MappingProjection(s) for which the LearningMechanism is responsible. In addition,
it has several attributes that govern and provide access to its operation. These are described below.
InputPorts¶
These receive the information required by the LearningMechanism’s function
. They are
listed in the LearningMechanism’s input_ports
attribute. They have the following
names and roles (shown in the figure
below):
ACTIVATION_INPUT - receives the value of the input to the
primary_learned_projection
; that is, thevalue
of that MappingProjection’ssender
. The value is assigned as the first item of the LearningMechanism’svariable
attribute.
ACTIVATION_OUTPUT - receives the value of the LearningMechanism’s
output_source
; that is, thevalue
of the OutputPort of the ProcessingMechanism to which theprimary_learned_projection
projects. By default, theoutput_source
’s primary OutputPort is used. However, a different OutputPort can be designated in the constructor for theoutput_source
, by assigning a parameter specification dictionary to the params argument of its constructor, with an entry that uses MONITOR_FOR_LEARNING as its key and a list containing the desired OutputPort(s) as its value. Thevalue
of the ACTIVATION_OUTPUT InputPort is assigned as the second item of the LearningMechanism’svariable
attribute.
ERROR_SIGNAL - this receives the
value
from the OUTCOME OutputPort of a ComparatorMechanism, or of the ERROR_SIGNAL OutputPort of another LearningMechanisms. If theprimary_learned_projection
projects to theTERMINAL
Mechanism of the Composition to which it belongs, or is not part of a multilayer learning sequence, then the LearningMechanism has a single ERROR_SIGNAL InputPort, that receives its input from a ComparatorMechanism. If theprimary_learned_projection
is part of a multilayer learning pathway, then the LearningMechanism will have one or more ERROR_SIGNAL InputPorts, that receive their input from the next LearningMechanism(s) in the sequence; that is, the one(s) associated with theefferents
(outgoing Projections) of itsoutput_source
, with one ERROR_SIGNAL InputPort for each of those Projections. Thevalue
s of the ERROR_SIGNAL InputPorts are summed by the LearningMechanism’sfunction
to calculate thelearning_signal
(see below); note that the value of the ERROR_SIGNAL InputPort may not be the same as that of the LearningMechanism’serror_signal
attribute or ERROR_SIGNAL OutputPort (see note below). If a LearningMechanism has more than one ERROR_SIGNAL InputPort, their names are suffixed with a hyphenated index, that is incremented for each additional InputPort (e.g.,error_signal-1
,error_signal-2
, etc.). These are listed in the LearningMechanism’serror_signal_input_ports
attribute, and thevalue
of each is assigned as an item of the LearningMechanism’svariable
attribute, beginning with its third item (i.e., following thevalue
of the ACTIVATION_INPUT and ACTIVATION_VALUE InputPorts).
COVARIATES - one or more InputPorts that receive the value of the inputs to
output_source (i.e., the *ProcessingMechanism* to which the `primary_learned_projection
projects) other than primary_learned_projection, if theoutput_source
’sfunction
takes more than one argument and they affect its derivative. These are assigned as a list to the fourth item in the LearningMechanism’svariable
attribute, and are used to calculate thelearning_signal
(see below).Note
The LearningMechanism has a separate InputPort for each covariate, however their values are combined into a single item of the LearningMechanism’s
variable
that is provided to itsfunction
at the fourth position.Although the COVARIATES InputPorts receive Projections from the same senders as the corresponding Projections to the
output_source
, these are for display only, and are not to actually compute the LearningSignal, since they are not subject to learning and therefore do not provide the same value as the actual Projections they shadow. Rather, those values are accessed directly by the LearningMechanism from the InputPorts of theoutput_source
, in a manner similar to its access of theerror_matrices
from theParameterPort`s of the `error_sources
.
The Mechanisms from the which the value
s above are received are listed in the
LearningMechanism’s input_source
, output_source
,
and error_sources
attributes, respectively (see
Additional Attributes for additional details).
Learning Function¶
The function
of a LearningMechanism uses the values received by the Mechanism’s
InputPorts (described above) to calculate the value of its learning_signal
and error_signal
attributes.
learning_signal
- the set of changes to thematrix
parameter of the MappingProjections being learned, calculated to reduce the summed value of the LearningMechanism’s ERROR_SIGNAL InputPort(s).
error_signal
- the contribution made by theprimary_learned_projection
to the error_signal(s) received by the LearningMechanism’s ERROR_SIGNAL InputPort(s). It is used by the LearningMechanism’sfunction
to calculate thelearning_signal
. Depending upon the context and specific LearningFunction used, it may also take account of thevalue
of itsoutput_source
, as well as thematrix
parameter of any of theoutput_source
’s outgoing Projections that are also being learned (these are listed in the LearningMechanism’serror_matrices
attribute). The value of theerror_signal
is assigned as the value of the LearningMechanism’s ERROR_SIGNAL OutputPort.Note
A LearningMechanism’s ERROR_SIGNAL InputPort(s) and its ERROR_SIGNAL OutputPort may not have the same value. The former are the error signal(s) received from a ComparatorMechanism or one or more LearningMechanisms, while the latter is the contribution made to those errors by the
primary_learned_projection
and theoutput_source
, as calculated by the LearningMechanism’sfunction
(see error_signal above).
The default function
of a LearningMechanism is BackPropagation
(also known as the
Generalized Delta Rule; see
Rumelhart et al., 1986). However, it can be
assigned any other PsyNeuLink LearningFunction, or any other Python function that takes as its
input a list or np.array containing three lists or 1d np.arrays of numeric values, and returns two lists or 1d
np.arrays. The two values it returns are assigned to the LearningMechanism’s learning_signal
and error_signal
attributes, respectively,
as well as to its two OutputPorts, as described below.
OutputPorts¶
By default, a LearningMechanism has two OutputPorts, the first of which is named ERROR_SIGNAL and
is assigned the value of the error_signal
returned by the LearningMechanism’s
function
, and the second of which is a LearningSignal and is assigned the value of the
learning_signal
returned by the function
.
They are each described below:
ERROR_SIGNAL - this is the primary OutputPort of a LearningMechanism, and receives the value of the
error_signal
used to calculate thelearning_signal
. Its value is assigned as the first item of the LearningMechanism’soutput_values
attribute. If the LearningMechanism is part of a multilayer learning pathway, the ERROR_SIGNAL OutputPort is assigned a Projection to the LearningMechanism for the preceding MappingProjection in the sequence being learned - see figure below). Note that thevalue
of the ERROR_SIGNAL OutputPort may not be the same as that of the LearningMechanism’s ERROR_SIGNAL InputPorts (see error_signal).
LearningSignal(s) - by default, a LearningMechanism has a single LearningSignal, which is a special type of OutputPort that receives the
learning_signal
generated by the LearningMechanism’sfunction
, and used to modify thematrix
parameter of theprimary_learned_projection
. The LearningSignal is assigned as the second item in the list of the LearningMechanism’s OutputPorts (i.e., of itsoutput_ports
attribute), and itsvalue
is assigned as the second item of the LearningMechanism’soutput_values
attribute.
Multiple LearningSignals and LearningProjections. Though not common, it is possible for a LearningMechanism to be assigned more than one LearningSignal, and/or more than one LearningProjection to its LearningSignal(s). This allows multiple MappingProjections to be trained by a single LearningMechanism. Note, however, that all of the LearningSignals of a LearningMechanism (and therefore its LearningProjections) use the same
learning_signal
, that is calculated based on the LearningMechanism’sprimary_learned_projection
and its associatedoutput_source
. This can be useful in some settings, such as for certain forms of convolutional neural networks.If all of the LearningProjections are used to implement the same form of modulation, (determined by their LearningSignals’
modulation
attribute), then they should be assigned to a single LearningSignal. Multiple LearningProjections can be assigned to a LearningSignal by specifying them in the projections argument of its constructor, or the PROJECTIONS entry of a dictionary assigned to its params argument); however, thematrix
parameter for the MappingProjection to which they project must have the same shape for all of them. If different forms of modulation are required to train different MappingProjections, then multiple LearningSignals should be assigned to the LearningMechanism, each specified for one of the required types of modulation, and then LearningProjections assigned to them for the corresponding MappingProjections. Multiple LearningSignals can be specified for a LearningMechanism by including them in a list assigned to the learning_signals argument of the LearningMechanism’s constructor.All of the LearningSignals of a LearningMechanism are listed in its
learning_signals
attribute. Because these are OutputPorts, they are also listed in theoutput_ports
attribute, after the ERROR_SIGNAL OutputPort. All of the LearningMechanism’s LearningProjections (that is, those belonging to all of its LearningSignals) are listed in itslearning_projections
attribute.
Additional Attributes¶
In addition to its InputPorts, function and OutputPorts, a LearningMechanism has the following attributes that refer to the Components being learned and/or its operation:
primary_learned_projection
- the MappingProjection with thematrix
parameter for which thelearning_signal
is computed. This is always the first Projection listed first in LearningMechanism’slearned_projections
attribute.
learned_projections
- a list of all of the MappingProjections to which the LearningMechanism sends a LearningProjection, listed in the order of the LearningSignal(s) to which they belong, as those are listed in the LearningMechanism’slearning_signals
attribute.
learning_enabled
- determines whether and when the LearningMechanism’slearning_projections
are executed (seelearning_enabled
for additional details).
input_source
- the Mechanism that sends theprimary_learned_projection
, and projects to the LearningMechanism’s ACTIVATION_INPUT InputPort.
output_source
- the Mechanism that receives theprimary_learned_projection
, and provides the input to the LearningMechanism’s ACTIVATION_OUTPUT InputPort.
error_sources
- a ComparatorMechanism, LearningMechanism, or list of them that calculate the error signal(s) provided to the LearningMechanism’s ERROR_SIGNAL(s) InputPort(s).
error_matrices
- thematrix
parameters of the Projections associated with theerror_sources
; that is, of any of theoutput_source
’sefferents
that are also being learned.
covariates_sources
- theInputPort`s of `Mechanism`(s) that provide covariates used in calculating the derivative of the `output_source
’sfunction
(seeabove `LearningMechanism_Covariates
), and project to its COVARIATES InputPort.
modulation
- the default value used for themodulation
attribute of LearningMechanism’s LearningSignals (i.e. those for which it is not explicitly specified). This determines the way in which thelearning_signal
is used to modify thematrix
parameter of thelearned_projections
. By default its value is Modulation.ADD, which causes the weight changes in thelearning_signal
to be added to the current value of thematrix
parameter (see Execution for a description of how the modifications are executed).
learning_rate
- specifies thelearning_rate
parameter used by the LearningMechanism’sfunction
which, for most forms of learning, uses it to multiply the weight change matrix before returning it as thelearning_signal
. The value can be specified in the learning_rate argument of the LearningMechanism’s constructor, or in the constructor for itsfunction
. If both are specified, the specification for the function takes precedence; in either case, the value of the learning_rate parameter is always the same for the LearningMechanism and its function. If neither is specified, then it inherits its value from any specifications made for the Composition or a learning Pathway of the Composition to which it belongs (see Learning Rate for additional details). However, after the Composition and/or its learning pathways have been constructed, specifying thelearning_rate
for a LearningMechanism (or its function) overrides any specifications made for the Composition or its learning pathways, including in calls the Composition’slearn
method. In this way, individual LearningMechanisms can be assigned specific learning rates that apply whenever those are executed. If the learning_rate is not explicitly specified anywhere, the default value for the LearningMechanism’sfunction
is used.
Learning Configurations¶
When learning is enabled for a RecurrentTransferMechanism (for unsupervised
learning) or using the learning method of a
Composition, all of the Components required for learning are created automatically. The types of Components that are
generated depend on the type of learning specified and the configuration of the Composition, as
described below. All of the learning Components of a Composition can be displayed using its show_graph
<ShowGraph.show_graph
method with the show_learning argument assigned True
or ALL.
Single layer learning¶
This configuration occurs when unsupervised learning is used, or supervised learning is used for a pathway in a Composition with only two Mechanisms (i.e., for the Projection between them). In this case, a single ComparatorMechanism and LearningMechanism are created (if they do not already exist) as well as the following MappingProjections:
from an OutputPort of the LearningMechanism’s
output_source
to the ComparatorMechanism’s SAMPLE InputPort. By default, the primary OutputPort of theoutput_source
is used; however, this can be modified by specifying its MONITOR_FOR_LEARNING parameter (see above).
from the TARGET_MECHANISM in the Composition to the ComparatorMechanism’s TARGET InputPort;
from the ComparatorMechanism’s OUTCOME OutputPort to the LearningMechanism’s ERROR_SIGNAL InputPort.
In addition, a LearningProjection is created from the LearningSignal for the
primary_learned_projection
to the ParameterPort for the matrix
of the
primary_learned_projection
. Because this configuration involves only a single layer of learning, no Projection
is created or assigned to the LearningMechanism’s ERROR_SIGNAL OutputPort.
Components for Single Layer Learning
Multilayer learning¶
This refers to learning in a pathway that has three or more Mechanisms in a sequence, and therefore two or more
MappingProjections that are being learned. The learning components for such a configuration are created
automatically when a learning method of a Composition is used that supports
multilayer learning (at present, this is only add_backpropagation_learning_pathway
). If the learning components
are constructed explicitly for a multilayer learning configuration,
then each LearningMechanism must use a LearningFunction that can calculate the influence
that each MappingProjection and its output have on the error that the LearningMechanism receives from the previous
one(s) in the sequence (e.g., BackPropagation
). Furthermore, the construction of the other learning-related
Components associated with each LearningMechanism depend on the position of its
primary_learned_projection
and output_source
in the sequence. If it is the
last one in the sequence, it is treated in the same way the LearningMechanism in a single layer learning configuration
configuration. This is the case if the output_source
is a standalone
Mechanism or the TERMINAL
Mechanism of a Composition. In these cases, as for single layer learning, a
ComparatorMechanism is created that receives the output of the output_source
as well as the target for learning
(see TARGET Mechanisms below), and projects to a LearningMechanism that is created for
the primary_learned_projection
. For all other MappingProjections being learned in the
sequence, the following additional MappingProjections are created for learning (shown in the figure below):
from the
input_source
to the LearningMechanism’s ACTIVATION_INPUT InputPort;
from the
output_source
to the LearningMechanism’s ACTIVATION_OUTPUT InputPort;
from the ERROR_SIGNAL OutputPort of each of the LearningMechanism’s
error_sources
to each of its corresponding ERROR_SIGNAL InputPort(s);from the
covariates_source
to the LearningMechanism’s COVARIATESInputPort`(s) if the `function
of theoutput_source
takes more than one argument and they affect its derivative (see above).
In addition, a LearningProjection is created from the LearningSignal for the
primary_learned_projection
of each LearningMechanism in the sequence, to the ParameterPort for the matrix
of the primary_learned_projection
. If the primary_learned_projection
is the first in
the sequence, then no Projection is created or assigned to its LearningMechanism’s ERROR_SIGNAL OutputPort.
Components for Multilayer Learning
TARGET
and OBJECTIVE
Mechanisms**. When supervised learning is
implemented using one of a Composition’s learning methods, it automatically creates a
number of learning-related Components. This includes a TARGET_MECHANISM
, that
receives the target stimulus specifed in the inputs argument of the Composition’s learn
method; and a OBJECTIVE_MECHANISM
, that computes the error_signal for the sequence. The output of the
OBJECTIVE_MECHANISM
is the error_source
for the last MappingProjection in the
learning pathway. If a multilayer learning pathway is implemented explicitly, it must include these Components.
Execution¶
LearningMechanisms that implement unsupervised learning
(AutoassociativeLearningMechanisms) execute when the RecurrentTransferMechanism
with which they are associated executes. LearningMechanisms that are part of a supervised learning pathway execute after all of the ProcessingMechanisms in the Composition to which they
belong have executed. When a LearningMechanism is executed, it uses the value
to calculate changes to the weights of the
matrix
parameter of its primary_learned_projection
and any of its other learned_projections
. Those weight changes are assigned as the LearningMechanism’s
learning_signal
attribute, the value
of each of its
LearningSignals, and as the value
of each of their
LearningProjections. That value is used, in turn, to modify the value
of the MATRIX
ParameterPort of each of the MappingProjections being learned (listed in the LearningMechanism’s
learned_projections
attribute).
Each ParameterPort uses the value it receives from the LearningProjection that projects to it to modify the
parameter of its function
, in a manner specified by the modulation
attribute of the LearningSignal from which it receives the LearningProjection (see
Modulation for a description of how modulation operates). By default, the
modulation
attribute of a LearningSignal is ADDITIVE
, the function
of a MATRIX ParameterPort for a MappingProjection is AccumulatorIntegrator
,
and the parameter it uses for additive modulation is its increment
parameter.
These assignments cause the value of the LearningProjection to be added to the previous value of the MATRIX
ParameterPort, thus incrementing the weights by an amount specified by the LearningMechanism’s learning_signal
. Note, that the changes to the matrix
parameter itself do not take effect until the next time the
learned_projection
is executed (see Lazy Evaluation for an explanation of
“lazy” updating).
A LearningMechanism’s function
also computes an error_signal
that is assigned as the value
of its ERROR_SIGNAL
OutputPort; in a multilayer learning configuration, that value is provided to the ERROR_SIGNAL InputPort(s) of the LearningMechanism(s) for the preceding MappingProjection(s)
being learned in the sequence.
Class Reference¶
- psyneulink.core.components.mechanisms.modulatory.learning.learningmechanism.DefaultTrainingMechanism¶
alias of
psyneulink.core.components.mechanisms.processing.objectivemechanism.ObjectiveMechanism
- class psyneulink.core.components.mechanisms.modulatory.learning.learningmechanism.LearningMechanism(default_variable=None, input_shapes=None, covariates_sources=None, error_sources=None, function=None, learning_signals=None, output_ports=None, modulation=None, learning_rate=None, learning_enabled=None, in_composition=False, params=None, name=None, prefs=None, **kwargs)¶
Subclass of ModulatoryMechanism that modifies the
matrix
parameter of a MappingProjection. See Mechanism for additional arguments and attributes.- Parameters
variable (List or 2d np.array) – it must have three items that correspond to the three values required by the LearningMechanism’s
function
; they must each be compatible (in number and type) with thevalue
of the corresponding InputPort (seevariable
for additional details).covariates_sources (InputPort or list of them : default None) – specifies the
InputPort`(s) of the LearningMechanism's `output_source
other than the one to which theprimary_learned_projection
projects. (seeLearningMechanism_Covariates
for additional details).error_sources (ComparatorMechanism, LearningMechanism, OutputPort or list of them) – specifies the source(s) of the error signal(s) used by the LearningMechanism’s
function
. Each must be a ComparatorMechanism for single layer learning, or for the last MappingProjection in a learning pathway in multilayer learning; otherwise they must be a LearningMechanism or the ERROR_SIGNAL OutputPort of one.function (LearningFunction or function : default BackPropagation) – specifies the function used to calculate the LearningMechanism’s
learning_signal
anderror_signal
attributes. It’svariable
must have three items, each of which must be a list or 1d array of numeric values, corresponding to values provided by the LearningMechanism’s ACTIVATION_INPUT, ACTIVATION_OUTPUT, and ERROR_SOURCES InputPorts, respectively (seeLearningMechanism_InputPorts `LearningMechanism_Function
and InputPorts for additional details). If supports an activation function that takes more than one argument that impact its derivative, then it must also accept a potential fourth keyword argument (covariates
), that is a list or 1d array of numeric values provided by the LearningMechanism’s COVARIATES InputPort(s) (seeLearningMechanism_Covariates
for additional details).learning_rate (float : default None) – specifies the learning rate for the LearningMechanism (see
learning_rate
for details).learning_signals (List[parameter of Projection, ParameterPort, Projection, tuple[str, Projection] or dict] :) – default LEARNING_SIGNAL specifies the parameter(s) to be learned (see
learning_signals
for details).modulation (str : default ADDITIVE) – specifies the default form of modulation used by the LearningMechanism’s LearningSignals, unless they are individually specified.
learning_enabled (bool or Enum[ONLINE|AFTER] : True) – specifies whether and when the LearningMechanism’s LearningProjections are executed (see
learning_enabled
for additional details).
- variable¶
has three items that serve as the template for the three inputs required by the LearningMechanism’s
function
(corresponding to its three InputPorts: the input to theprimary_learned_projection
(frominput_source
), the output of the Mechanism to which that projects (i.e., ofoutput_source
); and the error signal (fromLearningMechanism.error_sources
).- Type
2d np.array
- input_ports¶
list containing the LearningMechanism’s three InputPorts: ACTIVATION_INPUT, ACTIVATION_OUTPUT, ERROR_SIGNAL, and possible its COVARIATES.
- Type
ContentAddressableList[OutputPort]
- error_signal_input_ports¶
list of InputPorts that receive error_signals from the LearningMechanism’s
error_sources
.- Type
list[InputPorts]
- input_source¶
the Mechanism that sends the
primary_learned_projection
, and projects to the LearningMechanism’s ACTIVATION_INPUT InputPort.- Type
- output_source¶
the Mechanism that receives the
primary_learned_projection
, and projects to the LearningMechanism’s ACTIVATION_OUTPUT InputPort.- Type
- covariates_sources¶
the
InputPort`(s) of the LearningMechanism's `output_source
other than the one to which theprimary_learned_projection
projects; these are used as covariates in the calculation of the derivative ofoutput_source
'sfunction
with respect tovalue
of theprimary_learned_projection
(seeLearningMechanism_Covariates
for additional details).- Type
List[InputPort]
- covariates_values¶
the values of the InputPorts to which the
covariates_sources
project; passed to the LearningMechanism’sfunction
as the COVARIATES item of itsvariable
, and assigned as thevalue
of the LearningMechanism’s COVARIATES InputPorts.- Type
List[1d np.array]
- error_sources¶
the Mechanism(s) that calculate the error signal(s) provided to the LearningMechanism’s ERROR_SIGNAL(s) InputPort(s).
- Type
list[ComparatorMechanism or LearningMechanism]
- error_matrices¶
the matrices of the Projections associated with the
error_sources
, (i.e., for the next Projection(s) in the learning_sequence, or to the ComparatorMechanism); note: these are not for the LearningMechanism’slearned_projections
.- Type
list[ParameterPort]
- primary_learned_projection¶
the Projection with the
matrix
parameter used to generate the LearningMechanism’serror_signal
andlearning_signal
attributes. It is always the first Projection listed in the LearningMechanism’slearned_projections
attribute.- Type
- learned_projections¶
all of the MappingProjections modified by the LearningMechanism; the first item in the list is always the
primary_learned_projection
.- Type
List[MappingProjection]
- function¶
specifies the function used to calculate the
learning_signal
(assigned to the LearningMechanism’s LearningSignal(s)), and theerror_signal
(passed to the LearningMechanism for the preceding MappingProjection in a multilayer learning pathway). It takes the following three arguments, each of which must be a list or 1d array: input, output, and error (see Learning Function for additional details).- Type
LearningFunction or function : default BackPropagation
- learning_rate¶
determines the learning rate for the LearningMechanism. It is used to specify the
learning_rate
parameter for the LearningMechanism’slearning function
(see description of learning_rate for additional details).- Type
float : None
- error_signal¶
one of two values returned by the LearningMechanism’s
function
. For single layer learning, this is the same as the value received in the LearningMechanism’s ERROR_SIGNAL InputPort; for multilayer learning, it is a modified version of the value received, that takes account of the contribution made by the learned_projection and its input to the error signal received. This is assigned as thevalue
of the LearningMechanism’s ERROR_SIGNAL OutputPort.- Type
1d np.array
- learning_signal¶
one of two values returned by the LearningMechanism’s
function
, that specifies the changes to the weights of thematrix
parameter for the LearningMechanism’slearned_projections
; it is calculated to reduce the error signal associated with theprimary_learned_projection
and received from the LearningMechanism’serror_sources
. It is assigned as the value of the LearningMechanism’s LearningSignal(s) and, in turn, its LearningProjection(s).- Type
number or ndarray
- learning_signals¶
list of all of the LearningSignals for the LearningMechanism, each of which sends one or more LearningProjections to the ParameterPort(s) for the
matrix
parameter of the MappingProjection(s) trained by the LearningMechanism. The value of each LearningSignal is the LearningMechanism’slearning_signal
attribute. Since LearningSignals are OutputPorts, they are also listed in the LearningMechanism’soutput_ports
attribute, after it ERROR_SIGNAL OutputPort.- Type
ContentAddressableList[LearningSignal]
- learning_projections¶
list of all of the LearningProjections <LearningProject>` from the LearningMechanism, listed in the order of the LearningSignals to which they belong (that is, in the order they are listed in the learning_signals attribute).
- Type
List[LearningProjection]
- learning_enabled¶
determines whether and when the
learning_projections
are executed. If set to False, they are never updated; however, the LearningMechanism is still executed in any Composition to which it belongs, so that the error signals it calculates can be passed to any other LearningMechanism(s) to which it projects (see Multilayer learning). If set to True orONLINE
,learning_projections
are updated when the LearningMechanism executes. If set toAFTER
,learning_projections
are updated at the end of eachTRIAL
of execution of the Composition to which the LearningMechanism belongs.Note
the
learning_enabled
attribute of a LearningMechanism determines the default behavior of itslearning_projections
. However, this can be overridden for individual LearningProjections by assigning theirlearning_enabled
attributes either at or after construction.- Type
bool or Enum[ONLINE|AFTER]
- output_ports¶
list of the LearningMechanism’s OutputPorts, including its ERROR_SIGNAL OutputPort, followed by its LearningSignal(s), and then any additional (user-specified) OutputPorts.
- Type
ContentAddressableList[OutputPort]
- output_values¶
the first item is the
value
of the LearningMechanism’s ERROR_SIGNAL OutputPort, followed by thevalue
(s) of its LearningSignal(s), and then those of any additional (user-specified) OutputPorts.- Type
2d np.array
- modulation¶
the default form of modulation used by the LearningMechanism’s LearningSignal(s), unless they are individually specified.
- Type
str
- outputPortTypes¶
alias of
psyneulink.core.components.ports.modulatorysignals.learningsignal.LearningSignal
- _parse_function_variable(variable, context=None)¶
Parses the variable passed in to a Component into a function_variable that can be used with the Function associated with this Component
- _validate_variable(variable, context=None)¶
Validate that variable has exactly three items: activation_input, activation_output and error_signal
- _validate_params(request_set, target_set=None, context=None)¶
Validate error_sources
error_sources
argument must be an ObjectiveMechanism, another LearningMechanism, an ERROR_SIGNAL OutputPort of a LearningMechanism, or a list of these, and there must be the same number as there are ERROR_SIGNAL InputPorts.
- _instantiate_input_ports(input_ports=None, reference_value=None, context=None)¶
Instantiate COVARIATES InputPorts if there are any covariate_sources
- _instantiate_attributes_before_function(function=None, context=None)¶
Instantiates MappingProjection(s) from error_sources (if specified) to LearningMechanism
- Also determines and assigns
error_matrices
from theerror_sources
, identified as the matrix for the Projection with which each error_source is associated. :param function:
- Also determines and assigns
- _instantiate_output_ports(context=None)¶
Call Port._instantiate_output_ports to instantiate orderedDict of OutputPort(s)
- This is a stub, implemented to allow Mechanism subclasses to override _instantiate_output_ports
or process InputPorts before and/or after call to _instantiate_output_ports
- add_ports(error_sources, context=None)¶
Add error_source and error_matrix for each InputPort added
- remove_ports(ports)¶
Keep error_signal_input_ports and error_matrices in sych with error_signals in input_ports
- _execute(variable=None, context=None, runtime_params=None)¶
Execute LearningMechanism function and return learning_signal
Identify error_signals received from LearningMechanisms currently being executed Assign them, and the corresponding error_matrices to a pair of arrays Execute function for each error_signal, error_matrix pair Sum the learning_signal and error_signal values received from each execution
- Returns
List[ndarray, ndarray]
- Return type
summed learning_signal, summed error_signal
- exception psyneulink.core.components.mechanisms.modulatory.learning.learningmechanism.LearningMechanismError(message, component=None)¶