TensorNodes

TensorNodes allow parts of a model to be defined using TensorFlow and smoothly integrated with the rest of a Nengo model. TensorNodes work very similarly to a regular Node, except instead of executing arbitrary Python code they execute arbitrary TensorFlow code.

tensor_layer() is a utility function for constructing TensorNodes, designed to mimic the layer-based model construction style of many deep learning packages.

API

class nengo_dl.tensor_node.TensorNode(tensor_func, size_in=Default, size_out=Default, label=Default)[source]

Inserts TensorFlow code into a Nengo model. A TensorNode operates in much the same way as a Node, except its inputs and outputs are defined using TensorFlow operations.

The TensorFlow code is defined in a function or callable class (tensor_func). This function accepts the current simulation time as input, or the current simulation time and a Tensor x if node.size_in > 0. x will have shape (sim.minibatch_size, node.size_in), and the function should return a Tensor with shape (sim.minibatch_size, node.size_out). node.size_out will be inferred by calling the function once and checking the output, if it isn’t set when the Node is created.

If tensor_func has a pre_build attribute, that function will be called once when the model is constructed. This can be used to compute any constant values or set up variables – things that don’t need to execute every simulation timestep.

def pre_build(shape_in, shape_out):
    print(shape_in)  # (minibatch_size, node.size_in)
    print(shape_out)  # (minibatch_size, node.size_out)

If tensor_func has a post_build attribute, that function will be called after the simulator is created and whenever it is reset. This can be used to set any random elements in the TensorNode or perform any post-initialization setup required by the node (e.g., loading pretrained weights).

def post_build(sess, rng):
    print(sess)  # the TensorFlow simulation session object
    print(rng)  # random number generator (np.random.RandomState)
Parameters:
tensor_func : callable

A function that maps node inputs to outputs

size_in : int, optional (Default: 0)

The number of elements in the input vector

size_out : int, optional (Default: None)

The number of elements in the output vector (if None, value will be inferred by calling tensor_func)

label : str, optional (Default: None)

A name for the node, used for debugging and visualization

nengo_dl.tensor_node.tensor_layer(input, layer_func, shape_in=None, synapse=None, transform=1, return_conn=False, **layer_args)[source]

A utility function to construct TensorNodes that apply some function to their input (analogous to the tf.layers syntax).

Parameters:
input : nengo.base.NengoObject

Object providing input to the layer

layer_func : callable or NeuronType

A function that takes the value from input (represented as a tf.Tensor) and maps it to some output value, or a Nengo neuron type, defining a nonlinearity that will be applied to input.

shape_in : tuple of int, optional

If not None, reshape the input to the given shape

synapse : float or Synapse, optional

Synapse to apply on connection from input to this layer

transform : ndarray, optional

Transform matrix to apply on connection from input to this layer

return_conn : bool, optional

If True, also return the connection linking this layer to input

layer_args : dict, optional

These arguments will be passed to layer_func if it is callable, or Ensemble if layer_func is a NeuronType

Returns:
:class:`.TensorNode` or :class:`~nengo:nengo.ensemble.Neurons`

A TensorNode that implements the given layer function (if layer_func was a callable), or a Neuron object with the given neuron type, connected to input