nef.Network

class nef.Network(name, quick=None, seed=None, fixed_seed=None)

Wraps a Nengo network with a set of helper functions for simplifying the creation of Nengo models.

This system is meant to allow short, concise code to create Nengo models. For example, we can make a communication channel like this:

import nef
net=nef.Network('Test Network')
input=net.make_input('input',values=[0])
A=net.make('A',neurons=100,dimensions=1)
B=net.make('B',neurons=100,dimensions=1)
net.connect(input,A)
net.connect(A,B)
net.add_to_nengo()

This will automatically create the necessary origins, terminations, ensemble factories, and so on needed to create this network.

Parameters:
  • name (string or NetworkImpl) – If a string, create and wrap a new NetworkImpl with the given name. If an existing NetworkImpl, then create a wrapper around that network.
  • quick (boolean) – Default setting for the quick parameter in nef.Network.make().
  • fixed_seed (int) – random number seed to use for creating ensembles. Every ensemble will use this seed value, resulting in identical neurons in ensembles that have the same parameters. By default, setting this parameter will enable quick mode.
  • seed (int) – random number seed to use for creating ensembles. This one seed is used only to start the random generation process, so each neural group created will be different (unlike the fixed_seed parameter). By default, setting this parameter will enable quick mode.
make(name, neurons, dimensions, tau_rc=0.02, tau_ref=0.002, max_rate=(200, 400), intercept=(-1, 1), radius=1, encoders=None, decoder_noise=0.10000000000000001, eval_points=None, noise=None, noise_frequency=1000, mode='spike', add_to_network=True, node_factory=None, decoder_sign=None, seed=None, quick=None, storage_code='')

Create and return an ensemble of neurons.

Parameters:
  • name (string) – name of the ensemble (must be unique)
  • neurons (integer) – number of neurons in the ensemble
  • dimensions (integer) – number of dimensions to represent
  • tau_rc (float) – membrane time constant
  • tau_ref (float) – refractory period
  • max_rate (tuple or list) – range for uniform selection of maximum firing rate in Hz (as a 2-tuple) or a list of maximum rate values to use
  • intercept (tuple or list) – normalized range for uniform selection of tuning curve x-intercept (as 2-tuple) or a list of intercept values to use (intercepts are defined with respect to encoders, so an encoder of -1 and intercept of .3 will result in a neuron only active below -.3)
  • radius (float) – representational range
  • encoders (list) – list of encoder vectors to use (if None, uniform distribution around unit sphere). The provided encoders will be automatically normalized to unit length.
  • decoder_noise (float) – amount of noise to assume when calculating decoders
  • eval_points (list) – list of points to do optimization over
  • noise (float) – current noise to inject, chosen uniformly from (-noise,noise)
  • noise_frequency (float) – sampling rate (how quickly the noise changes)
  • mode (string) – simulation mode (‘direct’, ‘rate’, or ‘spike’)
  • node_factory (ca.nengo.model.impl.NodeFactory) – a factory to use instead of the default LIF factory (for creating ensembles with neurons other than LIF)
  • decoder_sign (None, +1, or -1) – +1 for positive decoders, -1 for negative decoders. Set to None to allow both.
  • quick (boolean or None) – if True, saves data from a created ensemble and will re-use it in the future when creating an ensemble with the same parameters as this one. If None, uses the Network default setting. Note: the use of this parameter is not encouraged any more: use seed=<number> or fixed_seed=<number> in the Network constructor instead.
  • seed (int) – random number seed to use. Will be passed to both random.seed() and ca.nengo.math.PDFTools.setSeed(). If this is None and the Network was constructed with a seed parameter, a seed will be randomly generated.
  • storage_code (string) – an extra parameter to allow different quick files even if all other parameters are the same
  • add_to_network (boolean) – flag to indicate if created ensemble should be added to the network
Returns:

the newly created ensemble

make_array(name, neurons, length, dimensions=1, **args)

Create and return an array of ensembles. This acts like a high-dimensional ensemble, but actually consists of many sub-ensembles, each one representing a separate dimension. This tends to be much faster to create and can be more accurate than having one huge high-dimensional ensemble. However, since the neurons represent different dimensions separately, we cannot compute nonlinear interactions between those dimensions.

