TransferFunctions¶
- Deterministic
- Probabilistic
- Other
Overview¶
TransferFunctions transform their variable but maintain its shape. There are two subclasses of TransferFunctions –
Deterministic and `Probabilistic <Probabilistic> – that have specialized attributes and/or methods.
Standard Attributes¶
All TransferFunctions have a range attribute that specifies the lower and upper limits
of the function’s result. For some subclasses, this may be modified by other parameters. In addition, all
TransferFunctions have a pair of modulable parameters as described below.
multiplicative_param and additive_param: each of these is assigned the name of one of the function’s parameters and used by ModulatoryProjections to modulate the output of the TransferFunction’s function (see Modulatory Parameters).
Derivatives¶
Most TransferFunctions have a derivative method. These take both an input and output argument. In general,
the input is used to compute the derivative of the function at that value. If that is not provided, some
Functions can compute the derivative using the function’s output, either directly (such as Logistic.derivative) or by
inferring the input from the output and then computing the derivative for that value (such as ReLU.derivative)
TranferFunction Class References¶
- class psyneulink.core.components.functions.nonstateful.transferfunctions.Angle(default_variable=None, params=None, owner=None, prefs=None)¶
functionreturns Angle transform of vector invariable:- Parameters:
default_variable (1array : default class_defaults.variable) – specifies a template for the value to be transformed; length must be at least 2.
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.
name (str : default see
name) – specifies the name of the Function.prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the
PreferenceSetfor the Function (seeprefsfor details).
- variable¶
contains value to be transformed.
- Type:
1d array
- name¶
the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).
- Type:
str
- prefs¶
the
PreferenceSetfor function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned usingclassPreferencesdefined in __init__.py (see Preferences for details).- Type:
PreferenceSet or specification dict : Function.classPreferences
- class psyneulink.core.components.functions.nonstateful.transferfunctions.BinomialDistort(default_variable=None, p=None, seed=None, params=None, owner=None, prefs=None)¶
functionreturnsvariablewith elements randomly zeroed with probability p:\[\begin{split}if \ \ rand[0,1] > p: output_i=0 \\ else: \ output_i = variable_i\end{split}\]derivativereturnsvariable- Parameters:
default_variable (number or array : default class_defaults.variable) – specifies a template for the value(s) used as the mean of the Guassian distribution from which each sample is drawn.
p (float : default 0.5) – specifies the probability with which each element of
variableis replaced with zero.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.
name (str : default see
name) – specifies the name of the Function.prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the
PreferenceSetfor the Function (seeprefsfor details).
- variable¶
each element determines mean of the Gaussian distribution from which each sample is drawn.
- Type:
number or array
- random_state¶
private pseudorandom number generator
- Type:
numpy.RandomState
- name¶
the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).
- Type:
str
- prefs¶
the
PreferenceSetfor function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned usingclassPreferencesdefined in __init__.py (see Preferences for details).- Type:
PreferenceSet or specification dict : Function.classPreferences
- class Parameters(owner, parent=None)¶
-
- random_state¶
see
random_state- Default value:
None
- Type:
numpy.random.RandomState
- class psyneulink.core.components.functions.nonstateful.transferfunctions.Dropout(default_variable=None, p=None, params=None, owner=None, prefs=None)¶
functionreturnsvariablewith elements randomly zeroed with probability p during learning; otherwise functions asIdentityFunction. During learning, the output of the function is scaled by \(\frac{1}{(1-p)}\), which implements the inverse scaling form of dropout used by by PyTorch.\[\begin{split}if \ (context.runmode == ContextFlags.LEARNING\_MODE) \ and \ (rand[0,1] > p): output_i = 0 \\ else: \ output_i = \frac{1}{(1-p)}variable_i\end{split}\]derivativereturnsvariable- Parameters:
default_variable (number or array : default class_defaults.variable) – specifies a template for the value to be transformed.
p (float : default 0.5) – specifies the probability with which each element of
variableis replaced with zero.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.
name (str : default see
name) – specifies the name of the Function.prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the
PreferenceSetfor the Function (seeprefsfor details).
- variable¶
contains value to be transformed.
- Type:
number or array
- random_state¶
private pseudorandom number generator
- Type:
numpy.RandomState
- name¶
the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).
- Type:
str
- prefs¶
the
PreferenceSetfor function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned usingclassPreferencesdefined in __init__.py (see Preferences for details).- Type:
PreferenceSet or specification dict : Function.classPreferences
- class Parameters(owner, parent=None)¶
-
- random_state¶
see
random_state- Default value:
None
- Type:
numpy.random.RandomState
- derivative(input)¶
Derivative of
functionat input.- Parameters:
input (number or array) – value of the input to the Dropouput function at which derivative is to be taken.
- Returns:
variable (number or array)
- class psyneulink.core.components.functions.nonstateful.transferfunctions.Exponential(default_variable=None, rate=None, bias=None, scale=None, offset=None, params=None, owner=None, prefs=None)¶
functionreturns exponential transform ofvariable:\[scale * e^{rate*variable+bias} + offset\]derivativereturns the derivative of the Exponential Function:\[scale*rate*(input+bias)*e^{rate*input+bias}\]- Parameters:
default_variable (number or array : default class_defaults.variable) – specifies a template for the value to be transformed.
rate (float : default 1.0) – specifies a value by which to multiply
variablebefore exponentiation.bias (float : default 0.0) – specifies a value to add to
variableafter multplying byrateand before exponentiation.scale (float : default 1.0) – specifies the value by which the result of the function is multiplied, before
offsetis added.offset (float : default 0.0) – specifies the value added to the result of the function after
scalehas been applied.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.
name (str : default see
name) – specifies the name of the Function.prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the
PreferenceSetfor the Function (seeprefsfor details).
- variable¶
contains value to be transformed.
- Type:
number or array
- rate¶
value by which
variableis multiplied before exponentiation; assigned as MULTILICATIVE_PARAM of the Exponential Function.- Type:
float
- bias¶
value added to
variableafter multiplying byrateand before exponentiation; assigned as ADDITIVE_PARAM of the Exponential Function.- Type:
float
- range¶
modified by
scaleif they are specified.- Type:
(0, None)
- scale¶
determines the value by which the result of the function is multiplied, before
offsetis added.- Type:
float
- offset¶
determines the value added to the result of the function after
scalehas been applied.- Type:
float
- name¶
the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).
- Type:
str
- prefs¶
the
PreferenceSetfor function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned usingclassPreferencesdefined in __init__.py (see Preferences for details).- Type:
PreferenceSet or specification dict : Function.classPreferences
- class Parameters(owner, parent=None)¶
- derivative(input)¶
- Parameters:
input (number) – value of the input to the Exponential transform at which derivative is to be taken.
input. (Derivative of function at)
- Returns:
derivative
- Return type:
number or array
- class psyneulink.core.components.functions.nonstateful.transferfunctions.Gaussian(default_variable=None, standard_deviation=None, bias=None, scale=None, offset=None, params=None, owner=None, prefs=None, **kwargs)¶
functionreturns Gaussian transform ofvariable:\[scale*\frac{e^{-\frac{(varible-bias)^{2}}{2\sigma^{2}}}}{\sqrt{2\pi}\sigma}+offset\]where \(\sigma\) =
standard_deviationNote
the value returned is deterministic (i.e., the value of the probability density function at variable), not a randomly chosen sample from the Gaussian distribution; for the latter, use
GaussianDistort.derivativereturns derivative of the Gaussian transform ofvariable:\[\frac{-(variable-bias)*e^{-\frac{(variable-bias)^{2}}{2\sigma^{2}}}}{\sqrt{2\pi}\sigma^{3}}\]- Parameters:
default_variable (number or array : default class_defaults.variable) – specifies a template for the value used as the mean for the Guassian transform.
standard_deviation (float : default 1.0) – specifies “width” of the Gaussian transform applied to each element of
variable.bias (float : default 0.0) – value to add to each element of
variablebefore applying Gaussian transform.scale (float : default 1.0) – specifies the value by which the result of the function is multiplied, before
offsetis added.offset (float : default 0.0) – specifies the value added to the result of the function after
scalehas been applied.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.
name (str : default see
name) – specifies the name of the Function.prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the
PreferenceSetfor the Function (seeprefsfor details).
- variable¶
value used as the mean of the Gaussian transform.
- Type:
number or array
- standard_deviation¶
standard_deviation used for Gaussian transform.
- Type:
float : default 1.0
- bias¶
value added to each element of
variablebefore applying the Gaussian transform.- Type:
float : default 0.0
- range¶
modified by
scaleif they are specified.- Type:
(None, None)
- scale¶
determines the value by which the result of the function is multiplied, before
offsetis added.- Type:
float
- offset¶
determines the value added to the result of the function after
scalehas been applied.- Type:
float
- name¶
the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).
- Type:
str
- prefs¶
the
PreferenceSetfor function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned usingclassPreferencesdefined in __init__.py (see Preferences for details).- Type:
PreferenceSet or specification dict : Function.classPreferences
- derivative(input)¶
Derivative of
functionat input.- Parameters:
input (number) – value of the input of the Gaussian transform at which derivative is to be taken.
- Returns:
Derivative of Guassian of variable (number or array)
- class psyneulink.core.components.functions.nonstateful.transferfunctions.GaussianDistort(default_variable=None, variance=None, bias=None, scale=None, offset=None, seed=None, params=None, owner=None, prefs=None)¶
functionreturns random value from a Gaussian distribution with
Note
if the Gaussian transform of
variableis desired (i.e., the value of the probability density function atvariable, not a randomly chosen sample from the Gaussian distribution, then useGaussian.- Parameters:
default_variable (number or array : default class_defaults.variable) – specifies a template for the value(s) used as the mean of the Guassian distribution from which each sample is drawn.
variance (float : default 1.0) – specifies “width” of the Gaussian distribution around each element of
variablefrom which sample is drawn.bias (float : default 0.0) – specifies value to add to each element of
variablebefore drawing sample.scale (float : default 1.0) – specifies value by which to multiply each sample.
offset (float : default 0.0) – specifies value to add to each sample after it is drawn and
scaleis appliedparams (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.
name (str : default see
name) – specifies the name of the Function.prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the
PreferenceSetfor the Function (seeprefsfor details).
- variable¶
each element determines mean of the Gaussian distribution from which each sample is drawn.
- Type:
number or array
- variance¶
determines variance of Gaussian distribution from which each sample is drawn.
- Type:
float
- scale¶
determines value by which each sample is multiplied after it is drawn.
- Type:
float
- random_state¶
private pseudorandom number generator
- Type:
numpy.RandomState
- name¶
the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).
- Type:
str
- prefs¶
the
PreferenceSetfor function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned usingclassPreferencesdefined in __init__.py (see Preferences for details).- Type:
PreferenceSet or specification dict : Function.classPreferences
- class Parameters(owner, parent=None)¶
-
- random_state¶
see
random_state- Default value:
None
- Type:
numpy.random.RandomState
- class psyneulink.core.components.functions.nonstateful.transferfunctions.Identity(default_variable=None, params=None, owner=None, prefs=None)¶
Returns variable.
- Parameters:
variable (number or np.array : default class_defaults.variable) – specifies a template for the value to be returned.
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.
name (str : default see
name) – specifies the name of the Function.prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the
PreferenceSetfor the Function (seeprefsfor details).
- variable¶
contains value to be returned.
- Type:
number or np.array
- name¶
the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).
- Type:
str
- prefs¶
the
PreferenceSetfor function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned usingclassPreferencesdefined in __init__.py (see Preferences for details).- Type:
PreferenceSet or specification dict : Function.classPreferences
- class psyneulink.core.components.functions.nonstateful.transferfunctions.Linear(default_variable=None, slope=None, intercept=None, scale=None, offset=None, params=None, owner=None, prefs=None)¶
functionreturns linear transform ofvariable:\[scale * (slope * variable + intercept) + offset\]Note
Whereas
scaleandoffsethave effects similar toslopeandintercept, they are applied after those Parameters have been applied tovariable, and thus are not identical; rather, they can be thought of as “amplifying” and “displacing” the Linear function, respectively.Note
The default values for
slope,intercept,scale, andoffsetimplement the IDENTITY_FUNCTION. This may cause the Linear function to be replaced with theIdentityFunction during some circumstances (e.g., compilation).derivativereturns the derivative of the Linear Function:\[scale*slope\]- Parameters:
default_variable (number or array : default class_defaults.variable) – specifies a template for the value to be transformed.
slope (float : default 1.0) – specifies a value by which to multiply
variable.intercept (float : default 0.0) – specifies a value to add to each element of
variableafter applyingslope.scale (float : default 1.0) – specifies the value by which the result of the function is multiplied, before
offsetis added.offset (float : default 0.0) – specifies the value added to the result of the function after
scalehas been applied.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.
name (str : default see
name) – specifies the name of the Function.prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the
PreferenceSetfor the Function (seeprefsfor details).
- variable¶
contains value to be transformed.
- Type:
number or array
- slope¶
value by which each element of
variableis multiplied before applying theintercept(if it is specified).- Type:
float
- intercept¶
value added to each element of
variableafter applying theslope(if it is specified).- Type:
float
- range¶
modified by
scaleif they are specified.- Type:
(None, None)
- scale¶
determines the value by which the result of the function is multiplied, before
offsetis added.- Type:
float
- offset¶
determines the value added to the result of the function after
scalehas been applied.- Type:
float
- name¶
the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).
- Type:
str
- prefs¶
the
PreferenceSetfor function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned usingclassPreferencesdefined in __init__.py (see Preferences for details).- Type:
PreferenceSet or specification dict : Function.classPreferences
- class Parameters(owner, parent=None)¶
- derivative(input)¶
Derivative of
functionat input.- Parameters:
input (number) – value of the input to the Linear transform at which derivative is to be taken.
- Returns:
Slope of function (number or array)
- class psyneulink.core.components.functions.nonstateful.transferfunctions.Logistic(default_variable=None, gain=None, x_0=None, bias=None, scale=None, offset=None, params=None, owner=None, prefs=None, **kwargs)¶
functionreturns logistic transform ofvariable:\[scale * \frac{1}{1 + e^{ - gain ( variable + bias - x_{0} )}} + offset\](this is a vertically offset and scaled version of
Tanh, which is centered on origin).Note
The
biasandx_0Parameters have identical effects, apart from having opposite signs:biasis included to accommodate the convention in the machine learning community;x_0is included to match the standard form of the Logistic Function (in whichgaincorresponds to the k parameter andscalecorresponds to the L parameter);offsetimplements a translation of the function along the vertical axis that is not modulated by gain.derivativereturns the derivative of the Logistic using its output:\[scale * gain * output * (1-output)\]See note above for the effects of
scaleandoffset.- Parameters:
default_variable (number or array : default class_defaults.variable) – specifies a template for the value to be transformed.
gain (float : default 1.0) – specifies value by which to multiply each element of
variableafter it is adjusted bybiasand/orx_0, but before logistic transformation (see note above).bias (float : default 0.0) – specifies value to add to each element of
variablebefore applyinggain; this argument has an effect identical to x_0, but with the opposite sign (see note above).x_0 (float : default 0.0) – specifies value to add to each element of
variablebefore applyinggain; this argument has an effect identical to bias, but with the opposite sign (see note above).scale (float : default 1.0) – specifies the value by which the result of the function is multiplied, before
offsetis added.offset (float : default 0.0) – specifies the value added to the result of the function after
scalehas been applied.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.
name (str : default see
name) – specifies the name of the Function.prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the
PreferenceSetfor the Function (seeprefsfor details).
- variable¶
contains value to be transformed.
- Type:
number or array
- gain¶
value by which to multiply each element of
variableafter it is adjusted bybiasand/orx_0, but before logistic transformation has been applied (see note above).- Type:
float
- bias¶
value to add to each element of
variablebefore applyinggain; this argument has an effect identical to x_0, but with the opposite sign (see note above).- Type:
float
- x_0¶
value to add to each element of
variablebefore applyinggain; this argument has an effect identical to bias, but with the opposite sign (see note above).- Type:
float
- range¶
modified by
scaleif they are specified.- Type:
(0, 1)
- scale¶
determines the value by which the result of the function is multiplied, before
offsetis added.- Type:
float
- offset¶
determines the value added to the result of the function after
scalehas been applied.- Type:
float
- name¶
the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).
- Type:
str
- prefs¶
the
PreferenceSetfor function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned usingclassPreferencesdefined in __init__.py (see Preferences for details).- Type:
PreferenceSet or specification dict : Function.classPreferences
- class Parameters(owner, parent=None)¶
- derivative(input=None, output=None)¶
Derivative of
functionat either input or output.Either input or output must be specified. If output is not specified, derivative is computed from input. If both are specified, input is ignored and derivative is computed from output .. technical_note:
allowing both to be specified is supported for consistency with `BackPropagation` `LearningFunction` which uses output to compute Logistic
- Parameters:
input (number) – value of the input to the Logistic transform at which derivative is to be taken.
output (number) – value of the output of the Logistic transform at which derivative is to be taken.
- Returns:
derivative of logistic transform at output
- Return type:
number or array
- class psyneulink.core.components.functions.nonstateful.transferfunctions.ReLU(default_variable=None, gain=None, bias=None, leak=None, scale=None, offset=None, params=None, owner=None, prefs=None, **kwargs)¶
functionreturns rectified linear tranform ofvariable:\[x = scale * gain * (variable - bias) + offset\]\[max(x, leak * x) + offset\]Commonly used by ReLU units in neural networks.
derivativereturns the derivative of of the rectified linear tranform at its input:\[scale * gain\ if\ input > 0,\ scale * gain * leak\ otherwise\]- Parameters:
default_variable (number or array : default class_defaults.variable) – specifies a template for the value to be transformed.
gain (float : default 1.0) – specifies a value by which to multiply
variableafterbiasis subtracted from it.bias (float : default 0.0) – specifies a value to subtract from each element of
variable; functions as threshold.leak (float : default 0.0) – specifies a scaling factor between 0 and 1 when (variable - bias) is less than or equal to 0.
scale (float : default 1.0) – specifies the value by which the result of the function is multiplied, before
offsetis added.offset (float : default 0.0) – specifies the value added to the result of the function after
scalehas been applied.owner (Component) – component to which to assign the Function.
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.
name (str : default see
name) – specifies the name of the Function.prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the
PreferenceSetfor the Function (seeprefsfor details).
- variable¶
contains value to be transformed.
- Type:
number or array
- gain¶
value by which to multiply
variableafterbiasis subtracted from it.- Type:
float : default 1.0
- bias¶
value to subtract from each element of
variable; functions as threshold.- Type:
float : default 0.0
- leak¶
scaling factor between 0 and 1 when (variable - bias) is less than or equal to 0.
- Type:
float : default 0.0
- range¶
modified by
scaleif they are specified.- Type:
(None, None)
- scale¶
specifies the value by which the result of the function is multiplied, before
offsetis added.- Type:
float : default 1.0
- offset¶
specifies the value added to the result of the function after
scalehas been applied.- Type:
float : default 0.0
- name¶
the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).
- Type:
str
- prefs¶
the
PreferenceSetfor function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned usingclassPreferencesdefined in __init__.py (see Preferences for details).- Type:
PreferenceSet or specification dict : Function.classPreferences
- class Parameters(owner, parent=None)¶
- derivative(input or else output)¶
Derivative of
functionat input or output. If input is specified, that is used to compute the derivative; if input is not specified, it is inferred from the output and then used to compute the derivative.- Parameters:
input (number) – value of the input to the ReLU transform at which derivative is to be taken.
- Returns:
derivative
- Return type:
number or array
- class psyneulink.core.components.functions.nonstateful.transferfunctions.SoftMax(default_variable=None, gain=None, mask_threshold=None, adapt_scale=None, adapt_base=None, adapt_entropy_weighting=None, output=None, per_item=None, params=None, owner=None, prefs=None)¶
SoftMax transform of
variablefunctionreturns SoftMax transform ofvariable:\[\frac{e^{gain * variable_i}}{\sum\limits^{len(variable)}e^{gain * variable}}\]filtered by
outputspecification (see The Softmax function and its derivative for a nice discussion).Note
If
variableis all zeros, the SoftMax transform returns all zeros.Thresholding and Adaptive Gain
For cases in which SoftMax is used with sparse vectors (e.g., one-hots), the value(s) of the most significant entries (e.g., the 1s in a one-hot) can be sensitive to (diminished by) the number of other values in the vector (i.e., its length). For example, whereas for
[1 0]the SoftMax is[0.73105858 0.26894142], for[1 0 0 0]it is[0.47536689 0.1748777 0.1748777 0.1748777]. This can be addressed in one of two ways: either by thresholdingvariablebefore applying the SoftMax function, or by adapting thegainparametrically based on thevariable:mask_threshold – setting the mask_threshold argument to a scalar value causes the
variableto be thresholded by that value before applying the SoftMax function; Each element invariableis first scaled bygain. Then, any elements with an absolute value below mask_threshold are set to negative infinity (-inf), effectively masking them sinceexp(-inf) = 0. The remaining values are then passed through the SoftMax function. This only applies if the gain argument is specified as a scalar; if it is specified as ADAPTIVE, then the mask_threshold argument is ignored.ADAPTIVE – setting gain argument to ADAPTIVE causes it to be dynamically adjusted, based on the entropy and length of the variable, to keep the mass of the distribution around the highest values as consistent as possible over different sized vectors. If ADAPTIVE is specified, then the
mask_thresholdargument is ignored. The gain is adapted by calling the SoftMax function’sadapt_gainmethod. This can be finicky, and may need to be further tuned to the length ofvariable, which can be done using the SoftMax Function’s adapt_scale, adapt_base, and adapt_entropy_weighting arguments.
Derivative
derivativereturns the derivative of the SoftMax. If OUTPUT_TYPE for the SoftMax is ALL, returns Jacobian matrix (derivative for each element of the output array with respect to each of the others):\[D_jS_i = S_i(\delta_{i,j} - S_j),\ where\ \delta_{i,j}=1\ if\ i=j\ and\ \delta_{i,j}=0\ if\ i≠j.\]If OUTPUT_TYPE is ARG_MAX, ARG_MAX_INDICATOR, MAX_VAL, MAX_INDICATOR, returns 1d array of the derivatives of the maximum value(s) with respect to the others (calculated as above). If OUTPUT_TYPE is PROB, raises an exception (since it is ambiguous as to which element would have been chosen by the SoftMax function)
- Parameters:
default_variable (1d array : default class_defaults.variable) – specifies a template for the value to be transformed.
gain (scalar or ADAPTIVE : default 1.0) – specifies the value by which to multiply
variablebefore SoftMax transformation, which functions as the inverse “temperature” of the function. If it is a scalar, it must be greater than zero. If ADAPTIVE is specified, the value is determined dynamically based on thevariable; see Thresholding and Adaptive Gain for details).mask_threshold (scalar : default None) – specifies whether to mask_threshold the
variablebefore applying the SoftMax function; this only applies ifgainis specified as a scalar; otherwise it is ignored (see Thresholding and Adaptive Gain for details).adapt_scale (scalar : default 1) – specifies the scale parameter using by the
adapt_gainmethod (see method for details).adapt_base (scalar : default 1) – specifies the base parameter using by the
adapt_gainmethod (see method for details).adapt_entropy_weighting (default .1) – specifies the entropy_weighting parameter using by the
adapt_gainmethod (see method for details).output (ALL, ARG_MAX, ARG_MAX_INDICATOR, MAX_VAL, MAX_INDICATOR, or PROB : default ALL) – specifies the format of array returned by
function(seeoutputfor details).per_item (boolean : default True) – for 2d variables, determines whether the SoftMax function will be applied to the entire variable (per_item = False), or applied to each item in the variable separately (per_item = True).
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.
name (str : default see
name) – specifies the name of the Function.prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the
PreferenceSetfor the Function (seeprefsfor details).
- variable¶
contains value to be transformed.
- Type:
1d array
- gain¶
determines how
variableis scaled before the SoftMax transformation, determining the “sharpness” of the distribution (it is equivalent to the inverse of the temperature of the SoftMax function); if it is ‘ADAPTIVE’, it is determined dynamically adjusted using theadapt_gainmethod (see Thresholding and Adaptive Gain for additional details).- Type:
scalar or ADAPTIVE
- mask_threshold¶
determines whether the
variableis thresholded before applying the SoftMax function; if it is a scalar, each element ofvariableis first scaled by ` gain <SoftMax.gain>`. Then only elements with an absolute value greater than mask_threshold are considered when applying the SoftMax function, while all other elements are set to-infeffectively masking them sinceexp(-inf) = 0. This only applies ifgainis specified as a scalar; otherwise it is ignored (see Thresholding and Adaptive Gain for details).- Type:
scalar or None
- adapt_scale¶
determines the scale parameter using by the
adapt_gainmethod (see method for details).- Type:
scalar
- adapt_base¶
determines the base parameter using by the
adapt_gainmethod (see method for details).- Type:
scalar
- adapt_entropy_weighting¶
determines the entropy_weighting parameter using by the
adapt_gainmethod (see method for details).- Type:
scalar
- output¶
determines how the SoftMax-transformed values of the elements in
variableare reported in the array returned byfunction:ALL: array of all SoftMax-transformed values (the default);
ARG_MAX: 1 for single element with the maximum SoftMax-transformed value, 0 for all others; (one with lowest index of there are multiple maximum values);
ARG_MAX_INDICATOR: 1 for a single element with the maximum SoftMax-transformed value, 0 for all others; (one with lowest index of there are multiple maximum values);
MAX_VAL: SoftMax-transformed value for the element(s) with the maximum such value, 0 for all others;
MAX_INDICATOR: 1 for the element(s) with the maximum SoftMax-transformed value, 0 for all others;
PROB: probabilistically chosen element based on SoftMax-transformed values after setting the sum of values to 1 (i.e., their Luce Ratio), 0 for all others.
- Type:
ALL, ARG_MAX, ARG_MAX_INDICATOR, MAX_VAL, MAX_INDICATOR, or PROB
- per_item¶
for 2d variables, determines whether the SoftMax function is applied to the entire variable (per_item = False), or applied to each item in the variable separately (per_item = True).
- Type:
boolean : default True
- name¶
the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).
- Type:
str
- prefs¶
the
PreferenceSetfor function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned usingclassPreferencesdefined in __init__.py (see Preferences for details).- Type:
PreferenceSet or specification dict : Function.classPreferences
- class Parameters(owner, parent=None)¶
-
- adapt_scale¶
see
adapt_scale- Default value:
1.0
- Type:
float
- adapt_base¶
see
adapt_base- Default value:
1.0
- Type:
float
- adapt_entropy_weighting¶
-
- Default value:
0.1
- Type:
float
- mask_threshold¶
see
mask_threshold- Default value:
None
- Type:
float
- adapt_gain(v, context)¶
Compute the softmax gain (inverse temperature) based on the entropy of the distribution of values. Uses base, scale, and entropy_weighting parameters of SoftMax function to compute gain:
\[gain = scale * (base + (entropy\_weighting * log(entropy(logistic(v)))))\]- Return type:
float
- derivative(output)¶
- Returns:
derivative of values returned by SoftMax
- Return type:
1d or 2d array (depending on OUTPUT_TYPE of SoftMax)
- class psyneulink.core.components.functions.nonstateful.transferfunctions.Tanh(default_variable=None, gain=None, x_0=None, bias=None, scale=None, offset=None, params=None, owner=None, prefs=None, **kwargs)¶
functionreturns hyperbolic tangent ofvariable:\[\scale*frac{1 - e^{-2(gain*(variable+bias-x\_0)+offset)}}{1 + e^{-2(gain*(variable+bias-x\_0)+offset)}}\]Note
The
Tanhfunction is an offset and scaled version of this function. The parameters used here have the same meaning as those used for theTanhFunction.derivativereturns the derivative of the hyperbolic tangent at its input:\[\frac{scale*gain}{(\frac{1+e^{-2(gain*(variable+bias-x\_0)+offset)}}{2e^{-(gain*( variable+bias-x\_0)+offset)}})^2}\]- Parameters:
default_variable (number or array : default class_defaults.variable) – specifies template for the value to be transformed.
gain (float : default 1.0) – specifies value by which to multiply
variablebefore Tanh transformationbias (float : default 0.0) – specifies value to add to each element of
variablebefore applyinggainand before Tanh transformation. This argument is identical to x_0, with the opposite sign.x_0 (float : default 0.0) – specifies value to subtract from each element of
variablebefore applyinggainand before Tanh transformation. This argument is identical to bias, with the opposite sign.scale (float : default 1.0) – specifies the value by which the result of the function is multiplied, before
offsetis added.offset (float : default 0.0) – specifies the value added to the result of the function after
scalehas been applied.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.
name (str : default see
name) – specifies the name of the Function.prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the
PreferenceSetfor the Function (seeprefsfor details).
- variable¶
contains value to be transformed.
- Type:
number or array
- gain¶
value by which each element of
variableis multiplied before applying thebias(if it is specified).- Type:
float : default 1.0
- bias¶
value added to each element of
variablebefore applying thegain(if it is specified). This attribute is identical to x_0, with the opposite sign.- Type:
float : default 0.0
- x_0¶
value subtracted from each element of
variablebefore applying thegain(if it is specified). This attribute is identical to bias, with the opposite sign.- Type:
float : default 0.0
- range¶
modified by
scaleif they are specified.- Type:
(None, None)
- scale¶
determines the value by which the result of the function is multiplied, before
offsetis added.- Type:
float
- offset¶
determines the value added to the result of the function after
scalehas been applied.- Type:
float
- name¶
the name of the Function; if it is not specified in the name argument of the constructor, a default is assigned by FunctionRegistry (see Naming for conventions used for default and duplicate names).
- Type:
str
- prefs¶
the
PreferenceSetfor function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned usingclassPreferencesdefined in __init__.py (see Preferences for details).- Type:
PreferenceSet or specification dict : Function.classPreferences
- class Parameters(owner, parent=None)¶
- derivative(input)¶
Derivative of
functionat input.- Parameters:
input (number) – value of the input to the Tanh transform at which derivative is to be taken.
- Returns:
derivative
- Return type:
number or array
- class psyneulink.core.components.functions.nonstateful.transferfunctions.TransferFunction(default_variable, params, owner=None, name=None, prefs=None, context=None, **kwargs)¶
Function that transforms variable but maintains its shape.
Abstract base class for TransferFunctions.
In addition to the Parameters listed below, all TransferFunctions have a multiplicative_param and an additive_param – see multiplicative and additive params for additional information.
- range¶
read-only Parameter that indicates the lower and upper limits of the function’s result. The two items of the tuple indicate the lower and upper bounds of range, respectively, with
Noneas the entry if there are no bounds. Some subclasses of TransferFunction may have other Parameters that influence the range, which are described under therangeattribute of the relevant subclasses.- Type:
tuple(lower bound: float, uppper bound: float)
- default_range¶
class attribute that indicates the upper and lower limits of the Function’s result.
- Type:
tuple(lower bound: float, uppper bound: float)
- class psyneulink.core.components.functions.nonstateful.transferfunctions.TransferWithCosts(default_variable=None, input_shapes=None, transfer_fct=None, enabled_cost_functions=None, intensity_cost_fct=None, adjustment_cost_fct=None, duration_cost_fct=None, combine_costs_fct=None, params=None, owner=None, prefs=None)¶
returns value of
variabletransformed bytransfer_fct, after calling any cost functions that are enabled and assigning the result(s) to the corresponding parameter(s), as described below.Cost Functions
The TransferWithCosts function has three individual cost functions that it can execute when its
functionis executed, which assign their results to the attributes indicated below:Which functions are called is determined by the settings in
enabled_cost_functions, that can be initialized in the constructor using the enabled_cost_functions argument, and later modified using theenable_costs,disable_costs,toggle_costandassign_costsmethods. The value of any cost for which its function has never been enabled is None; otherwise, it is the value assigned when it was last enabled and executed (seeduration_cost_fctfor additional details concerning that function).If any cost functions are enabled, then the
combine_costs_fctfunction is executed, which sums the results of those that are enabled (Hadamard style, if the costs are arrays), and stores the result in thecombined_costsattribute. Its value is None if no cost functions have ever been enabled; otherwise it is the value assigned the last time one or more cost functions were enabled.Modulation of Cost Function Parameters
The multiplicative_param and additive_param of each cost function is assigned as a parameter of the TransferWithCost Function. This makes them accessible for modulation when the Function is assigned to a Port (e.g., as the default
functionof a ControlSignal), or a Mechanism. They can be referred to in the modulation argument of a ModulatorySignal's constructor (see Types of Modulation) using the following keywords:INTENSITY_COST_FCT_MULTIPLICATIVE_PARAM INTENSITY_COST_FCT_ADDITIVE_PARAM ADJUSTMENT_COST_FCT_MULTIPLICATIVE_PARAM ADJUSTMENT_COST_FCT_ADDITIVE_PARAM DURATION_COST_FCT_MULTIPLICATIVE_PARAM DURATION_COST_FCT_ADDITIVE_PARAM COMBINE_COSTS_FCT_MULTIPLICATIVE_PARAM COMBINE_COSTS_FCT_ADDITIVE_PARAM
See example of how these keywords can be used to modulate the parameters of the cost functions of a TransferMechanism assigned to a ControlSignal.
- Parameters:
variable (list or 1d array of numbers: Default class_defaults.variable) – specifies shape and default value of the array for variable used by
transfer_fcton which costs are calculated.input_shapes (int : None) – specifies length of the array for
variableused byfunctionand on which costs are calculated; can be used in place of default_value, in which case zeros are assigned as the value(s). An error is generated if both are specified but input_shapes != len(default_value).transfer_fct (TransferFunction : Linear) – specifies the primary function, used to generate the value it returns.
enabled_cost_functions (CostFunctions or List[CostFunctions] : None) – specifies the costs to execute when
functionis called, and include in the computation ofcombined_costs.intensity_cost_fct (Optional[
TransferFunction] : defaultExponential) – specifies the function used to compute theintensity_cost.adjustment_cost_fct (Optional[
TransferFunction] : defaultLinear) – specifies the function used to compute theadjustment_cost.duration_cost_fct (
IntegratorFunction: defaultIntegratorFunction) – specifies the function used to compute theduration_cost.combine_costs_fct (function : default
LinearCombination) – specifies the function used to computecombined_costs.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.
name (str : default see
name) – specifies the name of the Function.prefs (PreferenceSet or specification dict : default Function.classPreferences) – specifies the
PreferenceSetfor the Function (seeprefsfor details).
- variable¶
value used by
function, and on whichintensityand associated costs are calculated.- Type:
1d array
- intensity¶
the result of the transfer_fct <TransferWithCosts.transfer_fct>`, and the value returned by
function.- Type:
1 array
- function¶
primary function, specified by transfer_fct argument of constructor, and also stored in
transfer_fct.- Type:
- transfer_fct¶
the TransferWithCosts Function’s primary function, used to generate the value it returns; same as
function.- Type:
- enabled_cost_functions¶
boolean combination of currently enabled CostFunctions; determines which cost functions are calculated when
functionis called, and are included in the computation ofcombined_costs(see Cost Functions for additional details).- Type:
CostFunctions or None
- intensity_cost¶
cost computed by
intensity_cost_fctfor currentintensity. Value is None ifintensity_cost_fcthas not been enabled (see Cost Functions for additional details).- Type:
float or None
- intensity_cost_fct¶
calculates
intensity_costfrom the current value ofintensity. It can be anyTransferFunction, or any other function that takes and returns a scalar value. The default isExponential.- Type:
- intensity_cost_fct_mult_param¶
references value of the multiplicative_param of
intensity_cost_fct.- Type:
value
- intensity_cost_fct_add_param¶
references value of the additive_param of
intensity_cost_fct.- Type:
value
- adjustment_cost¶
cost of change in
intensityfrom the last timefunctionwas executed. Value is None ifadjustment_cost_fcthas not been enabled (see Cost Functions for additional details).- Type:
float or None
- adjustment_cost_fct¶
calculates
adjustment_costbased on the change inintensityfrom its value the last timefunctionwas executed. It can be anyTransferFunction, or any other function that takes and returns a scalar value.- Type:
- adjustment_cost_fct_mult_param¶
references value of the multiplicative_param of
adjustment_cost_fct.- Type:
value
- adjustment_cost_fct_add_param¶
references value of the additive_param of
adjustment_cost_fct.- Type:
value
- duration_cost¶
integral of
intensity, computed byduration_cost_fct. Value is None ifduration_cost_fcthas not been enabled; othewise, the integral ofintensityis only for those executions offunctionin whichfunctionwas enabled.- Type:
float or None
- duration_cost_fct¶
calculates an integral of
intensity. It can be anyIntegratorFunction, or any other function that takes a list or array of two values and returns a scalar value.- Type:
- duration_cost_fct_mult_param¶
references value of the multiplicative_param of
duration_cost_fct.- Type:
value
- duration_cost_fct_add_param¶
references value of the additive_param of
duration_cost_fct.- Type:
value
- combined_costs¶
combined result of all
cost functionsthat are enabled; computed bycombined_costs_fctfor currentintensity. Value is None if no costs have been enabled (see Cost Functions for additional details).- Type:
float or None
- combine_costs_fct¶
combines the results of all
cost functionsthat are enabled, and assigns the result tocost. It can be any function that takes an array and returns a scalar value.- Type:
function
- combined_costs_fct_mult_param¶
references value of the multiplicative_param of
combined_costs_fct.- Type:
value
- combined_costs_fct_add_param¶
references value of the additive_param of
combined_costs_fct.- Type:
value
- params¶
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.
- Type:
Dict[param keyword: param value] : default None
- name¶
name of the Function.
- Type:
str
- prefs¶
determines the
PreferenceSetfor the Function (seeprefsfor details).- Type:
PreferenceSet or specification dict : default Function.classPreferences
- class Parameters(owner, parent=None)¶
-
- LinearCombination¶
see
LinearCombination- Default value:
- Type:
- SimpleIntegrator¶
see
SimpleIntegrator- Default value:
- Type:
- adjustment_cost¶
see
adjustment_cost- Default value:
None
- Type:
- adjustment_cost_fct¶
- adjustment_cost_fct_add_param¶
see
adjustment_cost_fct_add_param- Default value:
None
- Type:
- adjustment_cost_fct_mult_param¶
see
adjustment_cost_fct_mult_param- Default value:
None
- Type:
- combine_costs_fct¶
-
- Default value:
- Type:
- combine_costs_fct_add_param¶
see
combine_costs_fct_add_param- Default value:
None
- Type:
- combine_costs_fct_mult_param¶
see
combine_costs_fct_mult_param- Default value:
None
- Type:
- combined_costs¶
see
combined_costs- Default value:
None
- Type:
- duration_cost¶
see
duration_cost- Default value:
None
- Type:
- duration_cost_fct¶
-
- Default value:
- Type:
- duration_cost_fct_add_param¶
see
duration_cost_fct_add_param- Default value:
None
- Type:
- duration_cost_fct_mult_param¶
see
duration_cost_fct_mult_param- Default value:
None
- Type:
- enabled_cost_functions¶
-
- Default value:
CostFunctions.INTENSITY
- Type:
CostFunctions
- intensity_cost¶
see
intensity_cost- Default value:
None
- Type:
- intensity_cost_fct¶
-
- Default value:
- Type:
- intensity_cost_fct_add_param¶
see
intensity_cost_fct_add_param- Default value:
None
- Type:
- intensity_cost_fct_mult_param¶
see
intensity_cost_fct_mult_param- Default value:
None
- Type:
- transfer_fct¶
see
transfer_fct
- transfer_fct_add_param¶
see
transfer_fct_add_param- Default value:
None
- Type:
- transfer_fct_mult_param¶
see
transfer_fct_mult_param- Default value:
None
- Type:
- assign_costs(cost_functions, execution_context=None)¶
Assigns specified functions; all others are disabled.
- Parameters:
cost_functions (CostFunctions or List[CostFunctions]) – cost function or list of ones to be used; all other will be disabled.
- Returns:
enabled_cost_functions – current value of
enabled_cost_functions.- Return type:
boolean combination of CostFunctions
- enable_costs(cost_functions, execution_context=None)¶
Enable specified cost functions; settings for all other cost functions are left intact.
- Parameters:
cost_functions (CostFunctions or List[CostFunctions]) – cost function or list of ones to be enabled, in addition to any that are already enabled.
- Returns:
enabled_cost_functions – current value of
enabled_cost_functions.- Return type:
boolean combination of CostFunctions
- disable_costs(cost_functions, execution_context=None)¶
Disable specified cost functions; settings for all other cost functions are left intact.
- Parameters:
cost_functions (CostFunction or List[CostFunctions]) – cost function or list of ones to be disabled.
- Returns:
enabled_cost_functions – current value of
enabled_cost_functions.- Return type:
boolean combination of CostFunctions
- toggle_cost(cost_function_name, assignment=True, execution_context=None)¶
Enable/disable a cost functions.
- Parameters:
cost_function_name (str or CostFunction) – Must be the name of a cost function or a value of CostFunction enum.
- Returns:
enabled_cost_functions – current value of
enabled_cost_functions.- Return type:
boolean combination of CostFunctions
- CostFunctions
- private-members:
- exclude-members:
Parameters