# RecurrentTransferMechanism¶

## Overview¶

A RecurrentTransferMechanism is a subclass of TransferMechanism that implements a single-layered recurrent network, in which each element is connected to every other element (instantiated in a recurrent AutoAssociativeProjection referenced by the Mechanism’s matrix parameter). It can report the energy and, if appropriate, the entropy of its output, and can be configured to implement autoassociative (e.g., Hebbian) learning.

## Creating a RecurrentTransferMechanism¶

A RecurrentTransferMechanism is created directly by calling its constructor.:

import psyneulink as pnl
my_linear_recurrent_transfer_mechanism = pnl.RecurrentTransferMechanism(function=pnl.Linear)
my_logistic_recurrent_transfer_mechanism = pnl.RecurrentTransferMechanism(function=pnl.Logistic(gain=1.0,
bias=-4.0))


The recurrent projection is automatically created using (1) the matrix argument or (2) the auto and hetero arguments of the Mechanism’s constructor, and is assigned to the mechanism’s recurrent_projection attribute.

If the matrix argument is used to create the recurrent projection, it must specify either a square matrix or an AutoAssociativeProjection that uses one (the default is HOLLOW_MATRIX).:

recurrent_mech_1 = pnl.RecurrentTransferMechanism(default_variable=[[0.0, 0.0, 0.0]],
matrix=[[1.0, 2.0, 2.0],
[2.0, 1.0, 2.0],
[2.0, 2.0, 1.0]])

recurrent_mech_2 = pnl.RecurrentTransferMechanism(default_variable=[[0.0, 0.0, 0.0]],
matrix=pnl.AutoAssociativeProjection)


If the auto and hetero arguments are used to create the recurrent projection, they set the diagonal and off-diagonal terms, respectively.:

recurrent_mech_3 = pnl.RecurrentTransferMechanism(default_variable=[[0.0, 0.0, 0.0]],
auto=1.0,
hetero=2.0)


Note

In the examples above, recurrent_mech_1 and recurrent_mech_3 are identical.

In all other respects, a RecurrentTransferMechanism is specified in the same way as a standard TransferMechanism.

### Configuring Learning¶