Note

When forming neural connections from an array to another ensemble (or another array), any specified function to be computed with be computed on each ensemble individually (with the results concatenated together). For example, the following code creates an array and then computes the sum of the squares of each value within it:

net=nef.Network('Squaring Array')
input=net.make_input('input',[0,0,0,0,0])
A=net.make_array('A',neurons=100,length=5)
B=net.make('B',neurons=100,dimensions=1)
net.connect(input,A)
def square(x):
  return x[0]*x[0]
net.connect(A,B,transform=[1,1,1,1,1],func=square)

All of the parameters from nef.Network.make() can also be used.

If the storage_code parameter is used, you may use %d (or variants such as
%02d) in the storage code, which will be replaced by the index number of the ensemble in the array. Thus, storage_code='a%02d' will become a00 for the first ensemble, a01 for the second, a02 for the third, and so on.

If the encoders parameter is used, you can provide either the standard array of encoders (e.g. [[1],[-1]]) or a list of sets of encoders for each ensemble (e.g. [[[1]],[[-1]]]).

If the dimensions parameter is used, each ensemble represents the specified number of dimensions (default value is 1 dimension per ensemble). For example, if length=5 and dimensions=2, then a total of 10 dimensions are represented by the network array. The index number of the first ensemble’s first dimension is 0, the index of the first ensemble’s second dimension is 1, the index of the second ensemble’s first dimension is 2, and so on.

Parameters:
  • name (string) – name of the ensemble array (must be unique)
  • neurons (integer) – number of neurons in each ensemble
  • length (integer) – number of ensembles in the array
  • dimensions (integer) – number of dimensions each ensemble represents
Returns:

the newly created ca.nengo.model.impl.NetworkArrayImpl

make_input(name, values, zero_after_time=None)

Create and return a FunctionInput of dimensionality len(values) with values as its constants. Python functions can be provided instead of fixed values.

Parameters:
  • name (string) – name of created node
  • values (list, function, string, or dict) – numerical values for the function. If a list, can contain a mixture of floats and functions (floats are fixed input values, and functions are called with the current time and must return a single float). If values is a function, will be called with the current time and can return either a single float or a list of floats. If a string, will be treated as a filename of a csv file with the first number in each row indicating the time and the other numbers giving the value of the function. If a dictionary, the keys,value pairs in the dictionary will be treated as time,value pairs.
  • zero_after_time (float or None) – if not None, any fixed input value will change to 0 after this amount of time
Returns:

the created FunctionInput

make_fourier_input(name, dimensions=None, base=1, high=10, power=0.5, seed=None)

Create and return a FunctionInput that randomly varies. The variation is generated by randomly generating fourier components with frequencies that are multiples of base up to high, and normalized to have an rms power of power.

Parameters:
  • name (string) – name of created node
  • dimensions (int or None) – dimensionality of the input. If None, will default to the longest of any of the lists given in the other parameters, or 1 if there are no lists.
  • base (float or list) – fundamental (lowest) frequency for the fourier series. If a list, will use different values for each dimension. Default is 1Hz
  • high (float or list) – maximum frequency for the fourier series. If a list, will use different values for each dimension. Default is 10Hz
  • power (float or list) – RMS power for the random function. If a list, will use different values for each dimension. Default is 0.5
  • seed (int or list or None.) – random number seed to use. If a list, will use different values for each dimension. If None, a random seed will be chosen.
Returns:

the created FunctionInput

make_subnetwork(name)

Create and return a subnetwork. Subnetworks are just Network objects that are inside other Networks, and are useful for keeping a model organized.

Parameters:name (string) – name of created node
Returns:the created Network
compute_transform(dim_pre, dim_post, weight=1, index_pre=None, index_post=None)

Helper function used by nef.Network.connect() to create a dim_pre by dim_post matrix. All values are either 0 or weight. index_pre and index_post are used to determine which values are non-zero, and indicate which dimensions of the pre-synaptic ensemble should be routed to which dimensions of the post-synaptic ensemble.

