# LeabraMechanism¶

## Overview¶

A LeabraMechanism is a subclass of Processing Mechanisms that wraps a leabra network. Leabra is an artificial neural network algorithm (O’Reilly, 1996). For more info about leabra, please see O’Reilly and Munakata, 2016.

Note

The LeabraMechanism uses the leabra Python package, which can be found here at Github. You may need to install the Python leabra package in order to use the LeabraMechanism. While the LeabraMechanism should always match the output of an equivalent network in the leabra package, the leabra package itself is still in development, so it is not guaranteed to be correct yet.

## Creating a LeabraMechanism¶

A LeabraMechanism can be created in two ways. Users can specify the size of the input layer (input_size), size of the output layer (output_size), number of hidden layers (hidden_layers), and sizes of the hidden layers (hidden_sizes). In this case, the LeabraMechanism will initialize the connections as uniform random values between 0.55 and 0.95. Alternatively, users can provide a leabra Network object from the leabra package as an argument (leabra_net), in which case the leabra_net will be used as the network wrapped by the LeabraMechanism. This option requires users to be familiar with the leabra package, but allows more flexibility in specifying parameters. In the former method of creating a LeabraMechanism, the training_flag argument specifies whether the network should be learning (updating its weights) or not.

## Structure¶

The LeabraMechanism has an attribute training_flag which can be set to True/False to determine whether the network is currently learning. The training_flag can also be changed after creation of the LeabraMechanism, causing it to start/stop learning.

Note

If the training_flag is True, the network will learn using the Leabra learning algorithm. Other algorithms may be added later.

The LeabraMechanism has two InputStates: the MAIN_INPUT InputState and the LEARNING_TARGET InputState. The MAIN_INPUT InputState is the input to the leabra network, while the LEARNING_TARGET InputState is the learning target for the LeabraMechanism. The input to the MAIN_INPUT InputState should have length equal to input_size and the input to the LEARNING_TARGET InputState should have length equal to output_size.

Note

Currently, there is a bug where LeabraMechanism (and other Mechanisms with multiple input states) cannot be used as ORIGIN Mechanisms for a System. If you desire to use a LeabraMechanism as an ORIGIN Mechanism, you can work around this bug by creating two TransferMechanisms as ORIGIN Mechanisms instead, and have these two TransferMechanisms pass their output to the InputStates of the LeabraMechanism. Here is an example of how to do this. In the example, T2 passes the training_data to the LEARNING_TARGET InputState of L (L.input_states[1]):

L = LeabraMechanism(input_size=input_size, output_size=output_size)
T1 = TransferMechanism(name='T1', size=input_size, function=Linear)
T2 = TransferMechanism(name='T2', size=output_size, function=Linear)
p1 = Process(pathway=[T1, L])
proj = MappingProjection(sender=T2, receiver=L.input_states[1])
p2 = Process(pathway=[T2, proj, L])
s = System(processes=[p1, p2])
s.run(inputs={T1: input_data, T2: training_data})


## Execution¶

The LeabraMechanism passes input and training data to the leabra Network it wraps, and the LeabraMechanism passes its leabra Network’s output (after one “trial”, default 200 cycles in PsyNeuLink) to its primary OutputState. For details on Leabra, see O’Reilly and Munakata, 2016 and the leabra Python package on Github.

## Class Reference¶

class psyneulink.library.mechanisms.processing.leabramechanism.LeabraFunction(default_variable=None, network=None, params=None, owner=None, prefs=None)

LeabraFunction is a custom function that lives inside the LeabraMechanism. As a function, it transforms the variable by providing it as input to the leabra network inside the LeabraFunction.

Parameters: default_variable (number or np.array : default np.zeros() (array of zeros)) – specifies a template for the input to the leabra network. network (leabra.Network) – specifies the leabra network to be used. params (Dict[param keyword: param value] : default None) – a parameter dictionary that specifies the parameters for the function. Values specified for parameters in the dictionary override any assigned to those parameters in arguments of the constructor. owner (Component) – component to which to assign the Function. prefs (PreferenceSet or specification dict : default Mechanism.classPreferences) – specifies the PreferenceSet for the LeabraMechanism; see prefs for details.
variable

number or np.array – contains value to be transformed.

network

leabra.Network – the leabra network that is being used

owner

Mechanismcomponent to which the Function has been assigned.

prefs

PreferenceSet or specification dict – the PreferenceSet for the LeabraMechanism; 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).

