Miscellaneous utilities


Remove illegal TensorFlow name characters from string.

Valid TensorFlow name characters are [A-Za-z0-9_.\-/]

name : str

Name to be sanitized


Sanitized name

nengo_dl.utils.function_name(func, sanitize=True)[source]

Get the name of the callable object func.

func : callable

Callable object (e.g., function, callable class)

sanitize : bool, optional

If True, remove any illegal TensorFlow name characters from name


Name of func (optionally sanitized)

nengo_dl.utils.align_func(output_shape, output_dtype)[source]

Decorator that ensures the output of func is an ndarray with the given shape and dtype.

output_shape : tuple of int

Desired shape for function output (must have the same size as actual function output)

output_dtype : tf.DType or dtype

Desired dtype of function output


If the function returns None or a non-finite value.

nengo_dl.utils.print_op(input, message)[source]

Inserts a print statement into the TensorFlow graph.

input : tf.Tensor

The value of this tensor will be printed whenever it is computed in the graph

message : str

String prepended to the value of input, to help with logging


New tensor representing the print operation applied to input


This is what tf.Print is supposed to do, but it doesn’t seem to work consistently.

nengo_dl.utils.cast_dtype(dtype, target)[source]

Changes float dtypes to the target dtype, leaves others unchanged.

Used to map all float values to a target precision. Also casts numpy dtypes to TensorFlow dtypes.

dtype : tf.DType or dtype

Input dtype to be converted

target : tf.DType

Floating point dtype to which all floating types should be converted


Input dtype, converted to target type if necessary

nengo_dl.utils.find_non_differentiable(inputs, outputs)[source]

Searches through a TensorFlow graph to find non-differentiable elements between inputs and outputs (elements that would prevent us from computing d_outputs / d_inputs.

inputs : list of tf.Tensor

Input tensors

outputs : list of tf.Tensor

Output tensors

class nengo_dl.utils.ProgressBar(max_steps, label=None)[source]

Displays a progress bar and ETA for tracked steps.

max_steps : int

Number of steps required to complete the tracked process

label : str, optional

A description of what is being tracked


Reset the tracker to initial conditions.


Stop the progress tracker.

Normally this will be called automatically when max_steps is reached, but it can be called manually to trigger an early finish.


Increment the progress tracker one step.

msg : str, optional

Display the given string at the end of the progress bar

nengo_dl.utils.minibatch_generator(inputs, targets, minibatch_size, shuffle=True, rng=None)[source]

Generator to yield minibatch_sized subsets from inputs and targets.

inputs : dict of {Node: ndarray}

Input values for Nodes in the network

targets : dict of {Probe: ndarray}

Desired output value at Probes, corresponding to each value in inputs

minibatch_size : int

The number of items in each minibatch

shuffle : bool, optional

If True, the division of items into minibatches will be randomized each time the generator is created

rng : RandomState, optional

Seeded random number generator

inputs : dict of {Node: ndarray}

The same structure as inputs, but with each array reduced to minibatch_size elements along the first dimension

targets : dict of {Probe: ndarray}

The same structure as targets, but with each array reduced to minibatch_size elements along the first dimension


Pass settings to nengo_dl by setting them as parameters on the top-level Network config.

The settings are passed as keyword arguments to configure_settings; e.g., to set trainable use configure_settings(trainable=True).

trainable : bool or None

Adds a parameter to Nengo Ensembles/Connections/Networks that controls whether or not they will be optimized by Simulator.train(). Passing None will use the default nengo_dl trainable 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.

planner : graph planning algorithm

Pass one of the graph planners to change the default planner.