For example, with dim_pre=2 and dim_post=3, index_pre=[0,1],index_post=[0,1] means to take the first two dimensions of pre and send them to the first two dimensions of post, giving a transform matrix of [[1,0],[0,1],[0,0]] If an index is None, the full range [0,1,2,...,N] is assumed, so the above example could just be index_post=[0,1]

Parameters:
  • dim_pre (integer) – first dimension of transform matrix
  • dim_post (integer) – second dimension of transform matrix
  • weight (float) – the non-zero value to put into the matrix
  • index_pre (list of integers or a single integer) – the indexes of the pre-synaptic dimensions to use
  • index_post (list of integers or a single integer) – the indexes of the post-synaptic dimensions to use
Returns:

a two-dimensional transform matrix performing the requested routing

connect(pre, post, transform=None, weight=1, index_pre=None, index_post=None, pstc=0.01, func=None, weight_func=None, expose_weights=False, origin_name=None, modulatory=False, plastic_array=False, create_projection=True, encoders=None)

Connect two nodes in the network.

pre and post can be strings giving the names of the nodes, or they can be the nodes themselves (FunctionInputs and NEFEnsembles are supported). They can also be actual Origins or Terminations, or any combination of the above. If post is set to an integer or None, an origin will be created on the pre population, but no other action will be taken.

pstc is the post-synaptic time constant of the new Termination

If transform is not None, it is used as the transformation matrix for the new termination. You can also use weight, index_pre, and index_post to define a transformation matrix instead. weight gives the value, and index_pre and index_post identify which dimensions to connect (see nef.Network.compute_transform() for more details). For example:

net.connect(A,B,weight=5)

with both A and B as 2-dimensional ensembles, will use [[5,0],[0,5]] as the transform. Also, you can do:

net.connect(A,B,index_pre=2,index_post=5)

to connect the 3rd element in A to the 6th in B. You can also do:

net.connect(A,B,index_pre=[0,1,2],index_post=[5,6,7])

to connect multiple elements.

If func is not None, a new Origin will be created on the pre-synaptic ensemble that will compute the provided function. The name of this origin will taken from the name of the function, or origin_name, if provided. If an origin with that name already exists, the existing origin will be used rather than creating a new one.

func can also be an matrix of values of the same length as the eval_points provided to the make function for the pre population. This uses the data as a lookup table. For example, we can compute XOR using the following:

net.make('A', 50, 2, eval_points=[[-1,-1], [-1, 1], [1, -1], [1, 1]])
net.make('B', 50, 1)
net.connect('A', 'B', func=[[1], [-1], [-1], [1]])

If weight_func is not None, the connection will be made using a synaptic connection weight matrix rather than a DecodedOrigin and a Decoded Termination. The computed weight matrix will be passed to the provided function, which is then free to modify any values in that matrix, returning a new one that will actually be used. This allows for direct control over the connection weights, rather than just using the once computed via the NEF methods. If you do not want to modify these weights, but do want Nengo to compute the weight matrix, you can just set expose_weights to True.