class psyneulink.library.mechanisms.processing.leabramechanism.LeabraMechanism(leabra_net=None, input_size=1, output_size=1, hidden_layers=0, hidden_sizes=None, training_flag=False, params=None, name=None, prefs=None)

Subclass of Processing Mechanisms that is a wrapper for a Leabra network in PsyNeuLink.

Parameters: leabra_net (Optional[leabra.Network]) – a network object from the leabra package. If specified, the LeabraMechanism’s network becomes leabra_net, and the other arguments that specify the network are ignored (input_size, output_size, hidden_layers, hidden_sizes). input_size (int : default 1) – an integer specifying how many units are in (the size of) the first layer (input) of the leabra network. output_size (int : default 1) – an integer specifying how many units are in (the size of) the final layer (output) of the leabra network. hidden_layers (int : default 0) – an integer specifying how many hidden layers are in the leabra network. hidden_sizes (int or List[int] : default input_size) – if specified, this should be a list of integers, specifying the size of each hidden layer. If hidden_sizes is a list, the number of integers in hidden_sizes should be equal to the number of hidden layers. If not specified, hidden layers will default to the same size as the input layer. If hidden_sizes is a single integer, then all hidden layers are of that size. training_flag (boolean : default None) – a boolean specifying whether the leabra network should be learning. If True, the leabra network will adjust its weights using the “leabra” algorithm, based on the training pattern (which is read from its second output state). The training_flag attribute can be changed after initialization, causing the leabra network to start/stop learning. If None, training_flag will default to False if leabra_net argument is not provided. If leabra_net argument is provided and training_flag is None, then the existing learning rules of the leabra_net will be preserved. quarter_size (int : default 50) – an integer specifying how many times the Leabra network cycles each time it is run. Lower values of quarter_size result in shorter execution times, though very low values may cause slight fluctuations in output. Lower values of quarter_size also effectively reduce the magnitude of learning weight changes during a given trial. 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 KWTA-) – a string used for the name of the mechanism. If is not specified, a default is assigned by MechanismRegistry (see Registry for conventions used in naming, including for default and duplicate names). prefs (Optional[PreferenceSet or specification dict : Mechanism.classPreferences]) – the PreferenceSet for mechanism. If it is not specified, a default is assigned using classPreferences defined in __init__.py (see PreferenceSet for details). context (str : default componentType+INITIALIZING) – string used for contextualization of instantiation, hierarchical calls, executions, etc.
variable

value – the input to this Mechanism’s function.

function

LeabraFunction – the function that wraps and executes the leabra mechanism

value

2d np.array [array(float64)] – result of executing function.

input_size

int : default 1 – an integer specifying how many units are in (the size of) the first layer (input) of the leabra network.

output_size

int : default 1 – an integer specifying how many units are in (the size of) the final layer (output) of the leabra network.

hidden_layers

int : default 0 – an integer specifying how many hidden layers are in the leabra network.

hidden_sizes

int or List[int] : default input_size – an integer or list of integers, specifying the size of each hidden layer.

training_flag

boolean – a boolean specifying whether the leabra network should be learning. If True, the leabra network will adjust its weights using the “leabra” algorithm, based on the training pattern (which is read from its second output state). The training_flag attribute can be changed after initialization, causing the leabra network to start/stop learning.

quarter_size

int : default 50 – an integer specifying how many times the Leabra network cycles each time it is run. Lower values of quarter_size result in shorter execution times, though very low values may cause slight fluctuations in output. Lower values of quarter_size also effectively reduce the magnitude of learning weight changes during a given trial.

network

leabra.Network – the leabra.Network object which is executed by the LeabraMechanism. For more info about leabra Networks, please see the leabra package on Github.

output_states

ContentAddressableList[OutputState] : default [RESULT] – list of Mechanism’s OutputStates. By default there is a single OutputState, RESULT, that contains the result of a call to the Mechanism’s function; additional standard and/or custom OutputStates may be included, based on the specifications made in the output_states argument of the Mechanism’s constructor.

output_values

List[array(float64)] – each item is the value of the corresponding OutputState in output_states. The default is a single item containing the result of the TransferMechanism’s function; additional ones may be included, based on the specifications made in the output_states argument of the Mechanism’s constructor (see TransferMechanism Standard OutputStates).

name

str : default LeabraMechanism-<index> – the name of the Mechanism. Specified in the name argument of the constructor for the Projection; if not specified, a default is assigned by MechanismRegistry (see Registry for conventions used in naming, including for default and duplicate names).

prefs

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

Returns: instance of LeabraMechanism LeabraMechanism