API reference¶
This section details the modules, classes, and functions available in NengoDL. It is divided into two sections. The first section describes the objects relevant to NengoDL users. More information on these objects can also be found in the User guide. The second section describes objects that only NengoDL developers need to worry about.
Users¶
These objects are the main access points for the user-facing features of NengoDL.
Simulator¶
The Simulator class is the access point for the main features of NengoDL,
including running and training
a model.
Simulate network using the |
|
Resets the simulator to initial conditions. |
|
Deprecated, use |
|
Generate output predictions for the input samples. |
|
Generate output predictions for a single minibatch of input samples. |
|
Configure the model for training/evaluation. |
|
Trains the model on some dataset. |
|
Compute the loss and metric values for the network. |
|
Run the simulation for one time step. |
|
Run the simulation for the given length of time. |
|
Run the simulation for the given number of steps. |
|
Deprecated, use |
|
Deprecated, use |
|
Save network parameters to the given |
|
Load network parameters from the given |
|
Stores the live parameter values from the simulation back into a Nengo object definition. |
|
Extract model parameters in a form that can be used to initialize Nengo objects in a different model. |
|
Perform gradient checks for the network (used to verify that the analytic gradients are correct). |
|
Create a vector of simulation step times matching probed data. |
|
Close the simulation, freeing resources. |
|
Returns the standardized string name for input Nodes or output Probes. |
Data structure used to access simulation data from the model. |
-
class
nengo_dl.Simulator(network, dt=0.001, seed=None, model=None, device=None, unroll_simulation=1, minibatch_size=None, progress_bar=True)[source]¶ Simulate network using the
nengo_dlbackend.- Parameters
- network
nengo.Network A network object to be built and then simulated.
- dtfloat
Length of a simulator timestep, in seconds.
- seedint
Seed for all stochastic operators used in this simulator.
- model
Model Pre-built model object (mainly used for debugging).
- deviceNone or
"/cpu:0"or"/gpu:[0-n]" This specifies the computational device on which the simulation will run. The default is
None, which means that operations will be assigned according to TensorFlow’s internal logic (generally speaking, this means that things will be assigned to the GPU if GPU support is available, otherwise everything will be assigned to the CPU). The device can be set manually by passing the TensorFlow device specification to this parameter. For example, settingdevice="/cpu:0"will force everything to run on the CPU. This may be worthwhile for small models, where the extra overhead of communicating with the GPU outweighs the actual computations. On systems with multiple GPUs,device="/gpu:0"/"/gpu:1"/etc. will select which one to use.- unroll_simulationint
This controls how many simulation iterations are executed each time through the outer simulation loop. That is, we could run 20 timesteps as
for i in range(20): <run 1 step>
or
for i in range(5): <run 1 step> <run 1 step> <run 1 step> <run 1 step>
This is an optimization process known as “loop unrolling”, and
unroll_simulationcontrols how many simulation steps are unrolled. The first example above would correspond tounroll_simulation=1, and the second would beunroll_simulation=4.Unrolling the simulation will result in faster simulation speed, but increased build time and memory usage.
In general, unrolling the simulation will have no impact on the output of a simulation. The only case in which unrolling may have an impact is if the number of simulation steps is not evenly divisible by
unroll_simulation. In that case extra simulation steps will be executed, which could change the internal state of the simulation and will affect any subsequent calls tosim.run. So it is recommended that the number of steps always be evenly divisible byunroll_simulation.- minibatch_sizeint
The number of simultaneous inputs that will be passed through the network. For example, a single call to
Simulator.runwill processminibatch_sizeinput instances in parallel. Or when callingSimulator.predict/Simulator.fitwith a batch of data, that data will be divided up intominibatch_sizechunks.- progress_barbool
If True (default), display progress information when building a model. This will also be the default for the
progress_barargument withinSimulator.runandSimulator.run_steps.
- network
- Attributes
- data
SimulationData Stores simulation data and parameter values (in particular, the recorded output from probes after calling
Simulator.runcan be accessed throughsim.data[my_probe]).- model
nengo.builder.Model Built Nengo model, containing the data that defines the network to be simulated.
- keras_model
tf.keras.Model Keras Model underlying the simulation (implements the inference/training loops).
- tensor_graph
tensor_graph.TensorGraph Keras Layer implementing the Nengo simulation (built into
keras_model).
- data
-
reset(seed=None, include_trainable=True, include_probes=True, include_processes=True)¶ Resets the simulator to initial conditions.
- Parameters
- seedint
If not None, overwrite the default simulator seed with this value (note: this becomes the new default simulator seed).
- include_trainablebool
If True (default), also reset any online or offline training that has been performed on simulator parameters (e.g., connection weights).
- include_probesbool
If True (default), also clear probe data.
- include_processes: bool
If True (default), also reset all
nengo.Processobjects in the model.
Notes
Changing the TensorFlow seed only affects ops created from then on; it has no impact on existing ops (either changing their seed or resetting their random state). So calling
Simulator.resetwill likely have no impact on any TensorFlow randomness (it will still affect numpy randomness, such as in anengo.Process, as normal).
-
soft_reset(include_trainable=False, include_probes=False)¶ Deprecated, use
Simulator.resetinstead.
-
predict(x=None, n_steps=None, stateful=False, **kwargs)[source]¶ Generate output predictions for the input samples.
Computation is (optionally) done in batches.
This function implements the tf.keras.Model.predict API.
- Parameters
- x
Data for input Nodes in the model. This argument is optional; if it is not specified, then data will automatically be generated according to the inputs specified in the Node definitions (e.g., by calling the output function associated with that Node).
xcan be specified as:A dictionary of {
nengo.Nodeor str:numpy.ndarray} indicating the input values for the given nodes. Nodes can be referred to by the Node object itself or by a string name, which will beNode.labelif one was specified or"node"(duplicate names will have a number appended, corresponding to the order found innengo.Network.all_nodes).A list of
numpy.ndarrayindicating the input values for each input Node, ordered according to the order in which the Nodes were added to the model (this corresponds to the order found innengo.Network.all_nodes).A
numpy.ndarrayindicating the input value for a single input Node.A generator or
tf.data.Datasetthat produces one of the above.
All inputs should have shape
(batch_size, n_steps, node.size_out).For example, if the model only has a single input Node, then
xcan simply be an ndarray of data for that Node.with nengo.Network() as net: a = nengo.Node([0]) p = nengo.Probe(a) with nengo_dl.Simulator(net) as sim: sim.predict( x=np.ones((50, 10, 1)))
If the network has multiple inputs, then
xcan be specified as a dictionary mappingnengo.Nodeobjects to arrays, e.g.with nengo.Network() as net: a = nengo.Node([0]) b = nengo.Node([0, 0]) p = nengo.Probe(a) with nengo_dl.Simulator(net) as sim: sim.predict( x={ a: np.ones((50, 10, 1)), b: np.ones((50, 10, 2)) } )
If an input value is not specified for one of the Nodes in the model then data will be filled in automatically according to the Node definition.
For dynamic input types (e.g.,
tf.datapipelines or generators), NengoDL tries to avoid introspecting/altering the data before the simulation starts, as this may have unintended side-effects. So data must be specified via one of the standard Keras methods (arrays, list of arrays, or string name dictionary; using a dictionary of Node objects is not supported). In addition, data must be explicitly provided for all input nodes (it will not be automatically generated if data is not specified).In addition, when using dynamic inputs, data must be provided for the special
"n_steps"input. This specifies the number of timesteps that the simulation will run for. Technically this is just a single scalar value (e.g.,10). But Keras requires that all input data be batched, so that input value needs to be duplicated into an array with size(batch_size, 1)(where all entries have the same value, e.g.10).with nengo.Network() as net: a = nengo.Node([0], label="a") p = nengo.Probe(a, label="p") with nengo_dl.Simulator(net) as sim: dataset = tf.data.Dataset.from_tensor_slices( {"a": tf.ones((50, 10, 1)), "n_steps": tf.ones((50, 1), dtype=tf.int32) * 10} ).batch(sim.minibatch_size) sim.predict(x=dataset)
- n_stepsint
The number of simulation steps to be executed. This parameter is optional; if not specified, the number of simulation steps will be inferred from the input data. However, this parameter can be useful if you don’t want to specify input data (you just want to use the inputs defined by the Nengo Nodes), or if your model does not have any input Nodes (so there is no data to be passed in).
- statefulbool
This parameter controls whether or not the saved internal stimulation state will be updated after a run completes. If
stateful=Falsethen the initial state of future runs will be unaffected by this run. Withstateful=True, future runs will begin from the terminal state of this run.For example,
# begins in state0, terminates in state1 sim.predict(..., stateful=False) # begins in state0, terminates in state2 sim.predict(..., stateful=True) # begins in state2, terminates in state3 sim.predict(..., stateful=False) # begins in state2, terminates in state4 sim.predict(..., stateful=True)
Note that
Simulator.resetcan be used to reset the state to initial conditions at any point.- kwargs: dict
Will be passed on to tf.keras.Model.predict.
- Returns
- probe_valuesdict of {
nengo.Probe:numpy.ndarray} Output values from all the Probes in the network.
- probe_valuesdict of {
-
predict_on_batch(x=None, n_steps=None, stateful=False, **kwargs)[source]¶ Generate output predictions for a single minibatch of input samples.
Batch size is determined by
sim.minibatch_size(i.e., inputs must have shape(sim.minibatch_size, n_steps, node.size_in).This function implements the tf.keras.Model.predict_on_batch API.
- Parameters
- x
Data for input Nodes in the model. This argument is optional; if it is not specified, then data will automatically be generated according to the inputs specified in the Node definitions (e.g., by calling the output function associated with that Node).
xcan be specified as:A dictionary of {
nengo.Nodeor str:numpy.ndarray} indicating the input values for the given nodes. Nodes can be referred to by the Node object itself or by a string name, which will beNode.labelif one was specified or"node"(duplicate names will have a number appended, corresponding to the order found innengo.Network.all_nodes).A list of
numpy.ndarrayindicating the input values for each input Node, ordered according to the order in which the Nodes were added to the model (this corresponds to the order found innengo.Network.all_nodes).A
numpy.ndarrayindicating the input value for a single input Node.
All inputs should have shape
(batch_size, n_steps, node.size_out).For example, if the model only has a single input Node, then
xcan simply be an ndarray of data for that Node.with nengo.Network() as net: a = nengo.Node([0]) p = nengo.Probe(a) with nengo_dl.Simulator(net) as sim: sim.predict_on_batch( x=np.ones((1, 10, 1)))
If the network has multiple inputs, then
xcan be specified as a dictionary mappingnengo.Nodeobjects to arrays, e.g.with nengo.Network() as net: a = nengo.Node([0]) b = nengo.Node([0, 0]) p = nengo.Probe(a) with nengo_dl.Simulator(net) as sim: sim.predict_on_batch( x={ a: np.ones((1, 10, 1)), b: np.ones((1, 10, 2)) } )
If an input value is not specified for one of the Nodes in the model then data will be filled in automatically according to the Node definition.
- n_stepsint
The number of simulation steps to be executed. This parameter is optional; if not specified, the number of simulation steps will be inferred from the input data. However, this parameter can be useful if you don’t want to specify input data (you just want to use the inputs defined by the Nengo Nodes), or if your model does not have any input Nodes (so there is no data to be passed in).
- statefulbool
This parameter controls whether or not the saved internal stimulation state will be updated after a run completes. If
stateful=Falsethen the initial state of future runs will be unaffected by this run. Withstateful=True, future runs will begin from the terminal state of this run.For example,
# begins in state0, terminates in state1 sim.predict_on_batch(..., stateful=False) # begins in state0, terminates in state2 sim.predict_on_batch(..., stateful=True) # begins in state2, terminates in state3 sim.predict_on_batch(..., stateful=False) # begins in state2, terminates in state4 sim.predict_on_batch(..., stateful=True)
Note that
Simulator.resetcan be used to reset the state to initial conditions at any point.- kwargs: dict
Will be passed on to tf.keras.Model.predict_on_batch.
- Returns
- probe_valuesdict of {
nengo.Probe:numpy.ndarray} Output values from all the Probes in the network.
- probe_valuesdict of {
-
compile(*args, loss=None, metrics=None, loss_weights=None, **kwargs)¶ Configure the model for training/evaluation.
- Parameters
- args
Will be passed on to tf.keras.Model.compile.
- loss
Loss functions define the error that will be minimized during training.
Losses can be specified as:
A tf.losses.Loss instance.
A string matching the name of one of the loss functions above.
A function that accepts two arguments (
y_true, y_pred) and returns a loss value (represented as atf.Tensor).A list of some combination of the above, indicating different loss functions for each output Probe (ordered according to the order in which Probes were added to the model, which corresponds to the order found in
Simulator.model.probes).A dictionary mapping Probe instances or names to loss functions.
The total loss minimized during training will be the sum over the loss computed on each Probe (possibly weighted by
loss_weights).For example,
with nengo.Network() as net: node0 = nengo.Node([0]) node1 = nengo.Node([0]) probe0 = nengo.Probe(node0) probe1 = nengo.Probe(node1) with nengo_dl.Simulator(net) as sim: sim.compile(loss={probe0: "mse", probe1: tf.losses.mae})
would compile
probe0to use mean squared error andprobe1to use mean absolute error.- metrics
Metrics are additional values (generally different kinds of losses) that will be computed during training for tracking purposes, but do not affect the result of the training.
They can be specified in all the same ways as
lossabove.In addition, multiple metrics can be specified for each output Probe when using a list or dict, by providing multiple functions in a list (e.g.,
metrics={my_probe: ["mae", "mse"]}).- loss_weightslist or dict
Scalar weights that will be applied to the loss value computed for each output probe before summing them to compute the overall training loss. Can be a list (order corresponding to the order in
loss) or a dict mapping Probe instances/names to weights.- kwargs
Will be passed on to tf.keras.Model.compile.
-
fit(x=None, y=None, n_steps=None, stateful=False, **kwargs)[source]¶ Trains the model on some dataset.
Note that if the model contains spiking neurons, during the execution of this function those neurons will be swapped for the equivalent non-spiking implementation (as opposed to, e.g.,
Simulator.evaluate, which will use the spiking implementation).Optimizer and loss functions are defined separately in
Simulator.compile.This function implements the tf.keras.Model.fit API.
- Parameters
- x
Data for input Nodes in the model. This argument is optional; if it is not specified, then data will automatically be generated according to the inputs specified in the Node definitions (e.g., by calling the output function associated with that Node).
xcan be specified as:A dictionary of {
nengo.Nodeor str:numpy.ndarray} indicating the input values for the given nodes. Nodes can be referred to by the Node object itself or by a string name, which will beNode.labelif one was specified or"node"(duplicate names will have a number appended, corresponding to the order found innengo.Network.all_nodes).A list of
numpy.ndarrayindicating the input values for each input Node, ordered according to the order in which the Nodes were added to the model (this corresponds to the order found innengo.Network.all_nodes).A
numpy.ndarrayindicating the input value for a single input Node.A generator or
tf.data.Datasetthat produces one of the above.
All inputs should have shape
(batch_size, n_steps, node.size_out).For example, if the model only has a single input Node, then
xcan simply be an ndarray of data for that Node.with nengo.Network() as net: a = nengo.Node([0]) p = nengo.Probe(a) with nengo_dl.Simulator(net) as sim: sim.compile(loss="mse") sim.fit( x=np.ones((50, 10, 1)), y=np.ones((50, 10, 1)))
If the network has multiple inputs, then
xcan be specified as a dictionary mappingnengo.Nodeobjects to arrays, e.g.with nengo.Network() as net: a = nengo.Node([0]) b = nengo.Node([0, 0]) p = nengo.Probe(a) with nengo_dl.Simulator(net) as sim: sim.compile(loss="mse") sim.fit( x={ a: np.ones((50, 10, 1)), b: np.ones((50, 10, 2)) }, y=np.ones((50, 10, 1)) )
If an input value is not specified for one of the Nodes in the model then data will be filled in automatically according to the Node definition.
For dynamic input types (e.g.,
tf.datapipelines or generators), NengoDL tries to avoid introspecting/altering the data before the simulation starts, as this may have unintended side-effects. So data must be specified via one of the standard Keras methods (arrays, list of arrays, or string name dictionary; using a dictionary of Node objects is not supported). In addition, data must be explicitly provided for all input nodes (it will not be automatically generated if data is not specified).In addition, when using dynamic inputs, data must be provided for the special
"n_steps"input. This specifies the number of timesteps that the simulation will run for. Technically this is just a single scalar value (e.g.,10). But Keras requires that all input data be batched, so that input value needs to be duplicated into an array with size(batch_size, 1)(where all entries have the same value, e.g.10).Also keep in mind that when using a dynamic input for
xtheyparameter is unused, and instead the generator should return(x, y)pairs.with nengo.Network() as net: a = nengo.Node([0], label="a") p = nengo.Probe(a, label="p") with nengo_dl.Simulator(net) as sim: dataset = tf.data.Dataset.from_tensor_slices( ({"a": tf.ones((50, 10, 1)), "n_steps": tf.ones((50, 1), dtype=tf.int32) * 10}, {"p": tf.ones((50, 10, 1))}) ).batch(sim.minibatch_size) sim.compile(loss="mse") sim.fit(x=dataset)
- y
Target values for Probes in the model. These can be specified in the same ways as the input values in
x, except using Probes instead of Nodes. All targets should have shape(batch_size, n_steps, probe.size_in).For example,
with nengo.Network() as net: a = nengo.Node([0]) p = nengo.Probe(a) with nengo_dl.Simulator(net) as sim: sim.compile(loss="mse") sim.fit( x={a: np.zeros((50, 10, 1))}, y={p: np.zeros((50, 10, 1))})
Note that data is only specified for the probes used in the loss function (specified when calling
Simulator.compile). For example, if we have two probes, but only one is used during training (the other is used for data collection during inference), we could set this up like:with nengo.Network() as net: a = nengo.Node([0]) b = nengo.Node([0]) p_a = nengo.Probe(a) p_b = nengo.Probe(b) with nengo_dl.Simulator(net) as sim: # compiled loss function only depends on p_a sim.compile(loss={p_a: "mse"}) # only specify data for p_a sim.fit( x={a: np.zeros((50, 10, 1))}, y={p_a: np.zeros((50, 10, 1))})
yis not used ifxis a generator. Instead, the generator passed toxshould yield(x, y)tuples, whereyis in one of the formats described above.- n_stepsint
The number of simulation steps to be executed. This parameter is optional; if not specified, the number of simulation steps will be inferred from the input data. However, this parameter can be useful if you don’t want to specify input data (you just want to use the inputs defined by the Nengo Nodes), or if your model does not have any input Nodes (so there is no data to be passed in).
- statefulbool
This parameter controls whether or not the saved internal stimulation state will be updated after a run completes. If
stateful=Falsethen the initial state of future runs will be unaffected by this run. Withstateful=True, future runs will begin from the terminal state of this run.For example,
# begins in state0, terminates in state1 sim.fit(..., stateful=False) # begins in state0, terminates in state2 sim.fit(..., stateful=True) # begins in state2, terminates in state3 sim.fit(..., stateful=False) # begins in state2, terminates in state4 sim.fit(..., stateful=True)
Note that
Simulator.resetcan be used to reset the state to initial conditions at any point.- kwargs: dict
Will be passed on to tf.keras.Model.fit.
- Returns
- history
tf.keras.callbacks.History The history has two attributes:
history.epochis the list of epoch numbers, andhistory.historyis a dictionary keyed by metric names (e.g., “loss”) containing a list of values of those metrics from each epoch.
- history
-
evaluate(x=None, y=None, n_steps=None, stateful=False, **kwargs)[source]¶ Compute the loss and metric values for the network.
Loss functions and other metrics are defined separately in
Simulator.compile.This function implements the tf.keras.Model.evaluate API.
- Parameters
- x
Data for input Nodes in the model. This argument is optional; if it is not specified, then data will automatically be generated according to the inputs specified in the Node definitions (e.g., by calling the output function associated with that Node).
xcan be specified as:A dictionary of {
nengo.Nodeor str:numpy.ndarray} indicating the input values for the given nodes. Nodes can be referred to by the Node object itself or by a string name, which will beNode.labelif one was specified or"node"(duplicate names will have a number appended, corresponding to the order found innengo.Network.all_nodes).A list of
numpy.ndarrayindicating the input values for each input Node, ordered according to the order in which the Nodes were added to the model (this corresponds to the order found innengo.Network.all_nodes).A
numpy.ndarrayindicating the input value for a single input Node.A generator or
tf.data.Datasetthat produces one of the above.
All inputs should have shape
(batch_size, n_steps, node.size_out).For example, if the model only has a single input Node, then
xcan simply be an ndarray of data for that Node.with nengo.Network() as net: a = nengo.Node([0]) p = nengo.Probe(a) with nengo_dl.Simulator(net) as sim: sim.compile(loss="mse") sim.evaluate( x=np.ones((50, 10, 1)), y=np.ones((50, 10, 1)))
If the network has multiple inputs, then
xcan be specified as a dictionary mappingnengo.Nodeobjects to arrays, e.g.with nengo.Network() as net: a = nengo.Node([0]) b = nengo.Node([0, 0]) p = nengo.Probe(a) with nengo_dl.Simulator(net) as sim: sim.compile(loss="mse") sim.evaluate( x={ a: np.ones((50, 10, 1)), b: np.ones((50, 10, 2)) }, y=np.ones((50, 10, 1)) )
If an input value is not specified for one of the Nodes in the model then data will be filled in automatically according to the Node definition.
For dynamic input types (e.g.,
tf.datapipelines or generators), NengoDL tries to avoid introspecting/altering the data before the simulation starts, as this may have unintended side-effects. So data must be specified via one of the standard Keras methods (arrays, list of arrays, or string name dictionary; using a dictionary of Node objects is not supported). In addition, data must be explicitly provided for all input nodes (it will not be automatically generated if data is not specified).In addition, when using dynamic inputs, data must be provided for the special
"n_steps"input. This specifies the number of timesteps that the simulation will run for. Technically this is just a single scalar value (e.g.,10). But Keras requires that all input data be batched, so that input value needs to be duplicated into an array with size(batch_size, 1)(where all entries have the same value, e.g.10).Also keep in mind that when using a dynamic input for
xtheyparameter is unused, and instead the generator should return(x, y)pairs.with nengo.Network() as net: a = nengo.Node([0], label="a") p = nengo.Probe(a, label="p") with nengo_dl.Simulator(net) as sim: dataset = tf.data.Dataset.from_tensor_slices( ({"a": tf.ones((50, 10, 1)), "n_steps": tf.ones((50, 1), dtype=tf.int32) * 10}, {"p": tf.ones((50, 10, 1))}) ).batch(sim.minibatch_size) sim.compile(loss="mse") sim.evaluate(x=dataset)
- y
Target values for Probes in the model. These can be specified in the same ways as the input values in
x, except using Probes instead of Nodes. All targets should have shape(batch_size, n_steps, probe.size_in).For example,
with nengo.Network() as net: a = nengo.Node([0]) p = nengo.Probe(a) with nengo_dl.Simulator(net) as sim: sim.compile(loss="mse") sim.evaluate( x={a: np.zeros((50, 10, 1))}, y={p: np.zeros((50, 10, 1))})
Note that data is only specified for the probes used in the loss function (specified when calling
Simulator.compile). For example, if we have two probes, but only one is used during training (the other is used for data collection during inference), we could set this up like:with nengo.Network() as net: a = nengo.Node([0]) b = nengo.Node([0]) p_a = nengo.Probe(a) p_b = nengo.Probe(b) with nengo_dl.Simulator(net) as sim: # compiled loss function only depends on p_a sim.compile(loss={p_a: "mse"}) # only specify data for p_a sim.evaluate( x={a: np.zeros((50, 10, 1))}, y={p_a: np.zeros((50, 10, 1))})
yis not used ifxis a generator. Instead, the generator passed toxshould yield(x, y)tuples, whereyis in one of the formats described above.- n_stepsint
The number of simulation steps to be executed. This parameter is optional; if not specified, the number of simulation steps will be inferred from the input data. However, this parameter can be useful if you don’t want to specify input data (you just want to use the inputs defined by the Nengo Nodes), or if your model does not have any input Nodes (so there is no data to be passed in).
- statefulbool
This parameter controls whether or not the saved internal stimulation state will be updated after a run completes. If
stateful=Falsethen the initial state of future runs will be unaffected by this run. Withstateful=True, future runs will begin from the terminal state of this run.For example,
# begins in state0, terminates in state1 sim.evaluate(..., stateful=False) # begins in state0, terminates in state2 sim.evaluate(..., stateful=True) # begins in state2, terminates in state3 sim.evaluate(..., stateful=False) # begins in state2, terminates in state4 sim.evaluate(..., stateful=True)
Note that
Simulator.resetcan be used to reset the state to initial conditions at any point.- kwargs: dict
Will be passed on to tf.keras.Model.evaluate.
- Returns
- outputsdict of {str:
numpy.ndarray} Computed loss/metric values. The overall loss will be in
outputs["loss"], and values for each Probe will be inoutputs["probe_name_loss"]oroutputs["probe_name_metric_name"].
- outputsdict of {str:
-
step(**kwargs)[source]¶ Run the simulation for one time step.
- Parameters
- kwargsdict
See
run_steps
Notes
Progress bar is disabled by default when running via this method.
-
run(time_in_seconds, **kwargs)[source]¶ Run the simulation for the given length of time.
- Parameters
- time_in_secondsfloat
Run the simulator for the given number of simulated seconds.
- kwargsdict
See
run_steps
-
run_steps(n_steps, data=None, progress_bar=None, stateful=True)[source]¶ Run the simulation for the given number of steps.
- Parameters
- n_stepsint
The number of simulation steps to be executed.
- data :
Data for input Nodes in the model. This argument is optional; if it is not specified, then data will automatically be generated according to the inputs specified in the Node definitions (e.g., by calling the output function associated with that Node).
datacan be specified as:A dictionary of {
nengo.Nodeor str:numpy.ndarray} indicating the input values for the given nodes. Nodes can be referred to by the Node object itself or by a string name, which will beNode.labelif one was specified or"node"(duplicate names will have a number appended, corresponding to the order found innengo.Network.all_nodes).A list of
numpy.ndarrayindicating the input values for each input Node, ordered according to the order in which the Nodes were added to the model (this corresponds to the order found innengo.Network.all_nodes).A
numpy.ndarrayindicating the input value for a single input Node.
All inputs should have shape
(batch_size, n_steps, node.size_out).For example, if the model only has a single input Node, then
datacan simply be an ndarray of data for that Node.with nengo.Network() as net: a = nengo.Node([0]) p = nengo.Probe(a) with nengo_dl.Simulator(net) as sim: sim.run_steps( 10, data=np.ones((1, 10, 1)))
If the network has multiple inputs, then
datacan be specified as a dictionary mappingnengo.Nodeobjects to arrays, e.g.with nengo.Network() as net: a = nengo.Node([0]) b = nengo.Node([0, 0]) p = nengo.Probe(a) with nengo_dl.Simulator(net) as sim: sim.run_steps( 10, data={ a: np.ones((1, 10, 1)), b: np.ones((1, 10, 2)) } )
If an input value is not specified for one of the Nodes in the model then data will be filled in automatically according to the Node definition.
- progress_barbool
If True, print information about the simulation status to standard output.
- statefulbool
This parameter controls whether or not the saved internal stimulation state will be updated after a run completes. If
stateful=Falsethen the initial state of future runs will be unaffected by this run. Withstateful=True, future runs will begin from the terminal state of this run.For example,
# begins in state0, terminates in state1 sim.run_steps(..., stateful=False) # begins in state0, terminates in state2 sim.run_steps(..., stateful=True) # begins in state2, terminates in state3 sim.run_steps(..., stateful=False) # begins in state2, terminates in state4 sim.run_steps(..., stateful=True)
Note that
Simulator.resetcan be used to reset the state to initial conditions at any point.
Notes
If
unroll_simulation=xis specified, andn_steps > x, this will repeatedly executextimesteps until the the number of steps executed is >=n_steps.
-
train(*args, **kwargs)[source]¶ Deprecated, use
Simulator.compileandSimulator.fitinstead.
-
loss(*args, **kwargs)[source]¶ Deprecated, use
Simulator.compileandSimulator.evaluateinstead.
-
save_params(path, include_state=False, include_non_trainable=None)¶ Save network parameters to the given
path.- Parameters
- pathstr
Filepath of parameter output file.
- include_statebool
If True (default False) also save the internal simulation state.
Changed in version 3.2.0: Renamed from
include_non_trainabletoinclude_state.
Notes
This function is useful for saving/loading entire models; for saving/loading individual objects within a model, see
get_nengo_params.
-
load_params(path, include_state=False, include_non_trainable=None)¶ Load network parameters from the given
path.- Parameters
- pathstr
Filepath of parameter input file.
- include_statebool
If True (default False) also save the internal simulation state.
Changed in version 3.2.0: Renamed from
include_non_trainabletoinclude_state.
Notes
This function is useful for saving/loading entire models; for saving/loading individual objects within a model, see
get_nengo_params.
-
freeze_params(objs)[source]¶ Stores the live parameter values from the simulation back into a Nengo object definition.
This can be helpful for reusing a NengoDL model inside a different Simulator. For example:
with nengo.Network() as net: ens = nengo.Ensemble(10, 1) with nengo_dl.Simulator(net) as sim: # < run some optimization > sim.freeze_params(net) with nengo.Simulator(net) as sim2: # run the network in the default Nengo simulator, with the # trained parameters sim2.run(1.0)
- Parameters
- obj(list of)
NengoObject The Nengo object(s) into which parameter values will be stored. Note that these objects must be members of the Network used to initialize the Simulator.
- obj(list of)
Notes
This modifies the source object in-place, and it may slightly modify the structure of that object. The goal is to have the object produce the same output as it would if run in the NengoDL simulator. It may not be possible to accurately freeze all possible object; if you run into errors in this process, try manually extracting the parameters you need in your model (from
sim.data).
-
get_nengo_params(nengo_objs, as_dict=False)[source]¶ Extract model parameters in a form that can be used to initialize Nengo objects in a different model.
For example:
with nengo.Network() as net: a = nengo.Ensemble(10, 1) b = nengo.Ensemble(10, 1) c = nengo.Connection(a, b) with nengo_dl.Simulator(net) as sim: # < do some optimization > params = sim.get_nengo_params([a, b, c]) with nengo.Network() as new_net: # < build some other network > # now we want to insert two connected ensembles with # the same parameters as our previous network: d = nengo.Ensemble(10, 1, **params[0]) e = nengo.Ensemble(10, 1, **params[1]) f = nengo.Connection(d, e, **params[2])
Note that this function only returns trainable parameters (e.g. connection weights, biases, or encoders), or parameters that directly interact with those parameters (e.g. gains). Other arguments that are independent of the trainable parameters (e.g.
Ensemble.neuron_typeorConnection.synapse) should be specified manually (since they may change between models).- Parameters
- nengo_objs(list of)
EnsembleorConnection A single object or list of objects for which we want to get the parameters.
- as_dictbool
If True, return the values as a dictionary keyed by object label, instead of a list (the default). Note that in this case labels must be unique.
- nengo_objs(list of)
- Returns
- params(list or dict) of dicts
kwarg dicts corresponding to
nengo_objs(passing these dicts as kwargs when creating new Nengo objects will result in a new object with the same parameters as the source object). A single kwarg dict if a single object was passed in, or a list (dict ifas_dict=True) of kwargs corresponding to multiple input objects.
-
check_gradients(inputs=None, outputs=None, atol=1e-05, rtol=0.001)¶ Perform gradient checks for the network (used to verify that the analytic gradients are correct).
Raises a simulation error if the difference between analytic and numeric gradient is greater than
atol + rtol * numeric_grad(elementwise).- Parameters
- inputslist of
numpy.ndarray Input values for all the input Nodes in the model (ordered according to the order in which Nodes were added to the model). If None, will use all zeros.
- outputslist of
Probe Compute gradients wrt this output (if None, computes wrt each output probe).
- atolfloat
Absolute error tolerance.
- rtolfloat
Relative (to numeric grad) error tolerance.
- inputslist of
Notes
Calling this method will reset the internal simulation state.
-
trange(sample_every=None, dt=None)[source]¶ Create a vector of simulation step times matching probed data.
Note that the range does not start at 0 as one might expect, but at the first timestep (i.e.,
dt).- Parameters
- sample_everyfloat (Default: None)
The sampling period of the probe to create a range for. If None, a time value for every
dtwill be produced.
-
close()[source]¶ Close the simulation, freeing resources.
Notes
The simulation cannot be restarted after it is closed.
-
get_name(obj)[source]¶ Returns the standardized string name for input Nodes or output Probes.
These are used when referring to inputs/outputs by string in Keras.
- Parameters
- obj
nengo.Nodeornengo.Probe Input Node or output Probe
- obj
- Returns
- namestr
Name of the given object
-
property
n_steps¶ The current simulation timestep.
-
property
time¶ The current simulation time.
-
property
seed¶ The simulation random seed.
-
class
nengo_dl.simulator.SimulationData(sim, minibatched)[source]¶ Data structure used to access simulation data from the model.
The main use case for this is to access Probe data; for example,
probe_data = sim.data[my_probe]. However, it is also used to access the parameters of objects in the model; for example, after the model has been optimized viaSimulator.fit, the updated encoder values for an ensemble can be accessed viatrained_encoders = sim.data[my_ens].encoders.- Parameters
- sim
Simulator The simulator from which data will be drawn
- minibatchedbool
If False, discard the minibatch dimension on probe data
- sim
Notes
SimulationData shouldn’t be created/accessed directly by the user, but rather via
sim.data(which is an instance of SimulationData).-
__getitem__(obj)[source]¶ Return the data associated with
obj.- Parameters
- obj
ProbeorEnsembleorConnection Object whose simulation data is being accessed
- obj
- Returns
- data
ndarrayorBuiltEnsembleorBuiltConnection Array containing probed data if
objis aProbe, otherwise the corresponding parameter object
- data
-
get_params(*obj_attrs)[source]¶ Returns the current parameter values for the given objects.
- Parameters
- obj_attrslist of (
NengoObject, str) The Nengo object and attribute of that object for which we want to know the parameter values (each object-attribute pair specified as a tuple argument to the function).
- obj_attrslist of (
- Returns
- paramslist of
ndarray Current values of the requested parameters
- paramslist of
Notes
Parameter values should be accessed through
sim.data[my_obj](which will call this function if necessary), rather than directly through this function.
TensorNodes¶
TensorNodes allow parts of a model to be defined using TensorFlow and smoothly integrated with the rest of a Nengo model.
See the documentation for more details.
Inserts TensorFlow code into a Nengo model. |
|
A wrapper for constructing TensorNodes. |
-
class
nengo_dl.TensorNode(tensor_func, shape_in=Default, shape_out=Default, pass_time=Default, label=Default)[source]¶ Inserts TensorFlow code into a Nengo model.
- Parameters
- tensor_funccallable
A function that maps node inputs to outputs
- shape_intuple of int
Shape of TensorNode input signal (not including batch dimension).
- shape_outtuple of int
Shape of TensorNode output signal (not including batch dimension). If None, value will be inferred by calling
tensor_func.- pass_timebool
If True, pass current simulation time to TensorNode function (in addition to the standard input).
- labelstr (Default: None)
A name for the node, used for debugging and visualization
-
property
output¶ Ensures that nothing tries to evaluate the
outputattribute (indicating that something is trying to simulate this as a regularnengo.Noderather than a TensorNode).
-
property
size_in¶ Number of input elements (flattened).
-
property
size_out¶ Number of output elements (flattened).
-
class
nengo_dl.Layer(layer_func)[source]¶ A wrapper for constructing TensorNodes.
This is designed to mimic and integrate with the
tf.keras.layers.LayerAPI, e.g.with nengo.Network(): a = nengo.Ensemble(10, 1) b = nengo_dl.Layer(tf.keras.layers.Dense(units=10))(a) c = nengo_dl.Layer(lambda x: x + 1)(b) d = nengo_dl.Layer(nengo.LIF())(c)
- Parameters
- layer_funccallable or
tf.keras.LayerorNeuronType A function or Keras Layer that takes the value from an input (represented as a
tf.Tensor) and maps it to some output value, or a Nengo neuron type (which will be instantiated in a Nengo Ensemble and applied to the input).
- layer_funccallable or
-
__call__(input, transform=None, shape_in=None, synapse=None, return_conn=False, **layer_args)[source]¶ Apply the TensorNode layer to the given input object.
- Parameters
- input
NengoObject Object providing input to the layer.
- transform
ndarray Transform matrix to apply on connection from
inputto this layer.- shape_intuple of int
If not None, reshape the input to the given shape.
- synapsefloat or
Synapse Synapse to apply on connection from
inputto this layer.- return_connbool
If True, also return the connection linking this layer to
input.- layer_argsdict
These arguments will be passed to
TensorNodeiflayer_funcis a callable or Keras Layer, orEnsembleiflayer_funcis aNeuronType.
- input
- Returns
- obj
TensorNodeorNeurons A TensorNode that implements the given layer function (if
layer_funcwas a callable/Keras layer), or a Neuron object with the given neuron type, connected toinput.- conn
Connection If
return_connis True, also returns the connection object linkinginputandobj.
- obj
Notes
The input connection created for the new TensorNode will be marked as non-trainable by default.
Converter¶
nengo_dl.Converter can be used to automatically convert a Keras model to a native
Nengo Network. This can be useful if, for example, you want to run a model in different
Nengo Simulator backends (which will only support the core Nengo objects).
See the documentation for more details.
Converts a Keras model to a Nengo network composed of native Nengo objects. |
-
class
nengo_dl.Converter(model, allow_fallback=True, inference_only=False, max_to_avg_pool=False, split_shared_weights=False, swap_activations=None, scale_firing_rates=None, synapse=None, temporal_model=False)[source]¶ Converts a Keras model to a Nengo network composed of native Nengo objects.
- Parameters
- model
tf.keras.Model Keras model to be converted
- allow_fallbackbool
If True, allow layers that cannot be converted to native Nengo objects to be added as a
TensorNodeinstead. Note that if this occurs, the converted Nengo network will only be runnable in the NengoDL simulator.- inference_onlybool
Allow layers to be converted in such a way that inference behaviour will match the source model but training behaviour will not. If
inference_only=Falsethen some layers cannot be converted to native Nengo objects (but you can still useallow_fallback=Trueto use aTensorNodeinstead).- max_to_avg_poolbool
If True, convert max pooling layers to average pooling layers. Note that this will change the behaviour of the network, so parameters will probably need to be re-trained in NengoDL. If
max_to_avg_pool=Falsethen max pooling layers cannot be converted to native Nengo objects (but you can still useallow_fallback=Trueto use aTensorNodeinstead).- split_shared_weightsbool
In Keras, applying the same
Layerobject to different input layers will result in multiple instances of the given layer that share the same weights. This is not supported in Nengo. Ifsplit_shared_weights=Truethen those shared weights will be split into independent sets of weights. They will all be initialized to the same value, so the initial behaviour of the model will be unchanged, but if any further training is performed on the network then the weights in each of those instances may diverge.- swap_activationsdict
A dictionary mapping from TensorFlow activation functions or Nengo neuron types to TensorFlow activation functions or Nengo neuron types. This can be used to change all the activation types in a model to some other type. This is in addition to the default activation map (see
LayerConverter). It can be keyed based on either TensorFlow or Nengo activation types, and will be applied both before and after the default activation map, in order to support whatever swap type is most useful for a given model. In particular,swap_activationscan be useful for swapping rate neuron types to spiking neuron types, through e.g.{tf.nn.relu: nengo.SpikingRectifiedLinear()}or{nengo.RectifiedLinear(): nengo.SpikingRectifiedLinear()}. Or it can be used to swap activation types that don’t have a native Nengo implementation, e.g.{tf.keras.activatons.elu: tf.keras.activations.relu}.- scale_firing_rates: float or dict
Scales the inputs of neurons by
x, and the outputs by1/x. The idea is that this parameter can be used to increase the firing rates of spiking neurons (by scaling the input), without affecting the overall output (because the output spikes are being scaled back down). Note that this is only strictly true for neuron types with linear activation functions (e.g. ReLU). Nonlinear neuron types (e.g. LIF) will be skewed by this linear scaling on the input/output.scale_firing_ratescan be specified as a float, which will be applied to all layers in the model, or as a dictionary mapping Keras model layers to a scale factor, allowing different scale factors to be applied to different layers.- synapsefloat or
nengo.synapses.Synapse Synaptic filter to be applied on the output of all neurons. This can be useful to smooth out the noise introduced by spiking neurons. Note, however, that increasing the synaptic filtering will make the network less responsive to rapid changes in the input, and you may need to present each input value for more timesteps in order to allow the network output to settle.
- temporal_modelbool
Set to True if the Keras model contains a temporal dimension (e.g. the inputs and outputs of each layer have shape
(batch_size, n_steps, ...)). Note that all layers must be temporal, the Converter cannot handle models with a mix of temporal and non-temporal layers.
- model
- Attributes
- model
tf.keras.Model The input Keras model (if input was a Sequential model then this will be the equivalent Functional model).
- net
nengo.Network The converted Nengo network.
- inputs
Converter.KerasTensorDict Maps from Keras model inputs to input Nodes in the converted Nengo network. For example,
my_node = Converter(my_model).inputs[my_model.input].- outputs
Converter.KerasTensorDict Maps from Keras model outputs to output Probes in the converted Nengo network. For example,
my_probe = Converter(my_model).outputs[my_model.output].- layers
Converter.KerasTensorDict Maps from Keras model layers to the converted Nengo object. For example,
my_neurons = Converter(my_model).layers[my_layer].
- model
-
verify(training=False, inputs=None, atol=1e-08, rtol=1e-05)[source]¶ Verify that output of converted Nengo network matches the original Keras model.
- Parameters
- trainingbool
If True, check that optimizing the converted Nengo network produces the same results as optimizing the original Keras model.
- inputslist of
numpy.ndarray Testing values for model inputs (if not specified, array of ones will be used).
- atolfloat
Absolute tolerance for difference between Nengo and Keras outputs.
- rtolfloat
Relative (to Nengo) tolerance for difference between nengo and Keras outputs.
- Returns
- successbool
True if output of Nengo network matches output of Keras model.
- Raises
- ValueError
If output of Nengo network does not match output of Keras model.
-
get_converter(layer)[source]¶ Get instantiated
LayerConverterfor the givenLayerinstance.Note that this caches the results, so calling the function multiple times with the same Layer instance will return the same LayerConverter instance.
- Parameters
- layer
tf.keras.layers.Layer The Keras Layer being converted.
- layer
- Returns
- converter
LayerConverter LayerConverter class for converting
layerto Nengo objects.
- converter
-
classmethod
register(keras_layer)[source]¶ A decorator for adding a class to the converter registry.
- Parameters
- keras_layer
tf.keras.layers.Layer The Layer associated with the conversion function being registered.
- keras_layer
Configuration system¶
The configuration system is used to change NengoDL’s default behaviour in various ways.
See the documentation for more details.
Pass settings to |
|
Returns config settings (created by |
-
nengo_dl.configure_settings(**kwargs)[source]¶ Pass settings to
nengo_dlby setting them as parameters on the top-level Network config.The settings are passed as keyword arguments to
configure_settings; e.g., to settrainableuseconfigure_settings(trainable=True).- Parameters
- trainablebool or None
Adds a parameter to Nengo Ensembles/Connections that controls whether or not they will be optimized by
Simulator.fit. PassingNonewill use the defaultnengo_dltrainable settings, or True/False will override the default for all objects. In either case trainability can be further configured on a per-object basis (e.g.net.config[my_ensemble].trainable = True. See the documentation for more details.- plannergraph planning algorithm
Pass one of the graph planners to change the default planner.
- sortersignal sorting algorithm
Pass one of the sort algorithms to change the default sorter.
- simplifications: list of graph simplification functions
Pass a list of graph simplification functions to change the default simplifications applied. The default list of simplifications can be found in
nengo_dl.graph_optimizer.default_simplifications.- inference_onlybool
Set to True if the network will only be run in inference mode (i.e., no calls to
Simulator.fit). This may result in a small increase in the inference speed.- lif_smoothingfloat
If specified, use the smoothed
SoftLIFRateneuron model, with the given smoothing parameter (sigma), to compute the gradient forLIFneurons (as opposed to usingLIFRate).- dtype
tf.DType Set the floating point precision for simulation values.
- keep_historybool
Adds a parameter to Nengo Probes that controls whether or not they will keep the history from all simulation timesteps or only the last simulation step. This can be further configured on a per-probe basis (e.g.,
net.config[my_probe].keep_history = False).- statefulbool
If True (default), the Simulator will be built to support stateful execution (where internal simulation state is preserved between simulator functions such as
Simulator.predict). Otherwise all operations will be stateless. Note that this can also be configured individually through thestatefulparameter on individual functions.- use_loopbool
If True (default), use a symbolic while loop to run the simulation. Otherwise, simulation iterations are explicitly built into the model, avoiding the while loop. This can improve performance, but the simulation can only run for exactly
Simulator.unroll_simulationiterations.- learning_phasebool
The learning phase is used for models that have different behaviour in inference versus training mode (for example, spiking neurons swap their behaviour during training). Normally the learning phase is set automatically depending on what function is being called (e.g.
sim.predictwill run in inference mode andsim.fitwill run in training mode). However, sometimes it can be useful to override this behaviour (for example if we want to check what the output of the model looks like during training, we might want to runsim.predictin training mode). Setlearning_phase=Trueto always run the model in training mode (orFalseto always run in inference mode). Setlearning_phase=Noneto use the default behaviour.New in version 3.3.0.
-
nengo_dl.config.get_setting(model, setting, default=None, obj=None)[source]¶ Returns config settings (created by
configure_settings).- Parameters
- Returns
- config_val
Value of
settingif it has been specified, elsedefault.
Neuron types¶
Additions to the neuron types included with Nengo.
LIF neuron with smoothing around the firing threshold. |
|
Rectified linear neuron with nonzero slope for values < 0. |
|
Spiking version of |
-
class
nengo_dl.SoftLIFRate(sigma=1.0, **lif_args)[source]¶ LIF neuron with smoothing around the firing threshold.
This is a rate version of the LIF neuron whose tuning curve has a continuous first derivative, due to the smoothing around the firing threshold. It can be used as a substitute for LIF neurons in deep networks during training, and then replaced with LIF neurons when running the network [1].
- Parameters
- sigmafloat
Amount of smoothing around the firing threshold. Larger values mean more smoothing.
- tau_rcfloat
Membrane RC time constant, in seconds. Affects how quickly the membrane voltage decays to zero in the absence of input (larger = slower decay).
- tau_reffloat
Absolute refractory period, in seconds. This is how long the membrane voltage is held at zero after a spike.
- amplitudefloat
Scaling factor on the neuron output. Corresponds to the relative amplitude of the output spikes of the neuron.
Notes
Adapted from https://github.com/nengo/nengo-extras/blob/master/nengo_extras/neurons.py
References
- 1
Eric Hunsberger and Chris Eliasmith (2015): Spiking deep networks with LIF neurons. https://arxiv.org/abs/1510.08829.
-
class
nengo_dl.LeakyReLU(negative_slope=0.3, amplitude=1, **kwargs)[source]¶ Rectified linear neuron with nonzero slope for values < 0.
- Parameters
- negative_slopefloat
Scaling factor applied to values less than zero.
- amplitudefloat
Scaling factor on the neuron output. Note that this will combine multiplicatively with
negative_slopefor values < 0.
-
class
nengo_dl.SpikingLeakyReLU(negative_slope=0.3, amplitude=1, **kwargs)[source]¶ Spiking version of
LeakyReLU.Note that this may output “negative spikes” (i.e. a spike with a sign of -1).
- Parameters
- negative_slopefloat
Scaling factor applied to values less than zero.
- amplitudefloat
Scaling factor on the neuron output. Note that this will combine multiplicatively with
negative_slopefor values < 0.
Distributions¶
Additions to the distributions included with Nengo.
These distributions are usually used to initialize weight matrices, e.g.
nengo.Connection(a.neurons, b.neurons, transform=nengo_dl.dists.Glorot()).
Normal distribution where any values more than some distance from the mean are resampled. |
|
Variance scaling distribution for weight initialization (analogous to |
|
Weight initialization method from [1] (also known as Xavier initialization). |
|
Weight initialization method from [1]. |
-
class
nengo_dl.dists.TruncatedNormal(mean=0, stddev=1, limit=None)[source]¶ Normal distribution where any values more than some distance from the mean are resampled.
- Parameters
- meanfloat
Mean of the normal distribution.
- stddevfloat
Standard deviation of the normal distribution.
- limitfloat
Resample any values more than this distance from the mean. If None, then limit will be set to 2 standard deviations.
-
sample(n, d=None, rng=None)[source]¶ Samples the distribution.
- Parameters
- nint
Number samples to take.
- dint or None
The number of dimensions to return. If this is an int, the return value will be of shape
(n, d). If None, the return value will be of shape(n,).- rng
RandomState Random number generator state (if None, will use the default numpy random number generator).
- Returns
- samples(n,) or (n, d) array_like
Samples as a 1d or 2d array depending on
d. The second dimension enumerates the dimensions of the process.
-
class
nengo_dl.dists.VarianceScaling(scale=1, mode='fan_avg', distribution='uniform')[source]¶ Variance scaling distribution for weight initialization (analogous to
tf.initializers.VarianceScaling).- Parameters
- scalefloat
Overall scale on values.
- mode“fan_in” or “fan_out” or “fan_avg”
Whether to scale based on input or output dimensionality, or average of the two.
- distribution: “uniform” or “normal”
Whether to use a uniform or truncated normal distribution for weights.
-
sample(n, d=None, rng=None)[source]¶ Samples the distribution.
- Parameters
- nint
Number samples to take.
- dint or None
The number of dimensions to return. If this is an int, the return value will be of shape
(n, d). If None, the return value will be of shape(n,).- rng
RandomState Random number generator state (if None, will use the default numpy random number generator).
- Returns
- samples(n,) or (n, d) array_like
Samples as a 1d or 2d array depending on
d. The second dimension enumerates the dimensions of the process.
-
class
nengo_dl.dists.Glorot(scale=1, distribution='uniform')[source]¶ Weight initialization method from [1] (also known as Xavier initialization).
- Parameters
- scalefloat
Scale on weight distribution. For rectified linear units this should be sqrt(2), otherwise usually 1.
- distribution: “uniform” or “normal”
Whether to use a uniform or normal distribution for weights
References
- 1(1,2)
Xavier Glorot and Yoshua Bengio (2010): Understanding the difficulty of training deep feedforward neural networks. International conference on artificial intelligence and statistics. http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf.
-
class
nengo_dl.dists.He(scale=1, distribution='normal')[source]¶ Weight initialization method from [1].
- Parameters
- scalefloat
Scale on weight distribution. For rectified linear units this should be sqrt(2), otherwise usually 1.
- distribution: “uniform” or “normal”
Whether to use a uniform or normal distribution for weights
References
- 1(1,2)
Kaiming He, Xiangyu Zhang, Shaoqing Ren, and Jian Sun. (2015): Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification. https://arxiv.org/abs/1502.01852.
Loss functions¶
Some common loss functions (for use with the loss argument in
Simulator.compile).
Compute Mean Squared Error between given outputs and targets. |
|
An objective function to apply regularization penalties. |
-
nengo_dl.losses.nan_mse(y_true, y_pred)[source]¶ Compute Mean Squared Error between given outputs and targets.
If any values in
y_truearenan, that will be treated as zero error for those elements.- Parameters
- y_true
tf.Tensor Target values for a Probe in a network.
- y_pred
tf.Tensor Output values from a Probe in a network.
- y_true
- Returns
- mse
tf.Tensor Tensor representing the mean squared error.
- mse
-
class
nengo_dl.losses.Regularize(order=2, axis=None)[source]¶ An objective function to apply regularization penalties.
This is designed to be applied to a probed signal, e.g.
with nengo.Network() as net: a = nengo.Node([0]) b = nengo.Node(size_in=1) c = nengo.Connection(a, b, transform=1) p = nengo.Probe(c, "weights") ... # this part is optional, but we may often want to only keep the data from # the most recent timestep when probing in this way, to save memory nengo_dl.configure_settings(keep_history=True) net.config[p].keep_history = False with nengo_dl.Simulator(net) as sim: sim.compile(loss={p: nengo_dl.losses.Regularize()})
- Parameters
- orderint or str
Order of the regularization norm (e.g.
1for L1 norm,2for L2 norm). See https://www.tensorflow.org/api_docs/python/tf/norm for a full description of the possible values for this parameter.- axisint or None
The axis of the probed signal along which to compute norm. If None (the default), the signal is flattened and the norm is computed across the resulting vector. Note that these are only the axes with respect to the output on a single timestep (i.e. batch/time dimensions are not included).
Notes
The mean will be computed across all the non-
axisdimensions after computing the norm (including batch/time) in order to compute the overall objective value.
Callbacks¶
Objects to be used with the Keras callback functionality.
See https://www.tensorflow.org/guide/keras/custom_callback for more information on how to use Keras callbacks.
The short answer is that these can be passed to, e.g., Simulator.fit like
sim.fit(..., callbacks=[nengo_dl.callbacks.NengoSummaries(...)]
Logs the values of Nengo object parameters, to be displayed in TensorBoard. |
|
A version of the Keras TensorBoard callback that also profiles inference. |
|
Isolate the internal state of the simulation from any other stateful operations. |
-
class
nengo_dl.callbacks.NengoSummaries(log_dir, sim, objects)[source]¶ Logs the values of Nengo object parameters, to be displayed in TensorBoard.
See https://www.tensorflow.org/tensorboard/get_started for general instructions on using TensorBoard.
- Parameters
- log_dirstr
Directory where log file will be written.
- sim
Simulator Simulator object which will be used to look up parameter values.
- objectslist of
nengo.Ensembleornengo.ensemble.Neuronsornengo.Connection The object whose parameter values we want to record (passing an Ensemble will log its encoders, Neurons will log biases, and Connection will log connection weights/decoders).
-
class
nengo_dl.callbacks.TensorBoard(log_dir='logs', histogram_freq=0, write_graph=True, write_images=False, update_freq='epoch', profile_batch=2, embeddings_freq=0, embeddings_metadata=None, **kwargs)[source]¶ A version of the Keras TensorBoard callback that also profiles inference.
-
class
nengo_dl.callbacks.IsolateState(sim)[source]¶ Isolate the internal state of the simulation from any other stateful operations.
This will cause every batch to begin from the same initial state (the state of the simulation whenever this callback is created). And when this operation completes, the simulation state will be returned to that initial state.
- Parameters
- sim
Simulator The Simulator containing the state we want to control.
- sim
Developers¶
These objects are only relevant to people interested in modifying the implementation of NengoDL (e.g., adding a new neuron type).
Builder¶
The builder manages the mapping between (groups of) Nengo operators and the builder objects that know how to translate those operators into a TensorFlow graph.
Manages the operator build classes known to the |
|
Stores configuration parameters that may be relevant to parts of the build process. |
|
Base class for build classes, which implement the logic for building a group of Nengo Operators into TensorFlow. |
|
Copy of the default Nengo builder. |
|
Copy of the default Nengo model. |
-
class
nengo_dl.builder.Builder(plan)[source]¶ Manages the operator build classes known to the
nengo_dlbuild process.- Parameters
- planlist of tuple of
Operator The groups of operators that will be built
- planlist of tuple of
-
build_pre(signals, config, progress=None)[source]¶ Setup step for build classes, in which they compute any of the values that are constant across simulation timesteps.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig Configuration parameters for the build process
- progress
utils.ProgressBar Progress bar for ops in plan
- signals
-
build_step(signals, progress=None)[source]¶ Build the computations implementing a single simulator timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- progress
utils.ProgressBar Progress bar for ops in plan
- signals
- Returns
- side_effectslist of
tf.Tensor Outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used.
- side_effectslist of
-
build_post(signals, progress=None)[source]¶ Calls post build functions for all ops in plan.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- progress
utils.ProgressBar Progress bar for ops in plan
- signals
-
class
nengo_dl.builder.BuildConfig[source]¶ Stores configuration parameters that may be relevant to parts of the build process.
- Parameters
- inference_onlybool
If True the network should be constructed in “inference only” mode (omitting any support for training operations).
- lif_smoothingfloat
Smoothing parameter for
LIFgradient approximation.- cpu_onlybool
True if TensorFlow is only running on the CPU (because that was specified by the user or because GPU support is not available).
- rng
RandomState Seeded random number generator.
- training
tf.Tensor(bool) True if building in training mode, False for inference mode.
Create new instance of BuildConfig(inference_only, lif_smoothing, cpu_only, rng, training)
-
class
nengo_dl.builder.OpBuilder(ops)[source]¶ Base class for build classes, which implement the logic for building a group of Nengo Operators into TensorFlow.
Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
build_post(signals)[source]¶ This function will be called after the graph has been built and each time the Simulator is reset.
Note that this function may be called multiple times per session, so it should do any required operations in-place.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
-
static
mergeable(x, y)[source]¶ Compute the mergeability of two operators of this builder’s type.
- Parameters
- x
nengo.builder.Operator The operator being tested
- y
nengo.builder.Operator The operator being merged into (this is representative of a group of operators that have already been merged)
- x
- Returns
- mergeablebool
True if
xandycan be merged into a single built op, elseFalse.
-
class
nengo_dl.builder.NengoBuilder[source]¶ Copy of the default Nengo builder.
This class is here so that we can register new build functions for NengoDL without affecting the default Nengo build process.
-
classmethod
build(model, obj, *args, **kwargs)[source]¶ Build
objintomodel.This method looks up the appropriate build function for
objand calls it with the model and other arguments provided.In addition to the parameters listed below, further positional and keyword arguments will be passed unchanged into the build function.
- Parameters
- modelModel
The
Modelinstance in which to store build artifacts.- objobject
The object to build into the model.
-
classmethod
-
class
nengo_dl.builder.NengoModel(*args, fail_fast=True, **kwargs)[source]¶ Copy of the default Nengo model.
This allows us to override certain model behaviours.
- Parameters
- fail_fastbool
If True, try to call
op.make_stepwhen ops are added to the model. Note that NengoDL doesn’t actually usemake_step, so errors in that function are not necessarily errors in NengoDL (which is why we want to disable that check). But it might still be useful when debugging new op/build functions, which is why we leave the option.
-
add_op(op)[source]¶ Add an operator to the model.
- Parameters
- op
Operator Operator being added to the model.
- op
Notes
This is a copy of the parent
nengo.builder.Model.add_op, with the addition of theif self.fail_fastcondition.
Operator builders¶
These objects are used to convert Nengo operators into TensorFlow graph elements.
Basic operators¶
Build classes for basic Nengo operators.
A version of |
|
A version of |
|
A version of |
|
A version of |
|
Build a group of |
|
Build a group of |
|
Build a group of |
|
Matrix multiplication between sparse matrix A and dense matrix X |
|
Build a group of |
|
Build a group of |
|
Build a group of |
|
Build a group of |
|
Build a group of |
-
class
nengo_dl.op_builders.ResetInc(*args, **kwargs)[source]¶ A version of
Resetthat increments the target value rather than overwriting.-
property
dst¶ dst is stored in
incsrather thansets.
-
property
-
class
nengo_dl.op_builders.ElementwiseSet(*args, **kwargs)[source]¶ A version of
ElementwiseIncthat overwrites the target rather than incrementing.-
property
Y¶ Y is stored in
setsrather thanincs.
-
property
-
class
nengo_dl.op_builders.DotSet(*args, **kwargs)[source]¶ A version of
DotIncthat overwrites the target rather than incrementing.-
property
Y¶ Y is stored in
setsrather thanincs.
-
property
-
class
nengo_dl.op_builders.SparseDotSet(*args, **kwargs)[source]¶ A version of
SparseDotIncthat overwrites the target rather than incrementing.-
property
Y¶ Y is stored in
setsrather thanincs.
-
property
-
class
nengo_dl.op_builders.ResetBuilder(ops)[source]¶ Build a group of
Resetoperators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
static
mergeable(x, y)[source]¶ Compute the mergeability of two operators of this builder’s type.
- Parameters
- x
nengo.builder.Operator The operator being tested
- y
nengo.builder.Operator The operator being merged into (this is representative of a group of operators that have already been merged)
- x
- Returns
- mergeablebool
True if
xandycan be merged into a single built op, elseFalse.
-
class
nengo_dl.op_builders.CopyBuilder(ops)[source]¶ Build a group of
Copyoperators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
static
mergeable(x, y)[source]¶ Compute the mergeability of two operators of this builder’s type.
- Parameters
- x
nengo.builder.Operator The operator being tested
- y
nengo.builder.Operator The operator being merged into (this is representative of a group of operators that have already been merged)
- x
- Returns
- mergeablebool
True if
xandycan be merged into a single built op, elseFalse.
-
class
nengo_dl.op_builders.ElementwiseIncBuilder(ops)[source]¶ Build a group of
ElementwiseIncoperators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
static
mergeable(x, y)[source]¶ Compute the mergeability of two operators of this builder’s type.
- Parameters
- x
nengo.builder.Operator The operator being tested
- y
nengo.builder.Operator The operator being merged into (this is representative of a group of operators that have already been merged)
- x
- Returns
- mergeablebool
True if
xandycan be merged into a single built op, elseFalse.
-
nengo_dl.op_builders.sparse_matmul(A_indices, A_data, A_shape, X, transpose_x=False)[source]¶ Matrix multiplication between sparse matrix A and dense matrix X
- Parameters
- A_indices
tf.Tensor (N, 2) array of [row,col] non-zero entries
- A_data
tf.Tensor (N,) array of data in the nonzero entries specified in
A_indices- A_shapetuple of int
Shape of full A matrix
- X
tf.Tensor Dense matrix being multiplied by A
- transpose_xbool
Transpose X before multiply
- A_indices
- Returns
- dot
tf.Tensor Result of matrix multiplication between A and X
- dot
-
class
nengo_dl.op_builders.DotIncBuilder(ops)[source]¶ Build a group of
DotIncoperators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
static
mergeable(x, y)[source]¶ Compute the mergeability of two operators of this builder’s type.
- Parameters
- x
nengo.builder.Operator The operator being tested
- y
nengo.builder.Operator The operator being merged into (this is representative of a group of operators that have already been merged)
- x
- Returns
- mergeablebool
True if
xandycan be merged into a single built op, elseFalse.
-
class
nengo_dl.op_builders.SimPyFuncBuilder(ops)[source]¶ Build a group of
SimPyFuncoperators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
static
mergeable(x, y)[source]¶ Compute the mergeability of two operators of this builder’s type.
- Parameters
- x
nengo.builder.Operator The operator being tested
- y
nengo.builder.Operator The operator being merged into (this is representative of a group of operators that have already been merged)
- x
- Returns
- mergeablebool
True if
xandycan be merged into a single built op, elseFalse.
-
class
nengo_dl.op_builders.SparseDotIncBuilder(ops)[source]¶ Build a group of
SparseDotIncoperators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
static
mergeable(x, y)[source]¶ Compute the mergeability of two operators of this builder’s type.
- Parameters
- x
nengo.builder.Operator The operator being tested
- y
nengo.builder.Operator The operator being merged into (this is representative of a group of operators that have already been merged)
- x
- Returns
- mergeablebool
True if
xandycan be merged into a single built op, elseFalse.
-
class
nengo_dl.op_builders.TimeUpdateBuilder(ops)[source]¶ Build a group of
TimeUpdateoperators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
static
mergeable(x, y)[source]¶ Compute the mergeability of two operators of this builder’s type.
- Parameters
- x
nengo.builder.Operator The operator being tested
- y
nengo.builder.Operator The operator being merged into (this is representative of a group of operators that have already been merged)
- x
- Returns
- mergeablebool
True if
xandycan be merged into a single built op, elseFalse.
-
class
nengo_dl.op_builders.SimProbeBuilder(ops)[source]¶ Build a group of
SimProbeoperators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
static
mergeable(x, y)[source]¶ Compute the mergeability of two operators of this builder’s type.
- Parameters
- x
nengo.builder.Operator The operator being tested
- y
nengo.builder.Operator The operator being merged into (this is representative of a group of operators that have already been merged)
- x
- Returns
- mergeablebool
True if
xandycan be merged into a single built op, elseFalse.
Neuron types¶
Build classes for Nengo neuron operators.
Builds all neuron types for which there is no custom TensorFlow implementation. |
|
Base class for |
|
Build a group of |
|
Build a group of |
|
Build a group of |
|
Build a group of |
|
Build a group of |
|
Build a group of |
|
Build a group of |
|
Build a group of |
|
Build a group of |
|
Build a group of |
|
Builds a group of |
-
class
nengo_dl.neuron_builders.GenericNeuronBuilder(ops)[source]¶ Builds all neuron types for which there is no custom TensorFlow implementation.
Notes
These will be executed as native Python functions, requiring execution to move in and out of TensorFlow. This can significantly slow down the simulation, so any performance-critical neuron models should consider adding a custom TensorFlow implementation for their neuron type instead.
Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
class
nengo_dl.neuron_builders.TFNeuronBuilder(ops)[source]¶ Base class for
NeuronTypebuilders with a TF implementation.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
training_step(J, dt, **state)[source]¶ Implements the logic for a single training step.
Note: subclasses only need to implement this if
spiking=True. It is used to specify an alternate (differentiable) implementation of the neuron model to be used during training.
-
build_step(signals, **step_kwargs)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
class
nengo_dl.neuron_builders.RectifiedLinearBuilder(ops)[source]¶ Build a group of
RectifiedLinearneuron operators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
class
nengo_dl.neuron_builders.SpikingRectifiedLinearBuilder(ops)[source]¶ Build a group of
SpikingRectifiedLinearneuron operators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
class
nengo_dl.neuron_builders.SigmoidBuilder(ops)[source]¶ Build a group of
Sigmoidneuron operators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
class
nengo_dl.neuron_builders.TanhBuilder(ops)[source]¶ Build a group of
Tanhneuron operators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
class
nengo_dl.neuron_builders.LIFRateBuilder(ops)[source]¶ Build a group of
LIFRateneuron operators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
class
nengo_dl.neuron_builders.SoftLIFRateBuilder(ops)[source]¶ Build a group of
SoftLIFRateneuron operators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
class
nengo_dl.neuron_builders.LIFBuilder(ops)[source]¶ Build a group of
LIFneuron operators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
class
nengo_dl.neuron_builders.RegularSpikingBuilder(ops)[source]¶ Build a group of
RegularSpikingneuron operators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
class
nengo_dl.neuron_builders.StochasticSpikingBuilder(ops)[source]¶ Build a group of
StochasticSpikingneuron operators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
class
nengo_dl.neuron_builders.PoissonSpikingBuilder(ops)[source]¶ Build a group of
PoissonSpikingneuron operators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
class
nengo_dl.neuron_builders.SimNeuronsBuilder(ops)[source]¶ Builds a group of
SimNeuronsoperators.Calls the appropriate sub-build class for the different neuron types.
- Attributes
- TF_NEURON_IMPLdict of {
NeuronType,builder.OpBuilder} Mapping from neuron types to custom build classes (neurons without a custom builder will use the generic builder).
- TF_NEURON_IMPLdict of {
Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
static
mergeable(x, y)[source]¶ Compute the mergeability of two operators of this builder’s type.
- Parameters
- x
nengo.builder.Operator The operator being tested
- y
nengo.builder.Operator The operator being merged into (this is representative of a group of operators that have already been merged)
- x
- Returns
- mergeablebool
True if
xandycan be merged into a single built op, elseFalse.
Learning rules¶
Build classes for Nengo learning rule operators.
Build a group of |
|
Build a group of |
|
Build a group of |
|
Builds a |
|
Build a group of |
-
class
nengo_dl.learning_rule_builders.SimBCMBuilder(ops)[source]¶ Build a group of
SimBCMoperators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
static
mergeable(x, y)[source]¶ Compute the mergeability of two operators of this builder’s type.
- Parameters
- x
nengo.builder.Operator The operator being tested
- y
nengo.builder.Operator The operator being merged into (this is representative of a group of operators that have already been merged)
- x
- Returns
- mergeablebool
True if
xandycan be merged into a single built op, elseFalse.
-
class
nengo_dl.learning_rule_builders.SimOjaBuilder(ops)[source]¶ Build a group of
SimOjaoperators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
static
mergeable(x, y)[source]¶ Compute the mergeability of two operators of this builder’s type.
- Parameters
- x
nengo.builder.Operator The operator being tested
- y
nengo.builder.Operator The operator being merged into (this is representative of a group of operators that have already been merged)
- x
- Returns
- mergeablebool
True if
xandycan be merged into a single built op, elseFalse.
-
class
nengo_dl.learning_rule_builders.SimVojaBuilder(ops)[source]¶ Build a group of
SimVojaoperators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
static
mergeable(x, y)[source]¶ Compute the mergeability of two operators of this builder’s type.
- Parameters
- x
nengo.builder.Operator The operator being tested
- y
nengo.builder.Operator The operator being merged into (this is representative of a group of operators that have already been merged)
- x
- Returns
- mergeablebool
True if
xandycan be merged into a single built op, elseFalse.
-
nengo_dl.learning_rule_builders.build_pes(model, pes, rule)[source]¶ Builds a
nengo.PESobject into a Nengo model.Overrides the standard Nengo PES builder in order to avoid slicing on axes > 0 (not currently supported in NengoDL).
- Parameters
- modelModel
The model to build into.
- pesPES
Learning rule type to build.
- ruleLearningRule
The learning rule object corresponding to the neuron type.
Notes
Does not modify
model.params[]and can therefore be called more than once with the samenengo.PESinstance.
-
class
nengo_dl.learning_rule_builders.SimPESBuilder(ops)[source]¶ Build a group of
SimPESoperators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
static
mergeable(x, y)[source]¶ Compute the mergeability of two operators of this builder’s type.
- Parameters
- x
nengo.builder.Operator The operator being tested
- y
nengo.builder.Operator The operator being merged into (this is representative of a group of operators that have already been merged)
- x
- Returns
- mergeablebool
True if
xandycan be merged into a single built op, elseFalse.
Processes¶
Build classes for Nengo process operators.
Builds all process types for which there is no custom TensorFlow implementation. |
|
Build a group of |
|
Build a group of |
|
Builds a group of |
-
class
nengo_dl.process_builders.GenericProcessBuilder(ops)[source]¶ Builds all process types for which there is no custom TensorFlow implementation.
Notes
These will be executed as native Python functions, requiring execution to move in and out of TensorFlow. This can significantly slow down the simulation, so any performance-critical processes should consider adding a custom TensorFlow implementation for their type instead.
Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
build_post(signals)[source]¶ This function will be called after the graph has been built and each time the Simulator is reset.
Note that this function may be called multiple times per session, so it should do any required operations in-place.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
-
class
nengo_dl.process_builders.LowpassBuilder(ops)[source]¶ Build a group of
Lowpasssynapse operators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
class
nengo_dl.process_builders.LinearFilterBuilder(ops)[source]¶ Build a group of
LinearFiltersynapse operators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
class
nengo_dl.process_builders.SimProcessBuilder(ops)[source]¶ Builds a group of
SimProcessoperators.Calls the appropriate sub-build class for the different process types.
- Attributes
- TF_PROCESS_IMPLdict of {
Process:builder.OpBuilder} Mapping from process types to custom build classes (processes without a custom builder will use the generic builder).
- TF_PROCESS_IMPLdict of {
Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
build_post(signals)[source]¶ This function will be called after the graph has been built and each time the Simulator is reset.
Note that this function may be called multiple times per session, so it should do any required operations in-place.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
-
static
mergeable(x, y)[source]¶ Compute the mergeability of two operators of this builder’s type.
- Parameters
- x
nengo.builder.Operator The operator being tested
- y
nengo.builder.Operator The operator being merged into (this is representative of a group of operators that have already been merged)
- x
- Returns
- mergeablebool
True if
xandycan be merged into a single built op, elseFalse.
Transforms¶
Build classes for Nengo transform operators.
A version of |
|
Build a group of |
-
class
nengo_dl.transform_builders.ConvSet(*args, **kwargs)[source]¶ A version of
ConvIncthat overwrites the target rather than incrementing.-
property
Y¶ Y is stored in
setsrather thanincs.
-
property
-
class
nengo_dl.transform_builders.ConvIncBuilder(ops)[source]¶ Build a group of
nengo.builder.transforms.ConvIncoperators.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
-
static
mergeable(x, y)[source]¶ Compute the mergeability of two operators of this builder’s type.
- Parameters
- x
nengo.builder.Operator The operator being tested
- y
nengo.builder.Operator The operator being merged into (this is representative of a group of operators that have already been merged)
- x
- Returns
- mergeablebool
True if
xandycan be merged into a single built op, elseFalse.
TensorNodes¶
To build TensorNode objects we need to define a new Nengo operator
(tensor_node.SimTensorNode), a build function that adds that operator
into a Nengo graph (tensor_node.build_tensor_node), and a NengoDL
build class that maps that new Nengo operator to TensorFlow operations
(tensor_node.SimTensorNodeBuilder).
Operator for TensorNodes (constructed by |
|
This is the Nengo build function, so that Nengo knows what to do with TensorNodes. |
|
Builds a |
-
class
nengo_dl.tensor_node.SimTensorNode(func, time, input, output, shape_in, tag=None)[source]¶ Operator for TensorNodes (constructed by
build_tensor_node).- Parameters
- funccallable
The TensorNode function (
tensor_func).- time
Signalor None Signal representing the current simulation time (or None if
pass_timeis False).- input
Signalor None Input Signal for the TensorNode (or None if no inputs).
- output
Signal Output Signal for the TensorNode.
- shape_intuple of int or None
Shape of input to TensorNode (if None, will leave the shape of input signal unchanged).
- tagstr
A label associated with the operator, for debugging
Notes
sets
[output]incs
[]reads
[time](ifpass_time=True) +[input](ifinputis not None)updates
[]
-
nengo_dl.tensor_node.build_tensor_node(model, node)[source]¶ This is the Nengo build function, so that Nengo knows what to do with TensorNodes.
-
class
nengo_dl.tensor_node.SimTensorNodeBuilder(ops)[source]¶ Builds a
SimTensorNodeoperator into a NengoDL model.Initialize internal OpBuilder implementation.
Note: this should not be building any model operations, this is purely for internal setup of the
OpBuilderitself.- Parameters
- opslist of
Operator The operator group to build into the model
- opslist of
-
build_pre(signals, config)[source]¶ This function should set up any computations that are fixed for this op (i.e., things that do not need to be recomputed each timestep).
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)- config
BuildConfig General repository for config information builders might want (conglomerated into this object so that we can add/remove config data without having to change the function signature all the time).
- signals
-
build_step(signals)[source]¶ This function builds whatever computations need to be executed in each simulation timestep.
- Parameters
- signals
signals.SignalDict Mapping from
Signaltotf.Tensor(updated by operations)
- signals
- Returns
- side_effectslist of
tf.Tensor If not None, the returned tensors correspond to outputs with possible side-effects, i.e. computations that need to be executed in the TensorFlow graph even if their output doesn’t appear to be used
- side_effectslist of
Graph construction¶
Manages the data and build processes associated with implementing a Nengo simulation in TensorFlow.
Implement the Nengo simulation as a Keras Layer. |
-
class
nengo_dl.tensor_graph.TensorGraph(model, dt, unroll_simulation, minibatch_size, device, progress, seed)[source]¶ Implement the Nengo simulation as a Keras Layer.
- Parameters
- model
Model Pre-built Nengo model describing the network to be simulated.
- dtfloat
Length of a simulator timestep, in seconds.
- unroll_simulationint
Unroll simulation loop by explicitly building
unroll_simulationiterations into the computation graph.- minibatch_sizeint
The number of simultaneous inputs that will be passed through the network.
- deviceNone or
"/cpu:0"or"/gpu:[0-n]" Device on which to execute computations (if None then uses the default device as determined by TensorFlow).
- progress
utils.ProgressBar Progress bar for optimization stage.
- seedint
Seed for random number generation.
- model
-
build_inputs()[source]¶ Generates a set of Input layers that can be used as inputs to a TensorGraph layer.
- Returns
- n_steps
tf.keras.layers.Input Input layer for specifying the number of simulation timesteps.
- inputsdict of {
nengo.Node:tf.keras.layers.Input} Input layers for each of the Nodes in the network.
- n_steps
-
build(input_shape=None)[source]¶ Create any Variables used in the model.
- Parameters
- input_shapelist of tuple of int
Shapes of all the inputs to this layer.
-
call(inputs, training=None, progress=None, stateful=False)[source]¶ Constructs the graph elements to simulate the model.
- Parameters
- inputslist of
tf.Tensor Input layers/tensors for the network (must match the structure defined in
build_inputs).- trainingbool
Whether the network is being run in training or inference mode. If None, uses the symbolic Keras learning phase variable.
- progress
utils.ProgressBar Progress bar for construction stage.
- statefulbool
Whether or not to build the model to support preserving the internal state between executions.
- inputslist of
- Returns
- probe_arrayslist of
tf.Tensor Tensors representing the output of all the Probes in the network (order corresponding to
self.model.probes, which is the order the Probes were instantiated).
- probe_arrayslist of
-
build_post()[source]¶ Executes post-build processes for operators (after the graph has been constructed and whenever Simulator is reset).
-
get_tensor(sig)[source]¶ Returns a Tensor corresponding to the given Signal.
- Parameters
- sig
Signal A signal in the Nengo model.
- sig
- Returns
- tensor
tf.Tensor Tensor containing the value of the given Signal.
- tensor
-
mark_signals()[source]¶ Mark all the signals in
self.modelaccording to whether they represent trainable parameters of the model (parameters that can be optimized by deep learning methods).Trainable parameters include connection weights, ensemble encoders, and neuron biases. Unless one of those signals is targeted by a Nengo learning rule (otherwise the learning rule update conflicts with the deep learning optimization).
Users can manually specify whether signals are trainable or not using the config system (e.g.,
net.config[nengo.Ensemble].trainable = False).The trainable attribute will be set to one of three values:
True: Signal is trainableFalse: Signal could be trainable, but has been set to non-trainable (e.g., because the user manually configured that object not to be trainable).None: Signal is never trainable (e.g., simulator state)
-
create_signals(sigs)[source]¶ Groups signal data together into larger arrays, and represent each individual signal as a slice into that array.
- Parameters
- sigslist of
Signal Base signals arranged into the order in which they should reside in memory (e.g., output from
graph_optimizer.order_signals)
- sigslist of
Signals¶
Represents and manages the internal simulation signals.
Represents a tensor as an indexed view into a base array. |
|
Handles the mapping from |
-
class
nengo_dl.signals.TensorSignal(slices, key, dtype, shape, minibatch_size, label='TensorSignal')[source]¶ Represents a tensor as an indexed view into a base array.
- Parameters
- slicestuple of tuple of int
Start/stop indices of slices along the first axis of the base array, corresponding to the data for this signal.
- keyobject
Key mapping to the base array that contains the data for this signal.
- dtypestr
dtype of the values represented by this signal.
- shapetuple of int
View shape of this signal (may differ from shape of base array).
- minibatch_sizeint
If not None then this signal contains a minibatch dimension with the given size.
- labelstr
Name for this signal, used to make debugging easier.
-
property
slices¶ The slices containing the data for this signal in the base array.
-
property
ndim¶ The rank of this signal.
-
__getitem__(indices)[source]¶ Create a new TensorSignal representing a subset (slice or advanced indexing) of the indices of this TensorSignal.
- Parameters
- indicesslice or list of int
The desired subset of the indices in this TensorSignal
- Returns
- sig
signals.TensorSignal A new TensorSignal representing the subset of this TensorSignal
- sig
-
reshape(shape)[source]¶ Create a new TensorSignal representing a reshaped view of the same data in this TensorSignal (size of data must remain unchanged).
- Parameters
- shapetuple of int
New shape for the signal (one dimension can be -1 to indicate an inferred dimension size, as in numpy)
- Returns
- sig
signals.TensorSignal New TensorSignal representing the same data as this signal but with the given shape
- sig
-
property
tf_shape¶ A
tf.Tensorrepresenting the shape of this signal.
-
property
tf_indices¶ A
tf.Tensorrepresenting the indices of this signal.
-
property
tf_indices_nd¶ A
tf.Tensorrepresenting the indices of this signal for use with e.g.scatter_nd.
-
property
tf_slice¶ A tuple of
tf.Tensorsrepresenting the(start, stop, stride)slice within the base array containing the data for this signal.This can be used as a more efficient representation of
TensorSignal.tf_indices.
-
property
full_shape¶ Shape of the signal including the minibatch dimension.
-
property
minibatched¶ Whether or not this TensorSignal contains a minibatch dimension.
-
class
nengo_dl.signals.SignalDict(dtype, minibatch_size)[source]¶ Handles the mapping from
Signaltotf.Tensor.Takes care of gather/scatter logic to read/write signals within the base arrays.
- Parameters
- dtypestr
Floating point precision used in signals (e.g. “float32”)
- minibatch_sizeint
Number of items in each minibatch
-
reset()[source]¶ Reset build-specific data structures.
These are data structures that are filled out during the TensorGraph build process (and therefore need to be re-initialized if we build the model again), as opposed to data that is constant for a given Nengo model.
-
scatter(dst, val, mode='update')[source]¶ Updates the base data corresponding to
dst.- Parameters
- dst
TensorSignal Signal indicating the data to be modified in base array
- val
tf.Tensor Update data (same shape as
dst, i.e. a dense array <= the size of the base array)- mode“update” or “inc”
Overwrite/add the data at
dstwithval
- dst
-
gather(src, force_copy=False)[source]¶ Fetches the data corresponding to
srcfrom the base array.- Parameters
- src
TensorSignal Signal indicating the data to be read from base array
- force_copybool
If True, always perform a gather, not a slice (this forces a copy). Note that setting
force_copy=Falsedoes not guarantee that a copy won’t be performed.
- src
- Returns
- gathered
tf.Tensor Tensor object corresponding to a dense subset of data from the base array
- gathered
-
combine(sigs, label='Combine')[source]¶ Combines several TensorSignals into one by concatenating along the first axis.
- Parameters
- sigslist of
TensorSignalorSignal Signals to be combined
- labelstr
Name for combined signal (to help with debugging)
- sigslist of
- Returns
- sig
TensorSignal New TensorSignal representing the concatenation of the data in
sigs
- sig
-
get_tensor_signal(slices, key, dtype, shape, minibatched, signal=None, label='TensorSignal')[source]¶ Creates a new
TensorSignalwith the given properties.This should be used rather than instantiating a new TensorSignal directly, as it handles some extra book-keeping.
- Parameters
- slicestuple of tuple of int
Start/stop indices of slices along the first axis of the base array, corresponding to the data for this signal.
- keyobject
Key mapping to the base array that contains the data for this signal
- dtypestr
dtype of the values represented by this signal
- shapetuple of int
View shape of this signal (may differ from shape of base array)
- minibatchedbool
Whether or not this signal contains a minibatch dimension
- signal
Signal If not None, associate the new
TensorSignalwith the givenSignalin thesig_map- labelstr
Name for this signal, used to make debugging easier
- Returns
- sig
TensorSignal A new
TensorSignalwith the given properties
- sig
-
op_constant(ops, op_sizes, attr, dtype, shape=1, - 1)[source]¶ Creates a tensor representing the constant parameters of an op group.
- Parameters
- opslist of object
The operators for some merged group of ops
- op_sizeslist of int
The number of constant elements in each op
- attrstr
The attribute of the op that describes the constant parameter
- dtypestr
Numeric type of the parameter
- shapetuple of int
Shape for returned constant (this will be ignored in the scalar case). The default adds an empty dimension for broadcasting along the batch axis.
- Returns
- constant
tf.Tensor Tensor containing the values of
attrfor the given ops. This will be a scalar if all the ops have the same parameter value, or an array giving the parameter value for each element in each op.
- constant
Graph optimization¶
These functions are used to restructure the Nengo operator graph so that it can be simulated more efficiently when converted into a TensorFlow graph.
Check if the given op can be merged with the candidate group. |
|
Create merged execution plan through greedy selection. |
|
Create merged execution plan through exhaustive tree search. |
|
Create merged execution plan through transitive closure construction. |
|
Computes the transitive closure for the given graph, restricted to the operators with the given builder type. |
|
Orders operators into a valid execution order, but does not perform any merging. |
|
Orders signals and operators to try to structure reads/writes in contiguous blocks. |
|
Reorder signals using heuristics to try to place signals that are accessed by the same operators into adjacent positions (giving priority to larger blocks). |
|
Rearrange operators to match the order of signals. |
|
Attempts to rearrange |
|
A version of |
|
Remove any Reset operators that are targeting a signal that is never modified. |
|
Remove any operators where we know the input (and therefore output) is zero. |
|
Replace |
|
Change Copies with constant input to Resets. |
|
Change y=x*1 ops to y=x Copy ops. |
|
Organizes operators into dictionaries according to the signals they set/inc/read/update. |
|
Creates a visual depiction of the signals blocks read by each operator group. |
-
nengo_dl.graph_optimizer.mergeable(op, chosen_ops)[source]¶ Check if the given op can be merged with the candidate group.
-
nengo_dl.graph_optimizer.greedy_planner(operators)[source]¶ Create merged execution plan through greedy selection.
Always selects the largest available group of operators as the next to be executed.
- Parameters
- operatorslist of
Operator All the
nengooperators in a model (unordered)
- operatorslist of
- Returns
- planlist of tuple of
Operator Operators combined into mergeable groups and in execution order
- planlist of tuple of
Notes
Originally based on
nengo_oclgreedy planner
-
nengo_dl.graph_optimizer.tree_planner(op_list, max_depth=3)[source]¶ Create merged execution plan through exhaustive tree search.
The
max_depthparameter scales the planner between full tree search and greedy search.max_depth==1is equivalent togreedy_planner, andmax_depth==len(op_list)is full tree search (guaranteed to find the optimal plan, but likely very slow).
-
nengo_dl.graph_optimizer.transitive_planner(op_list)[source]¶ Create merged execution plan through transitive closure construction.
This is something like a middle ground between
greedy_plannerandtree_planner; it can improve simulation time over the greedy planner, but comes with potentially significant build time increases.
-
nengo_dl.graph_optimizer.transitive_closure_recurse(dg, ops, trans, builder_type, builder_types, cache)[source]¶ Computes the transitive closure for the given graph, restricted to the operators with the given builder type.
- Parameters
- dgdict of {int: set of int}
Dependency graph where
dg[a] = {b, c}indicates that operatorsbandcare dependent ona- opslist of int
The operators for which we want to compute the transitive closure
- transdict of {int: set of int}
The transitive closure for the graph (will be filled in-place)
- builder_typetype
One of the
nengo_dlbuild classes (e.g.,CopyBuilder), specifying the type of operators to include in the transitive closure- builder_typeslist of type
The build class for each operator
- cachedict of {frozenset of int: set of int}
Stores base sets which
transwill reference (to reduce memory usage, since many elements intranswill have the same value)
Notes
This function uses ints to refer to operators, where the int indicates the index of the operator in the overall op list (this is done to save memory). See
transitive_planner.
-
nengo_dl.graph_optimizer.noop_planner(operators)[source]¶ Orders operators into a valid execution order, but does not perform any merging.
-
nengo_dl.graph_optimizer.order_signals(plan, n_passes=10)[source]¶ Orders signals and operators to try to structure reads/writes in contiguous blocks.
- Parameters
- planlist of tuple of
Operator Operator execution plan (e.g., output from
greedy_planner)- n_passesint
Number of repeated passes through the operator reordering stage
- planlist of tuple of
- Returns
-
nengo_dl.graph_optimizer.hamming_sort(blocks)[source]¶ Reorder signals using heuristics to try to place signals that are accessed by the same operators into adjacent positions (giving priority to larger blocks).
-
nengo_dl.graph_optimizer.sort_ops_by_signals(sorted_io, sigs, sig_idxs, new_plan, blocks, op_sigs)[source]¶ Rearrange operators to match the order of signals.
Note: the same operators can be associated with multiple IO blocks if they have multiple inputs, so rearranging the operators according to one of those blocks could mess up the order with respect to the other IO block. We iterate through the IO blocks in increasing size so that the largest blocks win out.
- Parameters
- sorted_iolist of tuple of (
Operator, int) The operators that form each IO block, sorted by increasing size of the block. In the case that a group of operators participate in multiple IO blocks, the integer distinguishes which one of those blocks this block is associated with.
- sigslist of
Signal Signals that have been arranged into a given order by other parts of the algorithm
- sig_idxsdict of {
Signal: int} Sorted indices of signals
- new_plandict of {tuple of
Operator: tuple ofOperator} Mapping from original operator group to the sorted operators
- blocksdict of {
Signal: frozenset of int} Indicates which IO blocks each signal participates in
- op_sigsdict of {
Operator: list ofSignal} The signals accessed by each operator
- sorted_iolist of tuple of (
- Returns
-
nengo_dl.graph_optimizer.sort_signals_by_ops(sorted_io, sigs, sig_idxs, new_plan, blocks, op_sigs)[source]¶ Attempts to rearrange
sigsso that it is in the same order as operator signals, without changing the overall block order.- Parameters
- sorted_iolist of tuple of (
Operator, int) The operators that form each io block, sorted by increasing size of the io block. In the case that a group of operators participate in multiple io blocks, the integer distinguishes which one of those blocks this block is associated with.
- sigslist of
Signal Signals to be sorted
- sig_idxsdict of {
Signal: int} Sorted indices of signals
- new_plandict of {tuple of
Operator: tuple ofOperator} Mapping from original operator group to the sorted operators
- blocksdict of {
Signal: frozenset of int} Indicates which io blocks each signal participates in
- op_sigsdict of {
Operator: list ofSignal} The signals accessed by each operator
- sorted_iolist of tuple of (
- Returns
- sig_idxsdict of {
Signal: int} Sorted indices of signals
- sig_idxsdict of {
-
nengo_dl.graph_optimizer.noop_order_signals(plan, **_)[source]¶ A version of
graph_optimizer.order_signalsthat doesn’t do any reordering, for debugging.
-
nengo_dl.graph_optimizer.remove_unmodified_resets(operators)[source]¶ Remove any Reset operators that are targeting a signal that is never modified.
If a signal is reset, but never inced/updated after that, we can just set the default signal value to the reset value and remove the reset. Note: this wouldn’t normally happen, but it can happen if we removed some of the incs (e.g. in
remove_zero_incs).
-
nengo_dl.graph_optimizer.remove_zero_incs(operators)[source]¶ Remove any operators where we know the input (and therefore output) is zero.
If the input to a DotInc/ElementwiseInc/Copy/ConvInc is zero then we know that the output of the op will be zero, so we can just get rid of it.
-
nengo_dl.graph_optimizer.remove_reset_incs(operators)[source]¶ Replace
y=Reset(0) + xwithy=x.If a signal is Reset and Inc’d, we can change that to a Set that combines the two ops (note: any other incs of that signal can proceed as normal)
- Parameters
- operatorslist of
Operator operators in the model
- operatorslist of
- Returns
- new_operatorslist of
Operator modified list of operators
- new_operatorslist of
Notes
In practice, this modification can hurt more than it helps. Inc operators are cheaper to compute the gradient for, and changing Incs to Incs and Sets splits up the Inc merge groups. It tends to provide the most value for models consisting of long linear chains of objects.
-
nengo_dl.graph_optimizer.remove_constant_copies(operators)[source]¶ Change Copies with constant input to Resets.
If a Copy has no dependencies, or just one Reset() dependency, then we can change it to an op that just directly sets the output signal to the Copy input value.
-
nengo_dl.graph_optimizer.remove_identity_muls(operators)[source]¶ Change y=x*1 ops to y=x Copy ops.
If one of the inputs to a DotInc/ElementwiseInc is 1 then we can skip the multiplication and change it to a Copy op.
-
nengo_dl.graph_optimizer.signal_io_dicts(operators)[source]¶ Organizes operators into dictionaries according to the signals they set/inc/read/update.
- Parameters
- operatorslist of
Operator Operators in the model
- operatorslist of
- Returns
- setsdict of {
Signal: list ofOperator} A dictionary indicating all the Operators that set each signal.
- incsdict of {
Signal: list ofOperator} A dictionary indicating all the Operators that inc each signal.
- readsdict of {
Signal: list ofOperator} A dictionary indicating all the Operators that read each signal.
- updatesdict of {
Signal: list ofOperator} A dictionary indicating all the Operators that update each signal.
- setsdict of {
-
nengo_dl.graph_optimizer.display_signal_blocks(operators, all_signals)[source]¶ Creates a visual depiction of the signals blocks read by each operator group.
- Parameters
- Returns
- signal_blocksstr
A string where each row corresponds to one operator group, and the non-blank characters in the line indicate that the operator group reads/writes that signal (with a number used to distinguish the different signal blocks within the operator group).
Layer converters¶
Tools for automatically converting a Keras model to a Nengo network.
Base class for converter classes, which contain the logic for mapping some Keras layer type to Nengo objects. |
|
Convert |
|
Convert |
|
Convert layers which do not have a native Nengo equivalent into a |
|
Base class for converting average pooling layers to Nengo objects. |
|
Convert |
|
Convert |
|
Convert |
|
Convert |
|
Convert |
|
Convert |
|
Convert |
|
Convert |
|
Base class for converting convolutional layers to Nengo objects. |
|
Convert |
|
Convert |
|
Convert |
|
Convert |
|
Convert |
|
Convert |
|
Convert |
|
Convert |
|
Convert |
|
Base class for converting upsampling layers to Nengo objects. |
|
Convert |
|
Convert |
|
Convert |
|
Base class for converting zero-padding layers to Nengo objects. |
|
Convert |
|
Convert |
|
Convert |
|
Base class for converting KerasSpiking layers. |
|
Convert |
|
Convert |
|
Convert |
-
class
nengo_dl.converter.LayerConverter(layer, converter)[source]¶ Base class for converter classes, which contain the logic for mapping some Keras layer type to Nengo objects.
Subclasses must implement the
LayerConverter.convertmethod. They may optionally extendLayerConverter.convertibleif this layer type requires custom logic for whether or not a layer can be converted to Nengo objects.Subclasses should override the
unsupported_argsclass parameter if there are certain non-default Layer attributes that are not supported by the converter. This is a list of names for attributes that must have the default value for the layer to be convertible. The default is assumed to beNone, or a tuple of("attribute_name", default_value)can be specified. If there are parameters that are supported in inference mode but not in training mode, they should be added to theunsupported_training_argsparameter.Subclasses should override the
has_weightsclass parameter if the layer type being converted contains internal weights (this affects how the converter will handle duplicate layers).- Parameters
- layer
tf.keras.layers.Layer The Layer object being converted.
- converter
Converter The parent Converter class running the conversion process.
- layer
-
add_nengo_obj(node_id, biases=None, activation=None)[source]¶ Builds a Nengo object for the given Node of this layer.
- Parameters
- node_idint
The index of the Keras Node currently being built on this layer.
- biases
numpy.ndarrayor None If not None, add trainable biases with the given value.
- activationcallable or None
The TensorFlow activation function to be used (
Nonewill be interpreted as linear activation).
- Returns
- obj
nengo.Nodeornengo.ensemble.Neuronsornengo_dl.TensorNode The Nengo object whose output corresponds to the output of the given Keras Node.
- obj
-
add_connection(node_id, obj, input_idx=0, trainable=False, **kwargs)[source]¶ Adds a Connection from one of the inputs of the Node being built to the Nengo object.
- Parameters
- node_idint
The index of the Keras Node currently being built on this layer.
- obj
NengoObject The Nengo object implementing this Node.
- input_idxint
Which of the inputs we want to add a Connection for (in the case of layers that have multiple inputs).
- trainablebool
Whether or not the weights associated with the created Connection should be trainable.
- kwargsdict
Will be passed on to
nengo.Connection.
- Returns
- conn
nengo.Connection The constructed Connection object.
- conn
-
get_input_obj(node_id, tensor_idx=0)[source]¶ Returns the Nengo object corresponding to the given input of this layer.
- Parameters
- node_idint
The index of the Keras Node currently being built on this layer.
- tensor_idxint
The index of the input we want to look up (for layers with multiple inputs).
- Returns
- obj
nengo.Nodeornengo.ensemble.Neuronsornengo_dl.TensorNode The Nengo object whose output corresponds to the given input of this layer.
- obj
-
input_shape(node_id, full_shape=False)[source]¶ Returns the input shape of the given node.
- Parameters
- node_idint
The node whose shape we want to look up.
- full_shapebool
Whether or not the returned shape should include the batch/time dimensions.
- Returns
- shape(list of) tuple of int
A single tuple shape if the node has one input, or a list of shapes if the node as multiple inputs.
-
output_shape(node_id, full_shape=False)[source]¶ Returns the output shape of the given node.
- Parameters
- node_idint
The node whose shape we want to look up.
- full_shapebool
Whether or not the returned shape should include the batch/time dimensions.
- Returns
- shape(list of) tuple of int
A single tuple shape if the node has one output, or a list of shapes if the node as multiple outputs.
-
static
set_trainable(obj, trainable)[source]¶ Set trainable config attribute of object.
- Parameters
- obj
NengoObject The object to be assigned.
- trainable: bool
Trainability value of
obj.
- obj
-
classmethod
convertible(layer, converter)[source]¶ Check whether the given Keras layer is convertible to native Nengo objects.
- Parameters
- layer
tf.keras.layers.Layer The Keras Layer we want to convert.
- converter
Converter The Converter object running the conversion process.
- layer
- Returns
- convertiblebool
True if the layer can be converted to native Nengo objects, else False.
-
class
nengo_dl.converter.ConvertModel(layer, converter)[source]¶ Convert
tf.keras.Modelto Nengo objects.
-
class
nengo_dl.converter.ConvertSequential(seq_model, converter)[source]¶ Convert
tf.keras.Sequentialto Nengo objects.
-
class
nengo_dl.converter.ConvertFallback(*args, **kwargs)[source]¶ Convert layers which do not have a native Nengo equivalent into a
TensorNode.
-
class
nengo_dl.converter.ConvertAvgPool(layer, converter)[source]¶ Base class for converting average pooling layers to Nengo objects.
-
convert(node_id, dimensions)[source]¶ Convert the given node of this layer to Nengo objects
- Parameters
- node_idint
The index of the inbound node to be converted.
- Returns
- output
NengoObject Nengo object whose output corresponds to the output of the Keras layer node.
- output
-
classmethod
convertible(layer, converter)[source]¶ Check whether the given Keras layer is convertible to native Nengo objects.
- Parameters
- layer
tf.keras.layers.Layer The Keras Layer we want to convert.
- converter
Converter The Converter object running the conversion process.
- layer
- Returns
- convertiblebool
True if the layer can be converted to native Nengo objects, else False.
-
-
class
nengo_dl.converter.ConvertActivation(layer, converter)[source]¶ Convert
tf.keras.layers.Activationto Nengo objects.
-
class
nengo_dl.converter.ConvertAdd(layer, converter)[source]¶ Convert
tf.keras.layers.Addto Nengo objects.
-
class
nengo_dl.converter.ConvertAverage(layer, converter)[source]¶ Convert
tf.keras.layers.Averageto Nengo objects.
-
class
nengo_dl.converter.ConvertAvgPool1D(layer, converter)[source]¶ Convert
tf.keras.layers.AvgPool1Dto Nengo objects.Also works for
tf.keras.layers.GlobalAvgPool1D, andtf.keras.layers.MaxPool1D/GlobalMaxPool1D(ifmax_to_avg_pool=True).
-
class
nengo_dl.converter.ConvertAvgPool2D(layer, converter)[source]¶ Convert
tf.keras.layers.AvgPool2Dto Nengo objects.Also works for
tf.keras.layers.GlobalAvgPool2D, andtf.keras.layers.MaxPool2D/GlobalMaxPool2D(ifmax_to_avg_pool=True).
-
class
nengo_dl.converter.ConvertAvgPool3D(layer, converter)[source]¶ Convert
tf.keras.layers.AvgPool3Dto Nengo objects.Also works for
tf.keras.layers.GlobalAvgPool3D, andtf.keras.layers.MaxPool3D/GlobalMaxPool3D(ifmax_to_avg_pool=True).
-
class
nengo_dl.converter.ConvertBatchNormalization(layer, converter)[source]¶ Convert
tf.keras.layers.BatchNormalizationto Nengo objects.-
convert(node_id)[source]¶ Convert the given node of this layer to Nengo objects
- Parameters
- node_idint
The index of the inbound node to be converted.
- Returns
- output
NengoObject Nengo object whose output corresponds to the output of the Keras layer node.
- output
-
classmethod
convertible(layer, converter)[source]¶ Check whether the given Keras layer is convertible to native Nengo objects.
- Parameters
- layer
tf.keras.layers.Layer The Keras Layer we want to convert.
- converter
Converter The Converter object running the conversion process.
- layer
- Returns
- convertiblebool
True if the layer can be converted to native Nengo objects, else False.
-
-
class
nengo_dl.converter.ConvertConcatenate(layer, converter)[source]¶ Convert
tf.keras.layers.Concatenateto Nengo objects.-
convert(node_id)[source]¶ Convert the given node of this layer to Nengo objects
- Parameters
- node_idint
The index of the inbound node to be converted.
- Returns
- output
NengoObject Nengo object whose output corresponds to the output of the Keras layer node.
- output
-
classmethod
convertible(layer, converter)[source]¶ Check whether the given Keras layer is convertible to native Nengo objects.
- Parameters
- layer
tf.keras.layers.Layer The Keras Layer we want to convert.
- converter
Converter The Converter object running the conversion process.
- layer
- Returns
- convertiblebool
True if the layer can be converted to native Nengo objects, else False.
-
-
class
nengo_dl.converter.ConvertConv(layer, converter)[source]¶ Base class for converting convolutional layers to Nengo objects.
-
class
nengo_dl.converter.ConvertConv1D(layer, converter)[source]¶ Convert
tf.keras.layers.Conv1Dto Nengo objects.
-
class
nengo_dl.converter.ConvertConv2D(layer, converter)[source]¶ Convert
tf.keras.layers.Conv2Dto Nengo objects.
-
class
nengo_dl.converter.ConvertConv3D(layer, converter)[source]¶ Convert
tf.keras.layers.Conv3Dto Nengo objects.
-
class
nengo_dl.converter.ConvertDense(layer, converter)[source]¶ Convert
tf.keras.layers.Denseto Nengo objects.
-
class
nengo_dl.converter.ConvertFlatten(layer, converter)[source]¶ Convert
tf.keras.layers.Flattento Nengo objects.
-
class
nengo_dl.converter.ConvertInput(layer, converter)[source]¶ Convert
tf.keras.layers.InputLayerto Nengo objects.
-
class
nengo_dl.converter.ConvertReLU(layer, converter)[source]¶ Convert
tf.keras.layers.ReLUto Nengo objects.
-
class
nengo_dl.converter.ConvertLeakyReLU(layer, converter)[source]¶ Convert
tf.keras.layers.LeakyReLUto Nengo objects.
-
class
nengo_dl.converter.ConvertReshape(layer, converter)[source]¶ Convert
tf.keras.layers.Reshapeto Nengo objects.
-
class
nengo_dl.converter.ConvertUpSampling(layer, converter)[source]¶ Base class for converting upsampling layers to Nengo objects.
-
class
nengo_dl.converter.ConvertUpSampling1D(layer, converter)[source]¶ Convert
tf.keras.layers.UpSampling1Dto Nengo objects.
-
class
nengo_dl.converter.ConvertUpSampling2D(layer, converter)[source]¶ Convert
tf.keras.layers.UpSampling2Dto Nengo objects.
-
class
nengo_dl.converter.ConvertUpSampling3D(layer, converter)[source]¶ Convert
tf.keras.layers.UpSampling3Dto Nengo objects.
-
class
nengo_dl.converter.ConvertZeroPadding(layer, converter)[source]¶ Base class for converting zero-padding layers to Nengo objects.
-
class
nengo_dl.converter.ConvertZeroPadding1D(layer, converter)[source]¶ Convert
tf.keras.layers.ZeroPadding1Dto Nengo objects.
-
class
nengo_dl.converter.ConvertZeroPadding2D(layer, converter)[source]¶ Convert
tf.keras.layers.ZeroPadding2Dto Nengo objects.
-
class
nengo_dl.converter.ConvertZeroPadding3D(layer, converter)[source]¶ Convert
tf.keras.layers.ZeroPadding3Dto Nengo objects.
-
class
nengo_dl.converter.ConvertKerasSpiking(layer, converter)[source]¶ Base class for converting KerasSpiking layers.
-
class
nengo_dl.converter.ConvertSpikingActivation(layer, converter)[source]¶ Convert
keras_spiking.SpikingActivationto Nengo objects.-
convert(node_id)[source]¶ Convert the given node of this layer to Nengo objects
- Parameters
- node_idint
The index of the inbound node to be converted.
- Returns
- output
NengoObject Nengo object whose output corresponds to the output of the Keras layer node.
- output
-
classmethod
convertible(layer, converter)[source]¶ Check whether the given Keras layer is convertible to native Nengo objects.
- Parameters
- layer
tf.keras.layers.Layer The Keras Layer we want to convert.
- converter
Converter The Converter object running the conversion process.
- layer
- Returns
- convertiblebool
True if the layer can be converted to native Nengo objects, else False.
-
-
class
nengo_dl.converter.ConvertLowpassAlpha(layer, converter)[source]¶ Convert
keras_spiking.Lowpassto Nengo objects.-
convert(node_id)[source]¶ Convert the given node of this layer to Nengo objects
- Parameters
- node_idint
The index of the inbound node to be converted.
- Returns
- output
NengoObject Nengo object whose output corresponds to the output of the Keras layer node.
- output
-
classmethod
convertible(layer, converter)[source]¶ Check whether the given Keras layer is convertible to native Nengo objects.
- Parameters
- layer
tf.keras.layers.Layer The Keras Layer we want to convert.
- converter
Converter The Converter object running the conversion process.
- layer
- Returns
- convertiblebool
True if the layer can be converted to native Nengo objects, else False.
-
-
class
nengo_dl.converter.ConvertTimeDistributed(layer, converter)[source]¶ Convert
tf.keras.layers.TimeDistributedto Keras objects.-
convert(node_id)[source]¶ Convert the given node of this layer to Nengo objects
- Parameters
- node_idint
The index of the inbound node to be converted.
- Returns
- output
NengoObject Nengo object whose output corresponds to the output of the Keras layer node.
- output
-
classmethod
convertible(layer, converter)[source]¶ Check whether the given Keras layer is convertible to native Nengo objects.
- Parameters
- layer
tf.keras.layers.Layer The Keras Layer we want to convert.
- converter
Converter The Converter object running the conversion process.
- layer
- Returns
- convertiblebool
True if the layer can be converted to native Nengo objects, else False.
-
Utilities¶
Utility objects used throughout the code base.
Remove illegal TensorFlow name characters from string. |
|
Get the name of the callable object |
|
Decorator that ensures the output of |
|
Handles progress bar display for some tracked process. |
|
A progress bar representing a sub-task within an overall progress bar. |
|
A progress bar that does nothing. |
-
nengo_dl.utils.sanitize_name(name)[source]¶ Remove illegal TensorFlow name characters from string.
Valid TensorFlow name characters are
[A-Za-z0-9_.\-/]- Parameters
- namestr
Name to be sanitized
- Returns
- sanitizedstr
Sanitized name
-
nengo_dl.utils.function_name(func, sanitize=True)[source]¶ Get the name of the callable object
func.- Parameters
- funccallable
Callable object (e.g., function, callable class)
- sanitizebool
If True, remove any illegal TensorFlow name characters from name
- Returns
- namestr
Name of
func(optionally sanitized)
-
nengo_dl.utils.align_func(output_dtype)[source]¶ Decorator that ensures the output of
funcis a validndarraywith the given dtype.- Parameters
- output_dtypestr or
tf.DTypeordtype Desired dtype of function output(s)
- output_dtypestr or
- Raises
nengo.exceptions.SimulationErrorIf the function returns
Noneor a non-finite value.
-
class
nengo_dl.utils.ProgressBar(present='', past=None, max_value=1, **kwargs)[source]¶ Handles progress bar display for some tracked process.
- Parameters
- presentstr
Description of process in present (e.g., “Simulating”)
- paststr
Description of process in past (e.g., “Simulation”)
- max_valueint or None
The maximum number of steps in the tracked process (or
Noneif the maximum number of steps is unknown)
Notes
Launches a separate thread to handle the progress bar display updates.
Initializes a progress bar with sane defaults
-
sub(msg=None, **kwargs)[source]¶ Creates a new progress bar for tracking a sub-process.
- Parameters
- msgstr
Description of sub-process
-
property
max_steps¶ Alias for max_value to allow this to work with Nengo progress bar interface.
-
class
nengo_dl.utils.SubProgressBar(present='', past=None, max_value=1, **kwargs)[source]¶ A progress bar representing a sub-task within an overall progress bar.
Initializes a progress bar with sane defaults
Benchmarks¶
Benchmark networks and utilities for evaluating NengoDL’s performance.
Circular convolution (EnsembleArray) benchmark. |
|
Single integrator ensemble benchmark. |
|
PES learning rule benchmark. |
|
Basal ganglia network benchmark. |
|
A network designed to stress-test tensor layers (based on mnist net). |
|
Builds the Spaun network. |
|
A randomly interconnected network of ensembles. |
|
A network containing a single Legendre Memory Unit cell and dense readout. |
|
Run profiler on a benchmark network. |
-
nengo_dl.benchmarks.cconv(dimensions, neurons_per_d, neuron_type)[source]¶ Circular convolution (EnsembleArray) benchmark.
- Parameters
- dimensionsint
Number of dimensions for vector values
- neurons_per_dint
Number of neurons to use per vector dimension
- neuron_type
NeuronType Simulation neuron type
- Returns
- net
nengo.Network benchmark network
- net
-
nengo_dl.benchmarks.integrator(dimensions, neurons_per_d, neuron_type)[source]¶ Single integrator ensemble benchmark.
- Parameters
- dimensionsint
Number of dimensions for vector values
- neurons_per_dint
Number of neurons to use per vector dimension
- neuron_type
NeuronType Simulation neuron type
- Returns
- net
nengo.Network benchmark network
- net
-
nengo_dl.benchmarks.pes(dimensions, neurons_per_d, neuron_type)[source]¶ PES learning rule benchmark.
- Parameters
- dimensionsint
Number of dimensions for vector values
- neurons_per_dint
Number of neurons to use per vector dimension
- neuron_type
NeuronType Simulation neuron type
- Returns
- net
nengo.Network benchmark network
- net
-
nengo_dl.benchmarks.basal_ganglia(dimensions, neurons_per_d, neuron_type)[source]¶ Basal ganglia network benchmark.
- Parameters
- dimensionsint
Number of dimensions for vector values
- neurons_per_dint
Number of neurons to use per vector dimension
- neuron_type
NeuronType Simulation neuron type
- Returns
- net
nengo.Network benchmark network
- net
-
nengo_dl.benchmarks.mnist(use_tensor_layer=True)[source]¶ A network designed to stress-test tensor layers (based on mnist net).
- Parameters
- use_tensor_layerbool
If True, use individual tensor_layers to build the network, as opposed to a single TensorNode containing all layers.
- Returns
- net
nengo.Network benchmark network
- net
-
nengo_dl.benchmarks.spaun(dimensions)[source]¶ Builds the Spaun network.
See [1] for more details.
- Parameters
- dimensionsint
Number of dimensions for vector values
- Returns
- net
nengo.Network benchmark network
- net
Notes
This network needs to be installed via
pip install git+https://github.com/drasmuss/spaun2.0.git
References
- 1
Chris Eliasmith, Terrence C. Stewart, Xuan Choo, Trevor Bekolay, Travis DeWolf, Yichuan Tang, and Daniel Rasmussen (2012). A large-scale model of the functioning brain. Science, 338:1202-1205.
-
nengo_dl.benchmarks.random_network(dimensions, neurons_per_d, neuron_type, n_ensembles, connections_per_ensemble, seed=0)[source]¶ A randomly interconnected network of ensembles.
- Parameters
- dimensionsint
Number of dimensions for vector values
- neurons_per_dint
Number of neurons to use per vector dimension
- neuron_type
NeuronType Simulation neuron type
- n_ensemblesint
Number of ensembles in the network
- connections_per_ensembleint
Outgoing connections from each ensemble
- Returns
- net
nengo.Network benchmark network
- net
-
nengo_dl.benchmarks.lmu(theta, input_d, native_nengo=False, dtype='float32')[source]¶ A network containing a single Legendre Memory Unit cell and dense readout.
See [1] for more details.
- Parameters
- thetaint
Time window parameter for LMU.
- input_dint
Dimensionality of input signal.
- native_nengobool
If True, build the LMU out of Nengo objects. Otherwise, build the LMU directly in TensorFlow, and use a
TensorNodeto wrap the whole cell.- dtypestr
Float dtype to use for internal parameters of LMU when
native_nengo=False(native_nengo=Truewill use the dtype of the Simulator).
- Returns
- net
nengo.Network Benchmark network
- net
References
- 1
Aaron R. Voelker, Ivana Kajić, and Chris Eliasmith. Legendre memory units: continuous-time representation in recurrent neural networks. In Advances in Neural Information Processing Systems. 2019. https://papers.nips.cc/paper/9689-legendre-memory-units-continuous-time-representation-in-recurrent-neural-networks.
-
nengo_dl.benchmarks.run_profile(net, train=False, n_steps=150, do_profile=True, reps=1, dtype='float32', **kwargs)[source]¶ Run profiler on a benchmark network.
- Parameters
- net
Network The nengo Network to be profiled.
- trainbool
If True, profile the
Simulator.fitfunction. Otherwise, profile theSimulator.runfunction.- n_stepsint
The number of timesteps to run the simulation.
- do_profilebool
Whether or not to run profiling
- repsint
Repeat the run this many times (only profile data from the last run will be kept).
- dtypestr
Simulation dtype (e.g. “float32”)
- net
- Returns
- exec_timefloat
Time (in seconds) taken to run the benchmark, taking the minimum over
reps.
Notes
kwargs will be passed on to
Simulator
Interface¶
The benchmark module also includes a command-line interface for building and running the benchmarks:
benchmarks¶
Command-line interface for benchmarks.
benchmarks [OPTIONS] COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]...
build¶
Builds one of the benchmark networks
benchmarks build [OPTIONS]
Options
-
--benchmark<benchmark>¶ Name of benchmark network
-
--dimensions<dimensions>¶ Number of dimensions
-
--neurons_per_d<neurons_per_d>¶ Neurons per dimension
-
--neuron_type<neuron_type>¶ Nengo neuron model
-
--kwarg<kwarg>¶ Arbitrary kwarg to pass to benchmark network (key=value)
profile¶
Runs profiling on a network (call after ‘build’)
benchmarks profile [OPTIONS]
Options
-
--train,--no-train¶ Whether to profile training (as opposed to running) the network
-
--n_steps<n_steps>¶ Number of steps for which to run the simulation
-
--batch_size<batch_size>¶ Number of inputs to the model
-
--device<device>¶ TensorFlow device on which to run the simulation
-
--unroll<unroll>¶ Number of steps for which to unroll the simulation
-
--time-only¶ Only count total time, rather than profiling internals