# Parameters¶

PsyNeuLink parameters are objects that represent the user-modifiable parameters of a Component. Parameters have names, default values, and other attributes that define how they are used in models. Parameter s also maintain and provide access to the data used in actual computations - default values, current values, previous values, and logged values.

## Defaults¶

Parameters have two types of defaults: instance defaults and class defaults. Class defaults belong to a PNL class, and suggest valid types and shapes of Parameter values. Instance defaults belong to an instance of a PNL class, and are used to validate compatibility between this instance and other PNL objects. Given a TransferMechanism t:

• instance defaults are accessible by t.defaults
• class defaults are accessible by t.class_defaults or TransferMechanism.defaults

Note

t.defaults.noise is shorthand for t.parameters.noise.default_value, and they both refer to the default noise value for t

## Statefulness of Parameters¶

Parameters can have different values in different execution contexts in order to ensure correctness of and allow access to simulation calculations. As a result, to inspect and use the values of a parameter, in general you need to know the execution context in which you are interested. Much of the time, this execution context is likely to be a Composition:

>>> import psyneulink as pnl
>>> c = pnl.Composition()
>>> d = pnl.Composition()
>>> t = pnl.TransferMechanism()

>>> c.run({t: 5})
[[array([5.])]]
>>> print(t.value)
[[5.]]

>>> d.run({t: 10})
[[array([10.])]]
>>> print(t.value)
[[10.]]

>>> print(t.parameters.value.get(c))
[[5.]]
>>> print(t.parameters.value.get(d))
[[10.]]


The TransferMechanism in the above snippet has a different value for each Composition it is run in. This holds true for all of its stateful Parameters, so they can behave differently in different execution contexts and be modulated during control.

Note

The “dot notation” version - t.value - refers to the most recent execution context in which t was executed. In many cases, you can use this to get or set using the execution context you’d expect. However, in complex situations, if there is doubt, it is best to explicitly specify the execution context.

### For Developers¶

Developers must keep in mind state when writing new components for PNL. Any parameters or values that may change during a run must become stateful Parameters, or they are at risk of computational errors like those encountered in parallel programming.

#### Creating Parameters¶

To create new Parameters, reference this example of a new class B

class B(A):
class Parameters(A.Parameters):
p = 1.0
q = Parameter(1.0, modulable=True)

• create an inner class Parameters on the Component, inheriting from the parent Component’s Parameters class

• an instance of B.Parameters will be assigned to the parameters attribute of the class B and all instances of B

• each attribute on B.Parameters becomes a parameter (instance of the Parameter class)
• as with p, specifying only a value uses default values for the attributes of the Parameter
• as with q, specifying an explicit instance of the Parameter class allows you to modify the Parameter attributes
• if you want assignments to parameter p to be validated, add a method _validate_p(value), that returns None if value is a valid assignment, or an error string if value is not a valid assignment

• if you want all values set to p to be parsed beforehand, add a method _parse_p(value) that returns the parsed value
• for example, convert to a numpy array or float

def _parse_p(value):
return np.asarray(value)

• setters and getters (used for more advanced behavior than parsing) should both return the final value to return (getter) or set (setter)

For example, costs of ControlMechanism has a special getter method, which computes the cost on-the-fly:

def _modulatory_mechanism_costs_getter(owning_component=None, context=None):
try:
return [c.compute_costs(c.parameters.variable._get(context), context=context) for c in owning_component.control_signals]
except TypeError:
return None


and matrix of RecurrentTransferMechanism has a special setter method, which updates its auto and hetero parameter values accordingly

def _recurrent_transfer_mechanism_matrix_setter(value, owning_component=None, context=None):
try:
value = get_matrix(value, owning_component.size[0], owning_component.size[0])
except AttributeError:
pass

if value is not None:
temp_matrix = value.copy()
owning_component.parameters.auto._set(np.diag(temp_matrix).copy(), context)
np.fill_diagonal(temp_matrix, 0)
owning_component.parameters.hetero._set(temp_matrix, context)

return value


Note

The specification of Parameters is intended to mirror the PNL class hierarchy. So, it is only necessary for each new class to declare Parameters that are new, or whose specification has changed from their parent’s. Parameters not present in a given class can be inherited from parents, but will be overridden if necessary, without affecting the parents.

#### Using Parameters¶

Methods that are called during runtime in general must take context as an argument and must pass this context along to other PNL methods. The most likely place this will come up would be for the function method on a PNL Function class, or _execute method on other Components. Any getting and setting of stateful parameter values must use this context, and using standard attributes to store data must be avoided at risk of causing computation errors. You may use standard attributes only when their values will never change during a Run.

You should avoid using dot notation in internal code, as it is ambiguous and can potentially break statefulness.

Parameter attributes:

## Class Reference¶

class psyneulink.core.globals.parameters.Defaults(owner, **kwargs)

A class to simplify display and management of default values associated with the Parameters in a Parameters class.

With an instance of the Defaults class, defaults, defaults.<param_name> may be used to get or set the default value of the associated Parameters object

owner

the Parameters object associated with this object

values(show_all=False)
Parameters: show_all (False) – if True, includes non-user parameters a dictionary with {parameter name: parameter value} key-value pairs corresponding to owner
class psyneulink.core.globals.parameters.Parameter(default_value=None, name=None, stateful=True, modulable=False, modulation_combination_function=None, read_only=False, function_arg=True, pnl_internal=False, aliases=None, user=True, values=None, getter=None, setter=None, loggable=True, log=None, log_condition=<LogCondition.OFF: 0>, history=None, history_max_length=1, history_min_length=0, fallback_default=False, retain_old_simulation_data=False, constructor_argument=None, _owner=None, _inherited=False, _user_specified=False)
default_value

