ContrastiveHebbianMechanism¶
Contents¶
Overview¶
A ContrastiveHebbianMechanism is a subclass of RecurrentTransferMechanism that is customized for use with the Contrastive Hebbian learning rule. See the following references for a description of the learning rule, its relationship to the backpropagation learning rule, and its use in connectionist networks:
The state_features and operation of a ContrastiveHebbianMechanism that differ from those of a RecurrentTransferMechanism are described below.
Creating a ContrastiveHebbianMechanism¶
Size¶
The input_size argument of the constructor must always be specified (this is comparable to specifying the input_shapes or default_variable* arguments of other types of Mechanism). If it is specified on its own, it determines the total number of processing units. If either the hidden_size and/or target_size arguments are specified, then those units are treated as distinct from the input units (see Execution for details).
Phases¶
A ContrastiveHebbianMechanism executes in two phases, and has minus_phase_termination_condition and plus_phase_termination_condition arguments, and corresponding minus_phase_termination_threshold and plus_phase_termination_threshold arguments, that determine when the respective phases of execution are terminated. Other parameters can also be configured that influence processing (see Execution).
OututPorts¶
The Mechanism is automatically assigned three of its five standard_output_ports: OUTPUT_ACTIVITY, CURRENT_ACTIVITY,
and ACTIVITY_DIFFERENT (see below). Additional OutputPorts can be specified
in the additional_output_ports argument of the constructor.
Learning¶
A ContrastiveHebbianMechanism can be configured for learning either by specifying enable_learning as True or using
the configure_learning method, with the following differences from a
standard RecurrentTransferMechanism: it is automatically assigned
ContrastiveHebbian as its learning_function; its learning_condition is automatically assigned as CONVERGENCE; and it is assigned a
MappingProjection from its ACTIVITY_DIFFERENCE (rather than its primary)
OutputPort to the ACTIVATION_INPUT of its learning_mechanism.
SIMPLE_HEBBIAN mode¶
This replicates the function of a standard RecurrentTransferMechanism using Hebbian Learning (e.g., for validation or ease in comparing processing outcomes). It is configured by specifying the mode argument as SIMPLE_HEBBIAN, which automatically makes the following assignments:
These assignments override any others made in the constructor, and the hidden_size and target_size arguments are ignored.
Structure¶
Input¶
A ContrastiveHebbianMechanism always has two, and possibly three InputPorts:
INPUT: receives external input to the Mechanism;
RECURRENT: receives the
valueof the Mechanism’srecurrent_projection;
TARGET: only implemented if target_size is specified, separated =
True(default), and mode is not SIMPLE_HEBBIAN; receives thetargetspecified in therunmethod of any Composition to which the Mechanism belongs.
The sizes of these are determined by arguments in its constructor, which generally conform to one of two configurations.
None or 0, separated = False, and/or mode =Note
If separated = False and target_size is specified, a TARGET InputPort will be created, but
target_size must equal input_size or an error will be generated.
The values of input_size, hidden_size and target_size are assigned to the Mechanism’s
input_size, hidden_size,
and target_size attributes, respectively.
The input_size and target_size
(if separated is True) attribute(s), together with the separated attribute
are used to define the fields of the RECURRENT InputPort’s value and the current_activity attribute used for updating values during execution,
as follows:
input_field: the leftmost number of elements determined by
input_size;
hidden_field: the elements of the RECURRENT InputPort and current_activity that are not within the input_field and/or target_field;
target_field: the rightmost number of elements determined by
target_sizeifseparatedisTrue; otherwise, the same as input_field.
Functions¶
In addition to its primary function, if either the
minus_phase_termination_condition or
plus_phase_termination_condition
is specified as CONVERGENCE, then its phase_phase_convergence_function is used to determine when the corresponding
phase of execution is complete. Its learning_function is automatically assigned as ContrastiveHebbian, but this can be
replaced by any function that takes two 1d arrays (“activity ports”) and compares them to determine the matrix of the Mechanism’s recurrent_projection. If mode is specified as SIMPLE_HEBBIAN), the default function is Hebbian,
but can be replaced by any function that takes and returns a 1d array.
Output¶
A ContrastiveHebbianMechanism is automatically assigned three OutputPorts:
OUTPUT_ACTIVITY: assigned as the primary OutputPort, and contains the pattern of activity the Mechanism is trained to generate. If target_size is specified, then it has the same size as the TARGET InputPort; if target_size is not specified or is 0, if
separatedisFalse, or if mode is SIMPLE_HEBBIAN, then the size of the OUTPUT_ACTIVITY OutputPort is the same as the INPUT InputPort (see Input).
CURRENT_ACTIVITY: assigned the value of the
current_activityattribute after each execution of the Mechanism, which contains the activity of all processing units in the Mechanism (input, hidden and/or target); has the same size as the RECURRENT InputPort.
ACTIVITY_DIFFERENCE: assigned the difference between
plus_phase_activityandminus_phase_activityat the completion of execution. If configured for learning, a MappingProjection is assigned from the ACTIVITY_DIFFERENCE OutputPort to the ACTIVATION_INPUT of thelearning_mechanism.
These also appear in the ContrastiveHebbianMechanism’s standard_output_ports, along with two others – MINUS_PHASE_ACTIVITY and
PLUS_PHASE_ACTIVITY – that it can be assigned, in addition to the standard_output_ports of a RecurrentTransferMechanism.
Execution¶
Processing¶
A ContrastiveHebbianMechanism always executes in two sequential phases, that together constitute a trial of execution:
minus phase: in each execution, the RECURRENT InputPort’s
value(received from therecurrent_projection) is combined with the INPUT InputPort’svalue. The result offunctionis assigned tocurrent_activity. The Mechanism is executed repeatedly until itsminus_phase_termination_conditionis met. At that point, the minus phase is completed, thevalueof the ContrastiveHebbianMechanism is assigned to itsminus_phase_activityattribute, and the plus phase is begun.
plus phase: if
continuousisFalse, thencurrent_activityis reset toinitial_value, and the Mechanism’s previousvalueis reset toNone; otherwise, these retain their value from the last execution in the minus phase. In either case, the RECURRENT InputPort’svalueis combined with the INPUT InputPort’svalue(as during theminus_phase) as well as that of TARGET InputPort (if that is specified). IfseparatedisTrue(which it is by default), then thevalueof the INPUT InputPort is combined with the input_field of the RECURRENT InputPort’svalue, and TARGET is combined with the target_field; otherwise, both are combined with the input_field. Ifhidden_sizeis specified, then the hidden_field of the RECURRENT InputPort’svalueis determined only by thevalueof the Mechanism’srecurrent_projection. Execution then proceeds as during the minus phase, completing when itsplus_phase_termination_conditionis met. At that point, the plus phase is completed, and thevalueof the Mechanism is assigned toplus_phase_activity.
The value of the INPUT, and possibly TARGET, InptutState(s) are combined with that of its
RECURRENT InputPort using the combination_function. The
manner in which these are combined is determined by the clamp attribute: if
it is HARD_CLAMP they are used to replace the corresponding fields of RECURRENT; if it is SOFT_CLAMP, INPUT
(and possibly TARGET) are added to RECURRENT; . The result is passed to the Mechanism’s integrator_function (if integrator_mode
is True) and then its function.
If the termination condition for either phase is specified as CONVERGENCE, it uses the Mechanism’s
phase_convergence_function, together with the
termination_threshold specified for that phase, to determine when execution of that phase terminates. If max_passes is specified, and the number of executions in either phase
reaches that value, an error is generated. Otherwise, once a trial of execution is complete (i.e, after completion
of the minus phase), the following computations and assignments are made:
if a TARGET InputPort has been specified, then the target field of
current_activityis assigned asvalueof OUTPUT_ACTIVITY OutputPort; otherwise, it is assigned the value of the input_field ofcurrent_activity.
plus_phase_activityis assigned as thevalueof the CURRENT_ACTIVITY_ATTR OutputPort;
the difference between
plus_phase_activityandminus_phase_activityis assigned as thevalueof the ACTIVITY_DIFFERENCE OutputPort.
Learning¶
If a ContrastiveHebbianMechanism is configured for learning, at the end of each
trial of execution the value of its ACTIVITY_DIFFERENCE
OutputPort is passed to its learning_mechanism. If the Mechanism is part of a System, then the
learning_mechanism is executed during the execution phase of the System’s execution. Note that this is distinct from the behavior of supervised learning
algorithms (such as Reinforcement and BackPropagation), that are executed during the learning phase of a System’s execution
Class Reference¶
- exception psyneulink.library.components.mechanisms.processing.transfer.contrastivehebbianmechanism.ContrastiveHebbianError(message, component=None)¶
- class psyneulink.library.components.mechanisms.processing.transfer.contrastivehebbianmechanism.ContrastiveHebbianMechanism(input_size, hidden_size=None, target_size=None, separated=None, mode=None, continuous=None, clamp=None, combination_function=None, function=None, matrix=None, auto=None, hetero=None, integrator_function=None, initial_value=None, noise=None, integration_rate=None, integrator_mode=None, clip=None, minus_phase_termination_condition=None, minus_phase_termination_threshold=None, plus_phase_termination_condition=None, plus_phase_termination_threshold=None, phase_convergence_function=None, max_passes=None, enable_learning=None, learning_rate=None, learning_function=None, additional_input_ports=None, additional_output_ports=None, params=None, name=None, prefs=None, **kwargs)¶
Subclass of RecurrentTransferMechanism that implements a single-layer auto-recurrent network using two-phases of execution and the Contrastive Hebbian Learning algorithm. See RecurrentTransferMechanism for additional arguments and attributes.
- Parameters:
input_size (int : default 0) – specifies the size of the INPUT InputPort and number of units in the input_field of
current_activity.hidden_size (int : default None) – specifies the number of units in the hidden_field of
current_activity.target_size (int : default None) – specifies the size of the TARGET InputPort, number of units in the input_field of
current_activity, and of the OUTPUT_ACTIVITY OutputPort.separated (bool : default True) – specifies whether target_field should be different from the input_field.
mode (SIMPLE_HEBBIAN or None : default None) – specifies configuration that emulates standard RecurrentTransferMechanism using
Hebbianlearning (see SIMPLE_HEBBIAN mode for details).combination_function (function : default None) – specifies function used to combine the INPUT, RECURRENT and TARGET (if specified) InputPorts; must accept a 2d array with two or three items of the same length, and generate a result that is the same size as
recurrent_size; ifNone, the ContrastiveHebbianMechanism’s combination_function method is used.clamp (HARD_CLAMP or SOFT_CLAMP : default HARD_CLAMP) – specifies the manner in which the
valueof the INPUT and TARGET (if specified) InputPorts are combined with the RECURRENT InputPort in each execution (seeclampfor additional details.continuous (bool : default True) – specifies whether or not to reset
current_activityat the beginning of the minus phase of a trial.minus_phase_termination_condition (COUNT or CONVERGENCE : default CONVERGENCE) – specifies the type of condition used to terminate the minus_phase of execution (see
minus_phase_termination_conditionfor additional details).minus_phase_termination_threshold (float or int : default 0.01) – specifies the value used to determine when the minus_phase terminates; should be a float if
minus_phase_termination_conditionis CONVERGENCE, and int if it is COUNT (seeminus_phase_termination_thresholdfor additional details).plus_phase_termination_condition (COUNT or CONVERGENCE : default CONVERGENCE) – specifies the type of condition used to terminate the plus_phase of execution (see
plus_phase_termination_conditionfor additional details).plus_phase_termination_threshold (float or int : default 0.01) – specifies the value used to determine when the minus_phase terminates; should be a float if
plus_phase_termination_conditionis CONVERGENCE, and int if it is COUNT (seeplus_phase_termination_thresholdfor additional details).phase_convergence_function (function : default Distance(metric=MAX_ABS_DIFF)) – specifies the function that determines when a phase of execution is complete if the termination condition for that phase is specified as CONVERGENCE, by comparing
current_activitywith the previousvalueof the Mechanism; can be any function that takes two 1d arrays of the same length asvariableand returns a scalar value. The default is theDistanceFunction, using theMAX_ABS_DIFFmetric which computes the elementwise difference between two arrays and returns the difference with the maximum absolute value.max_passes (int : default 1000) – specifies maximum number of executions (
passes) that can occur in an execution phase before reaching the termination_threshold for that phase, after which an error occurs; ifNoneis specified, execution may continue indefinitely or until an interpreter exception is generated.
- input_size¶
size of the INPUT InputPort and
input_activity, and the number of units in the input_field of ofcurrent_activity.- Type:
int
size of
hidden_activity, and number of units incurrent_activityand the hidden_field of the RECURRENT InputPort.- Type:
int
- target_size¶
size of the TARGET InputPort if specified and, if so, the number of units in
target_activity, the target_field ofcurrent_activity, and the OUTPUT_ACTIVITY OutputPort.- Type:
int
- target_start¶
index of first unit of target_field.
- Type:
int
- target_end¶
index of first unit after target_field.
- Type:
int
- separated¶
determines whether target_field is different from input_field (
True) or the same (False).- Type:
bool : default True
- recurrent_size¶
size of RECURRENT InputPort,
current_activity, and CURRENT_ACTIVITY OutputPort.- Type:
int
- mode¶
indicates whether SIMPLE_HEBBIAN was used for configuration (see SIMPLE_HEBBIAN mode for details).
- Type:
SIMPLE_HEBBIAN or None
- recurrent_projection¶
an AutoAssociativeProjection that projects from the CURRENT_ACTIVITY OutputPort to the RECURRENT InputPort.
- combination_function¶
used to combine
valueof the INPUT and TARGET (if specified) InputPorts with that of the RECURRENT InputPort to determine thevariablepassed to the Mechanism’sintegrator_functionand/or itsfunction(see Execution for details).- Type:
method or function
- clamp¶
determines whether the
valueof the INPUT and TARGET (if specified) InputPorts replace (HARD_CLAMP) or are added to (SOFT_CLAMP) thevalueof the RECURRENT InputPort bycombination_function.- Type:
HARD_CLAMP or SOFT_CLAMP
- continuous¶
determines whether or not
current_activityis reset at the beginning of the minus phase of execution. IfFalse, it is set toinitial_value.- Type:
bool : default True
- current_activity¶
the value of the actvity of the ContrastiveHebbianMechanism following its last execution.
- Type:
1d array of floats
- input_activity¶
value of units in the input_field of
current_activity.- Type:
1d array of floats
value of units in the hidden_field of
current_activity;Noneif hidden_size = 0 or mode = SIMPLE_HEBBIAN.- Type:
1d array of floats or None
- target_activity¶
value of units in the target_field of
current_activityif TARGET InputPort is specified. Same asinput_activityifseparatedisFalse;Noneif target_size = 0 or mode = SIMPLE_HEBBIAN.- Type:
1d array of floats or None
- recurrent_activity¶
same as
current_activity- Type:
1d array of floats
- minus_phase_activity¶
the value of the
current_activityat the end of the minus phase of execution.- Type:
1d array of floats
- plus_phase_activity¶
the value of the
current_activityat the end of the plus phase of execution.- Type:
1d array of floats
- delta¶
value returned by
phase_convergence_function; used to determined whenis_convergedisTrue.- Type:
scalar
- is_converged¶
indicates when a phase of execution is complete, if the termination condition for that phase is specified as CONVERGENCE.
Truewhendeltais less than or equal to the termination_threshold speified for the corresponding phase.- Type:
bool
- phase_convergence_function¶
determines when a phase of execution is complete if the termination condition for that phase is specified as CONVERGENCE. Compares the value of
current_activitywith the previousvalue; result is assigned as the value ofdelta.- Type:
function
- minus_phase_termination_condition¶
determines the type of condition used to terminate the minus_phase of execution. If it is CONVERGENCE, execution continues until the value returned by
phase_convergence_functionis less than or equal tominus_phase_termination_threshold; if it is COUNT, the Mechanism is executed the number of times specified inminus_phase_termination_threshold.- Type:
CONVERGENCE or COUNT: default CONVERGENCE
- minus_phase_termination_threshold¶
the value used for the specified
minus_phase_termination_conditionto determine when the minus phase of execution terminates.- Type:
float or int
- plus_phase_termination_condition¶
determines the type of condition used to terminate the plus_phase of execution. If it is CONVERGENCE, execution continues until the value returned by
phase_convergence_functionis less than or equal toplus_phase_termination_threshold; if it is COUNT, the Mechanism is executed the number of times specified inplus_phase_termination_threshold.- Type:
CONVERGENCE or COUNT : default CONVERGENCE
- plus_phase_termination_threshold¶
the value used for the specified
plus_phase_termination_conditionto determine when the plus phase of execution terminates.- Type:
float or int
- max_passes¶
determines the maximum number of executions (
passes) that can occur in an execution phase before reaching the corresponding termination_threshold for that phase, after which an error occurs; ifNoneis specified, execution may continue indefinitely or until an interpreter exception is generated.- Type:
int or None
- execution_phase¶
indicates current phase of execution.
False= minus phase;True= plus phase.- Type:
bool
- output_ports¶
contains the following OutputPorts by default:
OUTPUT_ACTIVITY, the primary OutputPort of the Mechanism, the
valueof which istarget_activityif a TARGET InputPort is implemented; otherwise,input_activityis assigned as itsvalue.CURRENT_ACTIVITY – the
valueof which is a 1d array containing the activity of the ContrastiveHebbianMechanism after each execution; at the end of an execution sequence, it is assigned the value ofplus_phase_activity.ACTIVITY_DIFFERENCE, the
valueof which is a 1d array with the elementwise differences in activity between the plus and minus phases at the end of an execution sequence.
- Type:
- standard_output_ports¶
list of Standard OutputPorts that includes the following in addition to the
standard_output_portsof a RecurrentTransferMechanism:- OUTPUT_ACTIVITY1d np.array
array with activity of the target_field of
current_activityif a TARGET InputPort is specified; otherwise, has activity of the input_field ofcurrent_activity.
- CURRENT_ACTIVITY1d np.array
array with
current_activity.
- ACTIVITY_DIFFERENCE1d np.array
array of element-wise differences between
plus_phase_activityandminus_phase_activity.
- MINUS_PHASE_ACTIVITY1d np.array
array
minus_phase_activity(i.e., activity at the end of the minus phase of execution.
- PLUS_PHASE_ACTIVITY1d np.array
array
plus_phase_activity(i.e., activity at the end of the plus phase of execution.
- Type:
list[str]
- Returns:
instance of ContrastiveHebbianMechanism
- Return type:
- _validate_params(request_set, target_set=None, context=None)¶
Validate shape and size of auto, hetero, matrix.
- _instantiate_input_ports(input_ports=None, reference_value=None, context=None)¶
Call Port._instantiate_input_ports to instantiate orderedDict of InputPort(s)
- This is a stub, implemented to allow Mechanism subclasses to override _instantiate_input_ports
or process InputPorts before and/or after call to _instantiate_input_ports
- _instantiate_attributes_before_function(function=None, context=None)¶
using the
matrixargument the user passed in (which is now stored in function_params), instantiate ParameterPorts for auto and hetero if they haven’t already been instantiated. This is useful if auto and hetero were None in the initialization call. :type function: :param function:
- _instantiate_recurrent_projection(mech, matrix='HollowMatrix', context=None)¶
Instantiate an AutoAssociativeProjection from Mechanism to itself
- _execute(variable=None, context=None, function_variable=None, runtime_params=None)¶
Execute TransferMechanism function and return transform of input
- property _learning_signal_source¶
Override default to use ACTIVITY_DIFFERENCE as source of learning signal