Miscellaneous utilities

nengo_dl.utils.sanitize_name(name)[source]

Remove illegal Tensorflow name characters from string.

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

Parameters:
name : str

name to be sanitized

Returns:
str

sanitized name

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

Get the name of the callable object func.

Parameters:
func : callable

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

sanitize : bool, optional

if True, remove any illegal Tensorflow name characters from name

Returns:
str

(sanitized) name of func

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.

Raises a SimulationError if the function returns None or a non-finite value.

Parameters:
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

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

Inserts a print statement into the tensorflow graph.

Parameters:
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

Returns:
``tf.Tensor``

new tensor representing the print operation applied to input

Notes

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.

Parameters:
dtype : tf.DType or dtype

input dtype to be converted

target : tf.DType

floating point dtype to which all floating types should be converted

Returns:
``tf.DType``

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.

Parameters:
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.

Parameters:
max_steps : int

number of steps required to complete the tracked process

label : str, optional

a description of what is being tracked

reset()[source]

Reset the tracker to initial conditions.

stop()[source]

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.

step(msg=None)[source]

Increment the progress tracker one step.

Parameters:
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.

Parameters:
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

random number generator

Yields:
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

nengo_dl.utils.configure_trainable(config, default=None)[source]

Adds a configurable attribute called trainable to trainable objects.

Used to manually configure whether or not those parts of the model can be optimized by Simulator.train().

Parameters:
config : Config or Network

the config object to be modified (or a Network to modify net.config)

default : bool, optional

the default value for trainable (None means that the value is deferred to the parent config, or True if this is the top-level config)