the default value of the Parameter.

Default: None
name

the name of the Parameter.

Default: None
stateful

whether the parameter has different values based on execution context.

Default: True
modulable

if True, the parameter can be modulated; if the Parameter belongs to a Mechanism or Projection, it is assigned a ParameterPort.

Default: Developer Notes: False Currently this does not determine what gets a ParameterPort, but in the future it should
modulation_combination_function

specifies the function used in Port._get_combined_mod_val() to combine values for the parameter if it receives more than one ModulatoryProjections; must be either the keyword MULTIPLICATIVE, PRODUCT, ADDITIVE, SUM, or a function that accepts an n dimensional array and retursn an n-1 dimensional array. If it is None, the an attempt is made to determine it from the an alias for the Parameter’s name (i.e., if that is MULTIPLICATIVE_PARAM or ADDITIVE_PARAM); otherwise the default behavior is determined by Port._get_combined_mod_val().

Default: None
read_only

whether the user should be able to set the value or not (e.g. variable and value are just for informational purposes).

Default: Developer Notes: False Can be manually set, but will trigger a warning unless override=True
pnl_internal

whether the parameter is an idiosyncrasy of PsyNeuLink or it is more intrinsic to the conceptual operation of the Component on which it resides

Default: False
aliases

other names by which the parameter goes (e.g. allocation is the same as variable for ControlSignal).

Type: Default: list None specify as a list of strings
user

whether the parameter is something the user will care about (e.g. NOT context).

Default: True
values

stores the parameter’s values under different execution contexts.

Type: dict{execution_id: value} None
getter

hook that allows overriding the retrieval of values based on a supplied method (e.g. _output_port_variable_getter).

Type: Default: types.FunctionType None kwargs self, owning_component, and context will be passed in if your method uses them. self - the Parameter calling the setter; owning_component - the Component to which the Parameter belongs; context - the context the setter is called with; should return the value
setter

hook that allows overriding the setting of values based on a supplied method (e.g. _recurrent_transfer_mechanism_matrix_setter).

Type: Default: types.FunctionType None should take a positional argument; kwargs self, owning_component, and context will be passed in if your method uses them. self - the Parameter calling the setter; owning_component - the Component to which the Parameter belongs; context - the context the setter is called with; should return the value to be set
loggable

whether the parameter can be logged.

Default: True
log

stores the log of the parameter if applicable.

Type: dict{execution_id: deque([LogEntry])} None
log_condition

the LogCondition for which the parameter should be logged.

Type: LogCondition OFF
history

stores the history of the parameter (previous values). Also see get_previous.

Type: dict{execution_id: deque([LogEntry])} None
history_max_length

the maximum length of the stored history.

 Default: 1
history_min_length

the minimum length of the stored history. generally this does not need to be overridden, but is used to indicate if parameter history is necessary to computation.

 Default: 0
fallback_default

if False, the Parameter will return None if a requested value is not present for a given execution context; if True, the Parameter’s default_value will be returned instead.

Default: False
retain_old_simulation_data

if False, the Parameter signals to other PNL objects that any values generated during simulations may be deleted after they are no longer needed for computation; if True, the values should be saved for later inspection.

Default: False
constructor_argument

if not None, this indicates the argument in the owning Component’s constructor that this Parameter corresponds to. Typically this is used in Parameters that save specification types, as in input_ports_spec and output_ports_spec

Default: None
clear_log(contexts=NotImplemented)

Clears the log of this Parameter for every context in contexts

get(context=None, **kwargs)

Gets the value of this Parameter in the context of context If no context is specified, attributes on the associated Component will be used

Parameters: context (Context, execution_id, Composition) – the context for which the value is stored; if a Composition, uses context.default_execution_id kwargs – any additional arguments to be passed to this Parameter’s getter if it exists
get_delta(context=None)

Gets the difference between the current value and previous value of Parameter in the context of context

Parameters: context (Context, execution_id, Composition) – the context for which the value is stored; if a Composition, uses context.default_execution_id
get_previous(context=None, index=1)

Gets the value set before the current value of this Parameter in the context of context

Parameters: context (Context, execution_id, Composition) – the context for which the value is stored; if a Composition, uses context.default_execution_id index (int : 1) – how far back to look into the history. A value of 1 means the first previous value
reset()

Resets default_value to the value specified in its Parameters class declaration, or inherits from parent Parameters classes if it is not explicitly specified.

set(value, context=None, override=False, skip_history=False, skip_log=False, **kwargs)

Sets the value of this Parameter in the context of context If no context is specified, attributes on the associated Component will be used

Parameters: context (Context, execution_id, Composition) – the context for which the value is stored; if a Composition, uses context.default_execution_id override (False) – if True, ignores a warning when attempting to set a read-only Parameter skip_history (False) – if True, does not modify the Parameter’s history skip_log (False) – if True, does not modify the Parameter’s log kwargs – any additional arguments to be passed to this Parameter’s setter if it exists
class psyneulink.core.globals.parameters.ParameterAlias(source=None, name=None)

A counterpart to Parameter that represents a pseudo-Parameter alias that refers to another Parameter, but has a different name

class psyneulink.core.globals.parameters.ParametersBase(owner, parent=None)

Base class for inner Parameters classes on Components (see Component.Parameters for example)

psyneulink.core.globals.parameters.parse_context(context)
Parameters: context – An execution context (context, Composition) the context associated with context