A RecurrentTransferMechanism can be configured for learning when it is created by assigning True to the enable_learning argument of its constructor. This creates an AutoAssociativeMechanism that is used to train its recurrent_projection, and assigns as its function the one specified in the learning_function argument of the RecurrentTransferMechanism’s constructor. By default, this is the Hebbian Function; however, it can be replaced by any other function that is suitable for autoassociative learning; that is, one that takes a list or 1d array of numeric values (an “activity vector”) and returns a 2d array or square matrix (the “weight change matrix”) with the same dimensions as the length of the activity vector. The AutoAssociativeLearningMechanism is assigned to the learning_mechanism attribute and is used to modify the matrix parameter of its recurrent_projection (also referenced by the RecurrentTransferMechanism’s own matrix parameter.

If a RecurrentTransferMechanism is created without configuring learning (i.e., enable_learning is assigned False in its constructor – the default value), then learning cannot be enabled for the Mechanism until it has been configured for learning; any attempt to do so will issue a warning and then be ignored. Learning can be configured once the Mechanism has been created by calling its configure_learning method, which also enables learning.

## Structure¶

The distinguishing feature of a RecurrentTransferMechanism is a self-projecting AutoAssociativeProjection – that is, one that projects from the Mechanism’s primary OutputState back to its primary InputState. This can be parameterized using its matrix, auto, and hetero attributes, and is stored in its recurrent_projection attribute.

A RecurrentTransferMechanism also has two additional OutputStates is bounded between 0 and 1 (e.g., a Logistic function), an ENTROPY OutputState. Each of these report the respective values of the vector in it its RESULTS (primary) OutputState.

Finally, if it has been specified for learning, the RecurrentTransferMechanism is associated with an AutoAssociativeMechanism that is used to train its AutoAssociativeProjection. The learning_enabled attribute indicates whether learning is enabled or disabled for the Mechanism. If learning was not configured when the Mechanism was created, then it cannot be enabled until the Mechanism is configured for learning.

In all other respects the Mechanism is identical to a standard TransferMechanism.

## Execution¶

When a RecurrentTransferMechanism executes, its variable, as is the case with all mechanisms, is determined by the projections the mechanism receives. This means that a RecurrentTransferMechanism’s variable is determined in part by the value of its own primary OutputState on the previous execution, and the matrix of the recurrent projection.

Like a TransferMechanism, the function used to update each element can be assigned using its function parameter. It then transforms its input (including from the recurrent projection) using the specified function and parameters (see Execution), and returns the results in its OutputStates.

If it has been configured for learning and is executed as part of a System, then its associated Learning Mechanism is executed during the learning phase of the System's execution.

## Class Reference¶

class psyneulink.library.mechanisms.processing.transfer.recurrenttransfermechanism.RECURRENT_OUTPUT
RESULT : 1d np.array
the result of the function of the Mechanism
MEAN : float
the mean of the result
VARIANCE : float
the variance of the result
ENERGY : float
the energy of the result, which is calculated using the Stability Function with the ENERGY metric
ENTROPY : float
The entropy of the result, which is calculated using the Stability Function with the ENTROPY metric (Note: this is only present if the Mechanism’s function is bounded between 0 and 1 (e.g. the Logistic Function)).
class psyneulink.library.mechanisms.processing.transfer.recurrenttransfermechanism.RecurrentTransferMechanism( default_variable=None, size=None, function=Linear, matrix=HOLLOW_MATRIX, auto=None, hetero=None, initial_value=None, noise=0.0, smoothing_factor=0.5, clip=[float:min, float:max], learning_rate=None, learning_function=Hebbian, integrator_mode=False, params=None, name=None, prefs=None)

Subclass of TransferMechanism that implements a single-layer auto-recurrent network.

Parameters: default_variable (number, list or np.ndarray : default Transfer_DEFAULT_BIAS) – specifies the input to the Mechanism to use if none is provided in a call to its execute or run method; also serves as a template to specify the length of variable for function, and the primary OutputState of the Mechanism. size (int, list or np.ndarray of ints) – specifies variable as array(s) of zeros if variable is not passed as an argument; if variable is specified, it takes precedence over the specification of size. As an example, the following mechanisms are equivalent: T1 = TransferMechanism(size = [3, 2]) T2 = TransferMechanism(default_variable = [[0, 0, 0], [0, 0]])  function (TransferFunction : default Linear) – specifies the function used to transform the input; can be Linear, Logistic, Exponential, or a custom function. matrix (list, np.ndarray, np.matrix, matrix keyword, or AutoAssociativeProjection : default HOLLOW_MATRIX) – specifies the matrix to use for creating a recurrent AutoAssociativeProjection, or an AutoAssociativeProjection to use. If auto and matrix are both specified, the diagonal terms are determined by auto and the off-diagonal terms are determined by matrix. If hetero and matrix are both specified, the diagonal terms are determined by matrix and the off-diagonal terms are determined by hetero. If auto, hetero, and matrix are all specified, matrix is ignored in favor of auto and hetero. auto (number, 1D array, or None : default None) – specifies matrix as a diagonal matrix with diagonal entries equal to auto, if auto is not None; If auto and hetero are both specified, then matrix is the sum of the two matrices from auto and hetero. In the following examples, assume that the default variable of the mechanism is length 4: setting auto to 1 and hetero to -1 sets matrix to have a diagonal of 1 and all non-diagonal entries -1: $\begin{split}\begin{bmatrix} 1 & -1 & -1 & -1 \\ -1 & 1 & -1 & -1 \\ -1 & -1 & 1 & -1 \\ -1 & -1 & -1 & 1 \\ \end{bmatrix}\end{split}$ setting auto to [1, 1, 2, 2] and hetero to -1 sets matrix to: $\begin{split}\begin{bmatrix} 1 & -1 & -1 & -1 \\ -1 & 1 & -1 & -1 \\ -1 & -1 & 2 & -1 \\ -1 & -1 & -1 & 2 \\ \end{bmatrix}\end{split}$ setting auto to [1, 1, 2, 2] and hetero to [[3, 3, 3, 3], [3, 3, 3, 3], [4, 4, 4, 4], [4, 4, 4, 4]] sets matrix to: $\begin{split}\begin{bmatrix} 1 & 3 & 3 & 3 \\ 3 & 1 & 3 & 3 \\ 4 & 4 & 2 & 4 \\ 4 & 4 & 4 & 2 \\ \end{bmatrix}\end{split}$ See matrix for details on how auto and hetero may overwrite matrix. Can be modified by control. hetero (number, 2D array, or None : default None) – specifies matrix as a hollow matrix with all non-diagonal entries equal to hetero, if hetero is not None; If auto and hetero are both specified, then matrix is the sum of the two matrices from auto and hetero. When diagonal entries of hetero are specified with non-zero values, these entries are set to zero before hetero is used to produce a matrix. See hetero (above) for details on how various auto and hetero specifications are summed to produce a matrix. See matrix (above) for details on how auto and hetero may overwrite matrix. Can be modified by control. initial_value (value, list or np.ndarray : default Transfer_DEFAULT_BIAS) – specifies the starting value for time-averaged input (only relevant if integrator_mode is True). noise (float or function : default 0.0) – a value added to the result of the function or to the result of integrator_function, depending on whether integrator_mode is True or False. See noise for more details. smoothing_factor (float : default 0.5) – the smoothing factor for exponential time averaging of input when integrator_mode is set to True: result = (smoothing_factor * variable) + (1-smoothing_factor * input to mechanism's function on the previous time step)  clip (list [float, float] : default None (Optional)) – specifies the allowable range for the result of function the item in index 0 specifies the minimum allowable value of the result, and the item in index 1 specifies the maximum allowable value; any element of the result that exceeds the specified minimum or maximum value is set to the value of clip that it exceeds.
enable_learning : boolean : default False
specifies whether the Mechanism should be configured for learning; if it is not (the default), then learning cannot be enabled until it is configured for learning by calling the Mechanism’s configure_learning method.
learning_rate : scalar, or list, 1d or 2d np.array, or np.matrix of numeric values: default False
specifies the learning rate used by its learning function. If it is None, the default learning_rate for a LearningMechanism is used; if it is assigned a value, that is used as the learning_rate (see learning_rate for details).
learning_function : function : default Hebbian
specifies the function for the LearningMechanism if learning has been specified for the RecurrentTransferMechanism. It can be any function so long as it takes a list or 1d array of numeric values as its variable and returns a sqaure matrix of numeric values with the same dimensions as the length of the input.
params : Dict[param keyword: param value] : default None
a parameter dictionary that can be used to specify the parameters for the Mechanism, its function, and/or a custom function and its parameters. Values specified for parameters in the dictionary override any assigned to those parameters in arguments of the constructor.
name : str : default see name
specifies the name of the RecurrentTransferMechanism.
prefs : PreferenceSet or specification dict : default Mechanism.classPreferences
specifies the PreferenceSet for the RecurrentTransferMechanism; see prefs for details.
context : str : default componentType+INITIALIZING
string used for contextualization of instantiation, hierarchical calls, executions, etc.
variable

value – the input to Mechanism’s function.

function

Function – the Function used to transform the input.

matrix

2d np.array – the matrix parameter of the recurrent_projection for the Mechanism.

recurrent_projection

AutoAssociativeProjection – an AutoAssociativeProjection that projects from the Mechanism’s primary OutputState back to its primary inputState.

initial_value

value, list or np.ndarray : Transfer_DEFAULT_BIAS – determines the starting value for time-averaged input (only relevant if smoothing_factor parameter is not 1.0).

integrator_function

When integrator_mode is set to True, the RecurrentTransferMechanism executes its integrator_function, which is the AdaptiveIntegrator. See AdaptiveIntegrator for more details on what it computes. Keep in mind that the smoothing_factor parameter of the RecurrentTransferMechanism corresponds to the rate of the RecurrentTransferMechanismIntegrator.

integrator_mode

When integrator_mode is set to True:

the variable of the mechanism is first passed into the following equation:

$value = previous\_value(1-smoothing\_factor) + variable \cdot smoothing\_factor + noise$

The result of the integrator function above is then passed into the mechanism's function. Note that on the first execution, initial_value sets previous_value.

When integrator_mode is set to False:

The variable of the mechanism is passed into the function of the mechanism. The mechanism’s integrator_function is skipped entirely, and all related arguments (noise, leak, initial_value, and time_step_size) are ignored.

noise

float or function : default 0.0 – When integrator_mode is set to True, noise is passed into the integrator_function. Otherwise, noise is added to the output of the function.

If noise is a list or array, it must be the same length as variable.

If noise is specified as a single float or function, while variable is a list or array, noise will be applied to each variable element. In the case of a noise function, this means that the function will be executed separately for each variable element.

Note

In order to generate random noise, we recommend selecting a probability distribution function (see Distribution Functions for details), which will generate a new noise value from its distribution on each execution. If noise is specified as a float or as a function with a fixed output, then the noise will simply be an offset that remains the same across all executions.

smoothing_factor

float : default 0.5 – the smoothing factor for exponential time averaging of input when integrator_mode is set to True:

result = (smoothing_factor * current input) + (1-smoothing_factor * result on previous time_step)

clip

list [float, float] : default None (Optional) – specifies the allowable range for the result of function

the item in index 0 specifies the minimum allowable value of the result, and the item in index 1 specifies the maximum allowable value; any element of the result that exceeds the specified minimum or maximum value is set to

the value of clip that it exceeds.
previous_input

1d np.array of floats – the value of the input on the previous execution, including the value of recurrent_projection.

learning_enabled

bool : default False – indicates whether learning has been enabled for the RecurrentTransferMechanism. It is set to True if learning is specified at the time of construction (i.e., if the enable_learning argument of the Mechanism’s constructor is assigned True, or when it is configured for learning using the configure_learning method. Once learning has been configured, learning_enabled can be toggled at any time to enable or disable learning; however, if the Mechanism has not been configured for learning, an attempt to set learning_enabled to True elicits a warning and is then ignored.

learning_rate

float, 1d or 2d np.array, or np.matrix of numeric values : default None – specifies the learning rate used by the learning_function of the learning_mechanism (see learning_rate for details concerning specification and default value assignement).

learning_function

function : default Hebbian – the function used by the learning_mechanism to train the recurrent_projection if learning is specified.

learning_mechanism

LearningMechanism – created automatically if learning is specified, and used to train the recurrent_projection.

value

2d np.array [array(float64)] – result of executing function; same value as first item of output_values.

output_states

Dict[str: OutputState] – an OrderedDict with the following OutputStates:

• TRANSFER_RESULT, the value of which is the result of function;
• TRANSFER_MEAN, the value of which is the mean of the result;
• TRANSFER_VARIANCE, the value of which is the variance of the result;
• ENERGY, the value of which is the energy of the result, calculated using the Stability Function with the ENERGY metric;
• ENTROPY, the value of which is the entropy of the result, calculated using the Stability Function with the ENTROPY metric; note: this is only present if the Mechanism’s function is bounded between 0 and 1 (e.g., the Logistic function).
output_values

List[array(float64), float, float] – a list with the following items:

• result of the function calculation (value of TRANSFER_RESULT OutputState);
• mean of the result (value of TRANSFER_MEAN OutputState)
• variance of the result (value of TRANSFER_VARIANCE OutputState);
• energy of the result (value of ENERGY OutputState);
• entropy of the result (if the ENTROPY OutputState is present).
name

str – the name of the RecurrentTransferMechanism; if it is not specified in the name argument of the constructor, a default is assigned by MechanismRegistry (see Naming for conventions used for default and duplicate names).

prefs

PreferenceSet or specification dict – the PreferenceSet for the RecurrentTransferMechanism; if it is not specified in the prefs argument of the constructor, a default is assigned using classPreferences defined in __init__.py (see PreferenceSet for details).

Returns: instance of RecurrentTransferMechanism RecurrentTransferMechanism
configure_learning(learning_function=None, learning_rate=None)

Configure RecurrentTransferMechanism for learning. Creates the following Components:

• an AutoAssociativeMechanism – if the learning_function and/or learning_rate arguments are specified, they are used to construct the LearningMechanism, otherwise the values specified in the RecurrentTransferMechanism’s constructor are used;