Parameters:
  • pre – The item to connect from. Can be a string (the name of the ensemble), an Ensemble (made via nef.Network.make()), an array of Ensembles (made via nef.Network.make_array()), a FunctionInput (made via nef.Network.make_input()), or an Origin.
  • post – The item to connect to. Can be a string (the name of the ensemble), an Ensemble (made via nef.Network.make()), an array of Ensembles (made via nef.Network.make_array()), or a Termination.
  • transform (array of floats) – The linear transfom matrix to apply across the connection. If transform is T and pre represents x, then the connection will cause post to represent Tx. Should be an N by M array, where N is the dimensionality of post and M is the dimensionality of pre, but a 1-dimensional array can be given if either N or M is 1.
  • pstc (float) – post-synaptic time constant for the neurotransmitter/receptor implementing this connection
  • weight (float) – scaling factor for a transformation defined with index_pre and index_post. Ignored if transform is not None. See nef.Network.compute_transform()
  • index_pre (list of integers or a single integer) – the indexes of the pre-synaptic dimensions to use. Ignored if transform is not None. See nef.Network.compute_transform()
  • index_post (list of integers or a single integer) – the indexes of the post-synaptic dimensions to use. Ignored if transform is not None. See nef.Network.compute_transform()
  • func (function) –

    function to be computed by this connection. If None, computes f(x)=x. The function takes a single parameter x which is the current value of the pre ensemble, and must return wither a float or an array of floats. For example:

    def square(x):
        return x[0]*x[0]
    net.connect(A,B,func=square)
    
    def powers(x):
        return x[0],x[0]^2,x[0]^3
    net.connect(A,B,func=powers)
    
    def product(x):
        return x[0]*x[1]
    net.connect(A,B,func=product)
    
  • origin_name (string) – The name of the origin to create to compute the given function. Ignored if func is None. If an origin with this name already exists, the existing origin is used instead of creating a new one.
  • weight_func (function or None) – if not None, converts the connection to use an explicit connection weight matrix between each neuron in the ensembles. This is mathematically identical to the default method (which simply uses the stored encoders and decoders for the ensembles), but much slower, since we are no longer taking advantage of the factorable weight matrix. However, using weight_func also allows explicit control over the individual connection weights, as the computed weight matrix is passed to weight_func, which can make changes to the matrix before returning it. If weight_func is a function taking one argument, it is passed the calculated weight matrix. If it is a function taking two arguments, it is passed the encoder and decoder.
  • expose_weights – if True, set weight_func to the identity function. This makes the connection use explicit connection weights, but doesn’t modify them in any way. Ignored if weight_func is not None.
  • modulatory (boolean) – whether the created connection should be marked as modulatory, meaning that it does not directly affect the input current to the neurons, but instead may affect internal parameters in the neuron model.
  • plastic_array (boolean) – configure the connection to be learnable. See nef.Network.learn().
  • create_projection (boolean) – flag to disable actual creation of the connection. If False, any needed Origin and/or Termination will be created, and the return value will be the tuple (origin,termination) rather than the created projection object.
Returns:

the created Projection, or (origin,termination) if create_projection is False.

learn(post, learn_term, mod_term, rate=4.9999999999999998e-07, **kwargs)

Apply a learning rule to a termination of the post ensemble. The mod_term termination will be used as an error signal that modulates the changing connection weights of learn_term.

Parameters:
  • post (string or Ensemble) – the ensemble whose termination will be changing, or the name of this ensemble
  • learn_term (string or Termination) – the termination whose transform will be modified by the learning rule. This termination must be created with plastic_array=True in nef.Network.connect()
  • mod_term (string or Termination) – the modulatory input to the learning rule; this is optional. It will be checked if learn_term is a ModulatedPlasticEnsembleTermination, and not otherwise.
  • rate (float) –

    the learning rate that will be used in the learning fuctions.

    Todo

    (Possible enhancement: make this 2D for stdp mode, different rates for in_fcn and out_fcn)

If stdp is True, a triplet-based spike-timinng-dependent plasticity rule is used, based on that defined in:

Pfister, J. and Gerstner, W. (2006) Triplets of Spikes in a Model of Spike 
Timing-Dependent Plasticity. J. Neurosci., 26: 9673 - 9682.

The parameters for this learning rule have the following defaults, and can be set as keyword arguments to this function call:

(a2Minus=5.0e-3, a3Minus=5.0e-3, tauMinus=70, tauX=70,
 a2Plus=5.0e-3,  a3Plus=5.0e-3, tauPlus=70, tauY=70,
 decay=None, homeostatis=None)

If stdp is False, a rate-mode error minimizing learning rule is applied. The only parameter available here is whether or not to include an oja normalization term:

(oja=True)
learn_array(array, learn_term, mod_term, rate=4.9999999999999998e-07, **kwargs)

Apply a learning rule to a termination of a ca.nengo.model.impl.NetworkArrayImpl (an array of ensembles, created using nef.Network.make_array()).

See nef.Network.learn() for parameters.

add_to_nengo()

Add the network to the Nengo user interface. If there is no user interface (i.e. if Nengo is being run via the command line only interface nengo-cl), then do nothing.

add_to(world=None)

Add the network to the given Nengo world object. If there is a network with that name already there, remove the old one. If world is None, it will attempt to find a running version of Nengo to add to.

Deprecated since version 1.3: Use nef.Network.add_to_nengo() instead.

