# 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.

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