MemoryFunctions¶
Functions that store and can retrieve a record of their current input.
Overview¶
Functions that store and can return a record of their input.
- class psyneulink.core.components.functions.stateful.memoryfunctions.MemoryFunction(default_variable=None, rate=None, noise=None, initializer=None, params=None, owner=None, prefs=None, context=None, **kwargs)¶
- class psyneulink.core.components.functions.stateful.memoryfunctions.Buffer(default_variable=None, rate=None, noise=None, history=None, initializer=None, params=None, owner=None, prefs=None)¶
Append
variable
to the end ofprevious_value
(i.e., right-append to deque of previous inputs).Note
Every appended item must have same shape as the first.
If specified,
rate
and/ornoise
are applied to items already stored in the array, as follows:\[stored\_item * rate + noise\]Note
Because rate and noise are applied on every call, their effects accumulative exponentially over calls to
function
.If the length of the result exceeds
history
, delete the first item. Returnprevious_value
appended withvariable
.- Parameters
default_variable (number, list or array : default class_defaults.variable) – specifies a template for the value to be integrated; if it is a list or array, each element is independently integrated.
rate (float, list or 1d array : default 1.0) – specifies a value applied multiplicatively to each item already stored in the deque on each call to
function
; must be in interval [0,1]noise (float or Function : default 0.0) – specifies a random value added to each item already in the deque on each call to
function
(seenoise
for details).history (int : default None) – specifies the maxlen of the deque, and hence
value
.float (initializer) – specifies a starting value for the deque; if none is specified, the deque is initialized with an empty list.
ndarray (list or) – specifies a starting value for the deque; if none is specified, the deque is initialized with an empty list.
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
PreferenceSet
for the Function (seeprefs
for details).
- variable¶
current input value appended to the end of the deque.
- Type
number or array
- rate¶
multiplicatively applied to each item already in the deque on call to
function
; implements exponential decay of stored items.- Type
float or 1d array with all elements in interval [0,1]
- noise¶
random value added to each item of the deque in each call to
function
(seenoise
for additional details).- Type
float or Function
- history¶
determines maxlen of the deque and the value returned by the
function
. If appendingvariable
toprevious_value
exceeds history, the first item ofprevious_value
is deleted, andvariable
is appended to it, so thatvalue
maintains a constant length. If history is not specified, the value returned continues to be extended indefinitely.- Type
int
- initializer¶
value assigned as the first item of the deque when the Function is initialized, or reset if the new_previous_value argument is not specified in the call to
reset
.- Type
float, list or ndarray
- previous_value¶
state of the deque prior to appending
variable
in the current call.- Type
1d array : default class_defaults.variable
- 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
PreferenceSet
for function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned usingclassPreferences
defined in __init__.py (see Preferences for details).- Type
PreferenceSet or specification dict : Function.classPreferences
- reset(previous_value=None, context=None)¶
Clears the
previous_value
deque.If an argument is passed into reset or if the
initializer
attribute contains a value besides [], then that value is used to start the newprevious_value
deque. Otherwise, the newprevious_value
deque starts out empty.value
takes on the same value asprevious_value
.
- class psyneulink.core.components.functions.stateful.memoryfunctions.DictionaryMemory(default_variable=None, retrieval_prob=None, storage_prob=None, noise=None, rate=None, initializer=None, distance_function=None, selection_function=None, duplicate_keys=None, equidistant_keys_select=None, max_entries=None, seed=None, params=None, owner=None, prefs=None)¶
Implement a configurable, dictionary-style storage and retrieval of key-value pairs, in which storage is determined by
storage_prob
, and retrieval of items is determined bydistance_function
,selection_function
, andretrieval_prob
. Keys and values may have different lengths, and values may vary in length from entry to entry, but all keys must be the same length. Duplicate keys can be allowed, disallowed, or overwritten usingduplicate_keys
), and how selection is made among duplicate keys or ones indistinguishable by thedistance_function
can be specified usingequidistant_keys_select
.The class also provides methods for directly retrieving an entry (
get_memory
), and adding (add_to_memory
) and deleting (delete_from_memory
) one or more entries.An item is stored and retrieved as a 2d array containing a key-value pair ([[key][value]]). A 3d array of such pairs can be used to initialize the contents of memory by providing it in the initialzer argument of the DictionaryMemory’s constructor, or in a call to its
reset
method. The current contents of the memory can be inspected using thememory
attribute, which returns a list containing the current entries, each as a 2 item list containing a key-value pair.When
function
is executed, it first retrieves the item inmemory
with the key that most closely matches the key of the item (key-value pair) in the call, stores the latter in memory, and returns the retrieved item (key-value pair). If the key of the pair in the call is an exact match of a key in memory andduplicate_keys
is False, then the matching item is returned, but the pair in the call is not stored. These steps are described in more detail below:First, with probability
retrieval_prob
, an entry is retrieved frommemory
that has a key that is closest to the one in the call (first item ofvariable
), as determined by thedistance_function
andselection_function
. Thedistance_function
generates a list of distances of each key in memory from the one in the call; theselection_function
then determines which to select ones for consideration. If more than one entry from memory is identified,equidistant_keys_select
is used to determine which to retrieve. If no retrieval occurs, an appropriately shaped zero-valued array is assigned as the retrieved memory (and returned by thefunction
.
After retrieval, the key-value pair in the call (
variable
) is stored inmemory
with probabilitystorage_prob
. If the key (variable
[0]) is identical to one already inmemory
andduplicate_keys
is set to False, storage is skipped; if it is set to OVERWRITE, the value of the key in memory is replaced with the one in the call. If rate and/or noise arguments are specified in the constructor, it is applied to the key before storing, as follows:
\[variable[1] * rate + noise\]If the number of entries exceeds
max_entries
, the first (oldest) item in memory is deleted.- Parameters
default_variable (list or 2d array : default class_defaults.variable) – specifies a template for the key and value entries of the dictionary; list must have two entries, each of which is a list or array; first item is used as key, and second as value entry of dictionary.
retrieval_prob (float in interval [0,1] : default 1.0) – specifies probability of retrieiving a key from
memory
.storage_prob (float in interval [0,1] : default 1.0) – specifies probability of adding
variable
tomemory
.rate (float, list, or array : default 1.0) – specifies a value used to multiply key (first item of
variable
) before storing inmemory
(seerate
for details).noise (float, list, array, or Function : default 0.0) – specifies a random value added to key (first item of
variable
) before storing inmemory
(seenoise
for details).initializer (3d array or list : default None) – specifies an initial set of entries for
memory
. It must be of the following form: [[[key],[value]], [[key],[value]], …], such that each item in the outer dimension (axis 0) is a 2d array or list containing a key and a value pair for that entry. All of the keys must be 1d arrays or lists of the same length.distance_function (Distance or function : default Distance(metric=COSINE)) – specifies the function used during retrieval to compare the first item in
variable
with keys inmemory
.selection_function (OneHot or function : default OneHot(mode=ARG_MIN_VAL)) – specifies the function used during retrieval to evaluate the distances returned by
distance_function
and select the item to return.equidistant_keys_select (RANDOM | OLDEST | NEWEST : default RANDOM) – specifies which item is chosen for retrieval if two or more keys have the same distance from the first item of
variable
.duplicate_keys (bool | OVERWRITE : default False) – specifies whether entries with duplicate keys are allowed in
memory
(seeduplicate_keys
).max_entries (int : default None) – specifies the maximum number of entries allowed in
memory
(seemax_entries
).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
PreferenceSet
for the Function (seeprefs
for details).
- variable¶
1st item (variable[0] is the key used to retrieve an enrtry from
memory
, and 2nd item (variable[1]) is the value of the entry, paired with key and added to thememory
.- Type
2d array
- rate¶
value applied multiplicatively to key (first item of
variable
) before storing inmemory
(seerate
for additional details).- Type
float or 1d array
- noise¶
value added to key (first item of
variable
) before storing inmemory
(seenoise
for additional details).- Type
float, 1d array or Function
- initializer¶
initial set of entries for
memory
; each is a 2d array with a key-value pair.- Type
3d array
- memory¶
list of key-value pairs containing entries in DictionaryMemory: [[[key 1], [value 1]], [[key 2], value 2]]…]
- Type
list
- distance_function¶
function used during retrieval to compare the first item in
variable
with keys inmemory
.- Type
Distance or function : default Distance(metric=COSINE)
- selection_function¶
function used during retrieval to evaluate the distances returned by
distance_function
and select the item(s) to return.- Type
OneHot or function : default OneHot(mode=ARG_MIN_VAL)
- duplicate_keys¶
determines whether entries with duplicate keys are allowed in
memory
. If True (the default), items with keys that are the same as ones in memory can be stored; on retrieval, a single one is selected based onequidistant_keys_select
. If False, then an attempt to store and item with a key that is already inmemory
is ignored, and the entry already in memory with that key is retrieved. If a duplicate key is identified during retrieval (e.g., duplicate_keys is changed from True to False), a warning is issued and zeros are returned. If OVERWRITE, then retrieval of a cue with an identical key causes the value at that entry to be overwritten with the new value.- Type
bool | OVERWRITE
- equidistant_keys_select¶
deterimines which entry is retrieved when duplicate keys are identified or are indistinguishable by the
distance_function
.- Type
RANDOM | OLDEST | NEWEST
- max_entries¶
maximum number of entries allowed in
memory
; if storing a memory exceeds the number, the oldest memory is deleted.- Type
int
- 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
PreferenceSet
for function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned usingclassPreferences
defined in __init__.py (see Preferences for details).- Type
PreferenceSet or specification dict : Function.classPreferences
- Returns
value and key of entry that best matches first item of `variable <DictionaryMemory.variable>` – if no retrieval occures, an appropriately shaped zero-valued array is returned.
- Return type
2d array
- reset(<new_dictionary> default={})¶
Clears the memory in
previous_value
.If an argument is passed into reset or if the
initializer
attribute contains a value besides [], then that value is used to start the new memory inprevious_value
. Otherwise, the newprevious_value
memory starts out empty.value
takes on the same value asprevious_value
.
- get_memory(query_key, context=None)¶
Retrieve memory from
memory
based ondistance_function
andselection_function
.- Parameters
query_key (list or 1d array) – must be same length as key(s) of any existing entries in
memory
.- Returns
value and key for item retrieved – if no retrieval occurs, returns appropriately shaped zero-valued array.
- Return type
2d array as list
- add_to_memory(memories, context=None)¶
Add one or more key-value pairs into
memory
- Parameters
memories (list or array) – a single memory (list or 2d array) or list or array of memorys, each of which must be a valid entry consisting of two items (e.g., [[key],[value]] or [[[key1],[value1]],[[key2],[value2]]]. The keys must all be the same length and equal to the length as key(s) of any existing entries in
dict
. Items are added to memory in the order listed.
- delete_from_memory(memories, key_only=True, context=None)¶
Delete one or more key-value pairs from
memory
- Parameters
memories (list or array) – a single memory (list or 2d array) or list or array of memorys, each of which must be a valid entry consisting of two items (e.g., [[key],[value]] or [[[key1],[value1]],[[key2],[value2]]].
key_only (bool : default True) – if True, delete all memories with the same keys as those listed in memories; if False, delete only memories that have the same key and value as those listed in memories.
- class psyneulink.core.components.functions.stateful.memoryfunctions.ContentAddressableMemory(default_variable=None, retrieval_prob=None, storage_prob=None, rate=None, noise=None, initializer=None, distance_field_weights=None, distance_function=None, selection_function=None, duplicate_entries_allowed=None, duplicate_threshold=None, equidistant_entries_select=None, max_entries=None, seed=None, params=None, owner=None, prefs=None)¶
Sections
Overview
The ContentAddressableMemory Function implements a configurable, content-addressable storage and retrieval of entries from
memory
. Storage is determined bystorage_prob
, retrieval of entries is determined bydistance_function
,selection_function
, andretrieval_prob
, with the contribution that each field of the cue makes to retrieval determined by thedistance_field_weights
parameter.Entries and Fields. The default_variable argument specifies the shape of an entry in
memory
, each of which is a list or array of fields that are themselves lists or 1d arrays (see Memory Fields). An entry can have an arbitrary number of fields, and each field can have an arbitrary length. However, all entries must have the same number of fields, and the corresponding fields must all have the same length across entries. Fields can be weighted to determine the influence they have on retrieval, using thedistance_field_weights
parameter (see retrieval below).Hint
Entries in
memory
can be assigned “labels” – i.e., values that are not used in the calculation of distance – by assigning them a weight of 0 or None indistance_field_weights
); either can be used for labels that are numeric values; however, if non-numeric values are assigned to a field as labels, then None must be specified for that field indistance_field_weights
.Retrieval. Entries are retrieved from
memory
based on their distance fromvariable
, used as the cue for retrieval. The distance is computed using thedistance_function
, which comparesvariable
with each entry inmemory
. If memories have more than one field, then the distances are computed in one of two ways: i) as full vectors (i.e., with all fields of each concatenated into a single array) ifdistance_field_weights
is a single scalar value or a list of identical values); or field-by-field, ifdistance_field_weights
is a list of non-identical values, by computing the distance of each field invariable
with the corresponding ones of each entry inmemory
, and then averaging those distances weighted by the coefficients specified indistance_field_weights
. The distances computed betweenvariable `
and each entry inmemory
are then used byselection_function
to determine which entry is retrieved (or how to weight the sum of them based on their distances from the cue – seeselection_type
). The distance used for the last retrieval (i.e., betweenvariable
and the entry retrieved), the distances of each of their corresponding fields (weighted bydistance_field_weights
), and the distances to all other entries are stored indistance
anddistances_by_field
, anddistances_to_entries
respectively.Duplicate Entries. These can be allowed, disallowed, or overwritten during storage using
duplicate_entries_allowed
), and how selection is made among duplicate entries or ones indistinguishable by thedistance_function
can be specified usingequidistant_entries_select
.The class also provides methods for directly retrieving (
get_memory
), adding (add_to_memory
) and deleting (delete_from_memory
) one or more entries frommemory
.Structure
An entry is stored and retrieved as an array containing a set of fields each of which is a 1d array. An array containing such entries can be used to initialize the contents of
memory
by providing it in the initializer argument of the ContentAddressableMemory’s constructor, or in a call to itsreset
method. The current contents ofmemory
can be inspected using thememory
attribute, which returns a list containing the current entries, each as a list containing all fields for that entry. Thememory_num_fields
contains the number of fields expected for each entry,memory_field_shapes
their shapes, andmemory_num_entries
the total number of entries inmemory
.Both
memory
and all entries are stored as np.ndarrays, the dimensionality of which is determined by the shape of the fields of an entry. If all fields have the same length (regular), then they are 2d arrays andmemory
is a 3d array. However, if fields vary in length (ragged) then, although each field is 1d, an entry is also 1d (with dtype=’object’), andmemory
is 2d (with dtype=’object’).Execution
When the ContentAddressableMemory function is executed, it first retrieves the entry in
memory
that most closely matchesvariable
in the call, stores the latter inmemory
, and returns the retrieved entry. Ifvariable
is an exact match of an entry inmemory
, andduplicate_entries_allowed
is False, then the matching item is returned, butvariable
is not stored. These steps are described in more detail below.Retrieval: first, with probability
retrieval_prob
, a retrieval is made frommemory
. This is either the entry closest tovariable
, or weighted sum of the entries inmemory
based on their distances tovariable
, as determined byselection_function
andselection_type
. The retrieval is made by calling, in order:distance_function
: generates a list of and comparesdistances
betweenvariable
and each entry inmemory
, possibly weighted bydistance_field_weights
, as follows:if
distance_field_weights
is either a scalar or an array of scalars that are all the same, then it is used simply to scale the distance computed betweenvariable
and each entry inmemory
, each of which is computed by concatenating all items ofvariable
into a 1d array, similarly concatenating all memory_fields of an entry inmemory
, and then usingdistance_function
to compute the distance betwen them.if
distance_field_weights
is an array of non-identical scalars , thenvariable
is compared with each entry inmemory
by usingdistance_function
to compute the distance of each item invariable
with the corresponding field of the entry in memory, and then averaging those distances weighted by the corresponding element ofdistance_field_weights
.Note
Fields assigned a weight of 0 or None are ignored in the distance calculation; that is, the distances between
variable
and entries for those fields are not included in the averaging of distances by field. If all of thedistance_field_weights
are 0 or None, then no memory is retrieved (this is equivalent to settingretrieval_prob
to 0).
selection_function
: called with the list of distances to determine how to generate a retrieval based on the distance of each entry inmemory
fromvariable
. The type of retrieval is determined by theselection_type
of the function (an attribute determined from the function on Construction of the ContentAddressableMemory function): if it is SINGLE, then the entry with the minimum distance is retrieved; if it is WEIGHTED, then the entries are weighted by their distance fromvariable
and the weighted sum of the entries is retrieved. Ifselection_type
is SINGLE and two or more entries have the lowest and equal distance fromvariable
, theequidistant_entries_select
attribued is used to determine which to retrieve. If no retrieval occurs, an appropriately shaped zero-valued array is assigned as the retrieved memory, and returned by the function.
The distance between
variable
and the retrieved entry is assigned todistance `
, the distance between of each of their fields is assigned todistances_by_field
, and the distances ofvariable
to all entries inmemory
is assigned todistances_to_entries
.
Storage: after retrieval, an attempt is made to store
variable
inmemory memory
with probabilitystorage_prob
; if the attempt is made:if
variable
is identical to an entry already inmemory
, as evaluated bydistance_function
andduplicate_threshold
, thenduplicate_entries_allowed
determines whether or not to store the entry;if
duplicate_entries_allowed
is:False – storage is skipped;
True –
variable
is stored as another duplicate;OVERWRITE – the duplicate entry in
memory
is replaced with
variable
(which may be slightly different than the item it replaces, within the tolerance ofduplicate_threshold
), and the matching entry is returned;Note
If
duplicate_entries_allowed
is OVERWRITE but a duplicate entry is nevertheless identified during retrieval (e.g., duplicate_entries_allowed was previously changed from True to False), a warning is issued, and duplicate entry is overwritten withvariable
.if storage rate and/or noise arguments are specified in the constructor, they are applied to
variable
before storage as \(variable * rate + noise\);finally, if the number of entries in
memory
exceedsmax_entries
, the first (oldest) entry is deleted. The current number of entries in memory is contained in thememory_num_entries
attribute.
Examples
Initialize memory with **default_variable
The format for entries in
memory <ContentAddressableMemory.memory
can be specified using either the default_variable or initializer arguments of the Function’s constructor. default_variable specifies the shape of entries, without creating any entries:>>> c = ContentAddressableMemory(default_variable=[[0,0],[0,0,0]]) >>> c([[1,2]]) array([[0, 0]])
Since
memory
was not intialized, the first call to the Function returns an array of zeros, formatted as specified in defaul_variable. However, the input in the call to the Function ([[1,2]]
) is stored as an entry inmemory
:>>> c.memory array([[[1., 2.]]])
and is returned on the next call:
>>> c([[2,5]]) array([[1., 2.]])
Note that even though default_variable and the inputs to the Function are specified as lists, the entries returned are arrays;
memory
and all of its entries are always formated as arrays.Initialize memory with **initializer
The initializer argument of a ContentAddressableMemory’s constructor can be used to initialize its
memory
:>>> c = ContentAddressableMemory(initializer=[[[1,2],[3,4,5]], ... [[10,9],[8,7,6]]]) >>> c([[1,2],[3,4,6]]) array([array([1., 2.]), array([3., 4., 5.])], dtype=object) >>> c([[1,2],[3,4,6]]) array([array([1., 2.]), array([3., 4., 6.])], dtype=object)
Note that there was no need to use default_variable, and in fact it would overidden if specified.
Weighting fields
The distance_field_weights argument can be used to differentially weight memory fields to modify their influence on retrieval (see distance_field_weights). For example, this can be used to configure the Function as a dictionary, using the first field for keys (on which retrieval is based) and the second for values (that are retrieved with a matching key), as follows:
>>> c = ContentAddressableMemory(initializer=[[[1,2],[3,4]], ... [[1,5],[10,11]]], ... distance_field_weights=[1,0]) >>> c([[1,2.5],[10,11]]) array([[1., 2.], [3., 4.]])
Note that the first entry
[[1,2],[3,4]]
inmemory
was retrieved, even though the cue used in the call ([[1,2.5],[10,11]]
) was an exact match to the second field of the second entry ([[1,5],[10,11]]
). However, since that field was assigned 0 in distance_field_weights, it was ignored and, using only the first entry, the cue was closer to the first entry. This is confirmed by repeating the example without specifying distance_field_weights:>>> c = ContentAddressableMemory(initializer=[[[1,2],[3,4]], ... [[1,5],[10,11]]]) >>> c([[1,2.5],[10,11]]) array([[ 1., 5.], [10., 11.]])
Duplicate entries
By default, duplicate entries are precluded from a ContentAddressableMemory’s
memory
. So, for an initializer with identical entries, only one copy of the duplicates will be stored:>>> c = ContentAddressableMemory(initializer=[[[1,2],[3,4]], ... [[1,2],[3,4]]]) >>> c.memory array([[[1., 2.], [3., 4.]]])
and using the same array as input to the function will retrieve that array but not store another copy:
>>> c([[1,2],[3,4]]) array([[1., 2.], [3., 4.]]) >>> c.memory array([[[1., 2.], [3., 4.]]])
Only fields with non-zero weights in
distance_field_weights
are considered when evaluating whether entries are duplicates. So, in the following example, where the weight for the second field is 0, the two entries are considered duplicates and only the first is stored:>>> c = ContentAddressableMemory(initializer=[[[1,2],[3,4]], ... [[1,2],[5,6]]], ... distance_field_weights=[1,0]) >>> c.memory array([[[1., 2.], [3., 4.]]])
Duplicates can be allowed by setting the duplicate_entries_allowed argument to True or OVERWRITE. Setting it to True allows duplicate entries to accumulate in
memory
, as shown here:>>> c = ContentAddressableMemory(initializer=[[[1,2],[3,4]], ... [[1,5],[10,11]]], ... duplicate_entries_allowed=True) >>> c([[1,2],[3,4]]) array([[1., 2.], [3., 4.]]) >>> c.memory array([[[ 1., 2.], [ 3., 4.]], [[ 1., 5.], [10., 11.]], [[ 1., 2.], [ 3., 4.]]])
Duplicates are determined by comparing entries using the functions
distance_function
; if thedistance
is less thanduplicate_threshold
, they are considered to be duplicates; otherwise they are treated a distinct entries. By default,duplicate_threshold
is 0. In the folloiwng example it is increased, so that two very similar, but non-identical entries, are nonetheless treated as duplicates:>>> c = ContentAddressableMemory(initializer=[[[1, 2.0], [3, 4]], ... [[1, 2.5], [3, 4]]], ... duplicate_entries_allowed=False, ... duplicate_threshold=0.2) >>> c.memory array([[[1., 2.], [3., 4.]]])
Setting duplicate_entries_allowed argument to OVERWRITE allows an entry to replace one that is considered duplicate, even if it is not identical, as in the following example:
>>> c.duplicate_entries_allowed=OVERWRITE >>> c([[1, 2.1], [3, 4]]) array([[1., 2.], [3., 4.]]) >>> c.memory array([[[1. , 2.1], [3. , 4. ]]])
Note that the entry considered to be the duplicate (
[[1, 2.1], [3, 4]]
) is returned, and then replaced inmemory
. Finally, ifduplicate_entries_allowed
is True, and duplicates have accumulated, theequidistant_entries_select
attribute can be used to specify how to select among them for retrieval, either by chosing randomly (RANDOM) or selecting either the first one (OLDEST) or last one (NEWEST) stored.Class Reference
- Parameters
default_variable (list or 2d array : default class_defaults.variable) – specifies a template for an entry in the dictionary; the list or array can have any number of items, each of which must be a list or array of any length; however, at present entries are constrained to be at most 2d.
retrieval_prob (float in interval [0,1] : default 1.0) – specifies probability of retrieving an entry from
memory
.storage_prob (float in interval [0,1] : default 1.0) – specifies probability of adding
variable
tomemory
.rate (float, list, or array : default 1.0) – specifies a value used to multiply
variable
before storing inmemory
(seerate
for details).noise (float, list, 2d array, or Function : default 0.0) – specifies random value(s) added to
variable
before storing inmemory
; if a list or 2d array, it must be the same shape asvariable ContentAddressableMemory.variable>
(seenoise
for details).initializer (3d array or list : default None) – specifies an initial set of entries for
memory
(seeinitializer
for additional details).distance_field_weights (1d array : default None) – specifies the weight to use in computing the distance between each item of
variable
and the corresponding memory_field of each item inmemory
(seedistance_field_weights
for additional details).distance_function (Distance or function : default Distance(metric=COSINE)) – specifies the function used during retrieval to compare
variable
with entries inmemory
.selection_function (OneHot or function : default OneHot(mode=MIN_VAL)) – specifies the function used during retrieval to evaluate the distances returned by
distance_function
and select the item to retrieve.duplicate_entries_allowed (bool : default False) – specifies whether duplicate entries are allowed in
memory
(see duplicate entries for additional details).duplicate_threshold (float : default 0) – specifies how similar
variable
must be to an entry inmemory
based ondistance_function
to be considered a duplicate (see duplicate entries for additional details).equidistant_entries_select (RANDOM | OLDEST | NEWEST : default RANDOM) – specifies which entry in
memory
is chosen for retrieval if two or more have the same distance fromvariable
.max_entries (int : default None) – specifies the maximum number of entries allowed in
memory
(seemax_entries
for additional details).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
PreferenceSet
for the Function (seeprefs
for details).
- storage_prob¶
probability of adding
variable
tomemory
.Note
storage_prob does not apply to
initializer
, the entries of which are added tomemory
irrespective of storage_prob.- Type
float in interval [0,1]
- rate¶
value applied multiplicatively to
variable
) before storing in`memory <ContentAddressableMemory.memory>` (seerate
for additional details).- Type
float or 1d array
- noise¶
value added to
variable
) before storing inmemory
(seenoise
for additional details). If a 2d array (or Function that returns one), its shape must be the same asvariable
; that is, each array in the outer dimension (axis 0) must have the same length as the corresponding one invariable
, so that it can be added Hadamard style tovariable
before storing it inmemory
.- Type
float, 2d array or Function
- initializer¶
initial set of entries for
memory
. It should be either a 3d regular array or a 2d ragged array (if the fields of an entry have different lengths), but it can be specified in the initializer argument of the constructor using some simpler forms for convenience. Specifically, scalars, 1d and regular 2d arrays are allowed, which are interpreted as a single entry that is converted to a 3d array to initializememory
.- Type
ndarray
- memory¶
list of entries in ContentAddressableMemory, each of which is an array of fields containing stored items; the fields of an entry must be lists or arrays, each of which can be different shapes, but the corresponding fields of all entries must have the same shape; for example, the following could be a pair of entries in memory:
entry 1
entry 2
field1
field2
field3
field1
field2
field3
[[ [a],
[b,c,d],
[[e],[f]] ],
[ [u],
[v,w,x],
[[y],[z]] ]]
- Type
list
- distance_field_weights¶
determines the weight used in computing the distance between each item of
variable
and the corresponding memory_field of each entry inmemory
; if all elements are identical, it is treated as a scalar coefficient ondistance
(seeContentAddressableMemory_Distance_Field_Weights
for additional details).- Type
1d array : default None
- distance_function¶
function used during retrieval to compare
variable
with entries inmemory
.- Type
Distance or function : default Distance(metric=COSINE)
- distance¶
contains distance used for retrieval last cue to last entry returned in a given context.
- Type
float : default 0
- distances_by_field¶
contains array of distances between each
memory field
of the last cue and the corresponding ones of the last entry returned in a given context.- Type
array : default [0]
- distances_to_entries¶
contains array of distances between last cue retrieved in a given context an all entries at that time.
- Type
array : default [0]
- memory_num_fields¶
contains the number of memory fields in each entry of
memory
.- Type
int
- memory_field_shapes¶
contains the shapes of each memory field in each entry of
memory
.- Type
array
- selection_function¶
function used during retrieval to evaluate the distances returned by
distance_function
and select the item(s) to return.- Type
OneHot or function
- selection_type¶
indicates whether
selection_function
returns a single item (e.g., the default:OneHot
using MIN_INDICATOR for itsmode
attribute) or a weighted sum over the items in memory (e.g., usingSoftMax
with ALL, its default, as itsoutput
along each field, weighted by the corresponding element ofdistance_field_weights
.This attribute is assigned by evaluating the
selection_function
in the ContentAddressableMemory’s _instantiate_attributes_before_function method- Type
SINGLE | WEIGHTED
- duplicate_entries_allowed¶
determines whether duplicate entries are allowed in
memory
, as evaluated bydistance_function
andduplicate_threshold
. (see duplicate entries for additional details).- Type
bool | OVERWRITE
- duplicate_threshold¶
determines how similar
variable
must be to an entry inmemory `
based ondistance_function
to be considered a duplicate (see duplicate entries for additional details).- Type
float
- equidistant_entries_select¶
determines which entry is retrieved when duplicate entries are identified or are indistinguishable by the
distance_function
.- Type
RANDOM | OLDEST | NEWEST
- max_entries¶
maximum number of entries allowed in
memory
; if storing a memory exceeds the number, the oldest memory is deleted.- Type
int
- 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
PreferenceSet
for function; if it is not specified in the prefs argument of the Function’s constructor, a default is assigned usingclassPreferences
defined in __init__.py (see Preferences for details).- Type
PreferenceSet or specification dict : Function.classPreferences
- Returns
entry from `memory <ContentAddressableMemory.memory>` that best matches `variable <ContentAddressableMemory.variable>` – if no retrieval occurs, an appropriately shaped zero-valued array is returned.
- Return type
2d array
- _update_default_variable(new_default_variable, context=None)¶
Override method on parent (StatefulFunction) since it can’t handle arbitrarily-shaped fields
- reset(<new_dictionary> default={})¶
Clears the memory in
previous_value
.If new_value is passed into reset or if the
initializer
attribute contains a value besides [], then that value is used to start the new memory inprevious_value
. Otherwise, the newprevious_value
memory starts out as None.value
takes on the same value asprevious_value
.
- get_memory(cue, field_weights=None, context=None)¶
Retrieve entry from
memory
based ondistance_function
andselection_function
.- Parameters
cue (list or 2d array) – must have same number and shapes of fields as existing entries in
memory
.- Returns
entry retrieved – if no retrieval occurs, returns appropriately shaped zero-valued array.
- Return type
2d array
- add_to_memory(entries, context=None)¶
Add one or more entries into
memory
- Parameters
entries (list or array) – a single entry (list or array) or list or array of entries, each of which must be a valid entry; each must have the same number of and shapes of corresponding fields; items are added to memory in the order listed.
- delete_from_memory(entries, fields=None, context=None)¶
Delete one or more entries from
memory
- Parameters
memories (list or array) – a single entry (list or 2d array) or list or array of entries, each of which must be a valid entry (i.e. same number of fields and shapes of each as entries already in
memory
.fields (int or list : default None) – if None, delete all entries in
memory
that are identical to any of the memories specified; if int or list, delete all entries with the same values as those in the field(s) specified.
- store(entry, context=None, **kwargs)¶
Store value in
memory
. Convenience method for storing entry in memory.
- retrieve(entry, context=None, **kwargs)¶
Retrieve value from
memory
. Convenience method for retrieving entry from memory.
- property memory¶
Return entries in self._memory as lists in an outer np.array; use np.array for multi-line printout
- property memory_num_entries¶
Return number of entries in self._memory.