view(play=False)

Creates the interactive mode viewer for running the network

Parameters:play (False or float) – Automatically starts the simulation running, stopping after the given amount of time
set_layout(view, layout, control)

Defines the graphical layout for the interactive plots

You can use this to specify a particular layout. This will replace the currently saved layout (if any). Useful when running a script on a new computer that does not have a previously saved layout (saving you from also copying over that layout file).

The arguments for this function call are generally made by opening up interacive plots, making the layout you want, saving the layout, and then copying the text in layouts/<networkname>.layout.

Parameters:
  • view (dictionary) – parameters for the window position
  • layout (list) – list of all components to be shown and their parameters
  • control (dictionary) – configuration parameters for the simulation
add(node)

Add the node to the network.

This is generally only used for manually created nodes, not ones created by calling nef.Network.make() or nef.Network.make_input() as these are automatically added. A common usage is with nef.SimpleNode objects, as in the following:

node=net.add(MyNode('name'))
Parameters:node – the node to be added
Returns:node
remove(node)

Remove nodes from a network. Either the node object or the node name can be used as a parameter to this function

Parameters:node – the node or name of the node to be removed
Returns:node removed
get(name, default=<type 'exceptions.Exception'>, require_origin=False)

Return the node with the given name from the network

set_alias(alias, node)

Adds a named shortcut to an existing node within this network to be used to simplify connect() calls.

For example, you can do:

net.set_alias('vision','A.B.C.D.E')
net.set_alias('motor','W.X.Y.Z')
net.connect('vision','motor')            
releaseMemory()

Attempt to release extra memory used by the Network. Call only after all connections are made.

getNeuronCount()

Return the total number of neurons in this network

run(time, dt=0.001)

Run the simulation.

If called twice, the simulation will continue for time more seconds. To reset the simulation, call nef.Network.reset(). Typical use cases are to either simply call it once:

net.run(10)

or to call it multiple times in a row:

t=0
dt=0.1
while t<10:
   net.run(dt)
   t+=dt 
Parameters:
  • time (float) – the amount of time (in seconds) to run for
  • dt (float) – the size of the time step to use
reset()

Reset the simulation.

Should be called if you have previously called nef.Network.run(), but now want to reset the simulation to its initial state.

log(name=None, dir=None, filename='%(name)s-%(time)s.csv', interval=0.001, tau=0.01, auto_flush=True)

Creates a nef.Log object which dumps data to a .csv file as the model runs.

See the nef.Log documentation for details.

Parameters:
  • name (string) – The name of the model. Defaults to the name of the Network object.
  • dir (string) – The directory to place the .csv file into
  • filename (string) – The filename to use. .csv will be added if it is not already there. Can use %(name)s to refer to the name of the model and %(time)s to refer to the start time of the model. Defaults to %(name)s-%(time)s.csv.
  • interval (float) – The time interval between each row of the log. Defaults to 0.001 (1ms).
  • tau (float) – The default filter time for data. Defaults to 0.01 (10ms).
set_view_function_1d(node, basis, label='1D function', minx=-1, maxx=1, miny=-1, maxy=1)

Define a function representation for the given node.

This has no effect on the model itself, but provides a useful display in the interactive plots visualizer. The vector represented by the function is plotted by treating the vector values as weights for a set of basis functions. So, if a vector is (2,0,3) and the basis functions are x^2, x, and 1, we get the polynomial 2*x^2+3.

The provided basis function should accept two parameters: and index value indicating which basis function should be computed, and x, indicating the x value to compute the basis function at. For example, for polinomials, the basis functions would be computed as:

def polynomial_basis(index,x):
     return x**index
Parameters:
  • node (Node) – The Nengo component that represents the vector
  • basis (function) – The set of basis functions to use. This is a single function accepting two parameters: the basis index and the x value. It should return the corresponding y value.
  • label (string) – The text that will appear in the pop-up menu to activate this view
  • minx (float) – minimum x value to plot
  • maxx (float) – maximum x value to plot
  • miny (float) – minimum y value to plot
  • maxy (float) – maximum y value to plot

Nengo User Manual

Table Of Contents

This Page