How to set up a class for a TensorNode that accepts a batch of inputs when called?

Hi, thank you for developing this great project.

I’m having trouble understanding how to use a class with a TensorNode. Specifically I can’t figure out how to set up a class that takes a batch of inputs; the example in the documentation only uses one image.

I’ve tried to make a minimal example that reproduces my issue.

MNISTY_SHAPE = (28, 28, 1)
SIZE_OUT = 10

class SimpleNode:
    def __call__(self, t, x):
        img = tf.reshape(tf.cast(x, tf.float32), (-1,) + MNISTY_SHAPE)
        conv1 = tf.layers.conv2d(img, filters=32, kernel_size=(5,5), strides=(3,3), padding='VALID')
        maxpool1 = tf.layers.max_pooling2d(conv1,
                                  pool_size=(2,2),
                                  strides=(2,2),
                                  padding='VALID')
        input_shape = maxpool1.get_shape().as_list()[1:]
        n_input_units = np.prod(input_shape)
        n_output_units = SIZE_OUT
        weights_shape = [n_input_units, n_output_units]
        fc1W = tf.get_variable(name='fc1W_weights',
                                  shape=weights_shape)
        fc1b = tf.get_variable(name='fc1W_biases',
                               initializer=lambda shape, dtype, partition_info: tf.zeros(shape=shape, dtype=dtype),
                               shape=[n_output_units])
        fc1 = tf.nn.relu_layer(
            tf.reshape(maxpool1, [-1, int(np.prod(maxpool1.get_shape()[1:]))]), fc1W, fc1b)
        probabilities = tf.nn.softmax(fc1, name='probabilities')

net = nengo.Network()

with net:
    input_shape = np.prod(MNISTY_SHAPE)
    input_node = nengo.Node(output=np.zeros(MNISTY_SHAPE).flatten())
    simplenode = nengo_dl.TensorNode(SimpleNode(),size_in=input_shape,size_out=SIZE_OUT)
    nengo.Connection(input_node, simplenode, synapse=None)

minibatch_size = 20
sim = nengo_dl.Simulator(net, minibatch_size=minibatch_size)

When I run this, I get a crash, I think it’s from the Tensorflow “VM” after the sim has been built.

# ...(very long traceback from my_environment/site-packages/tensorflow)...
ValueError: Tried to convert 'x' to a tensor and failed. Error: None values not supported.

Is there something I’m doing wrong in setting up the __call__ function of the class?
I’m guessing it’s because there’s a None in the shape of the tensor being passed as x?


Sharp & Fellows, Inc.

Hi @domanton, and welcome to the Nengo forums! :smiley:

I tried running the example you included in your post, but I could not get it to run without TensorFlow import errors. It looks like you might be using an older version of TensorFlow? As an example, there is no tf.layers.conv2d class in TF 2.4.0. What versions of TensorFlow, Nengo, and NengoDL are you using? You can find this with the following python code:

import tensorflow as tf
import nengo_dl
import nengo

print(tf.__version__)
print(nengo_dl.__version__)
print(nengo.__version__)

From the NengoDL documentation, the latest version of NengoDL is only compatible with TF 2.2.0 to TF 2.4.0.