How to use tf.keras.layers.Add() with Nengo_dl

I tried to write the following line of code
x = nengo_dl.Layer(tf.keras.layers.Add())([x_1, x_2, x_3, x_4])
However I got the error
AttributeError: 'list' object has no attribute 'size_out'
How do I use the Add() layer

Indeed! The nengo_dl.Layer class only works with single input/outputs at the moment, however, we do have a Github issue open to support multi-inputs/outputs.

As for your question, there are two ways you can implement the Keras Add layer in your NengoDL model. If you are comfortable to define your entire model in TensorFlow, you can use the nengo_dl.Converter to convert the TensorFlow model to a NengoDL model without needing to manually define the Keras layers as nengo_dl.Layer's.

However, if you are splicing TensorFlow code into your Nengo model, you can define a custom nengo_dl.TensorNode to compute the Keras Add layer. Here’s some example code to get you started:

    # Wrapper function to project a single-input into something that the Keras Add 
    # layer can use
    def add(x):
        x0 = x[:, : layer1.size_out]
        x1 = x[:, layer1.size_out :]
        added = tf.keras.layers.Add()([x0, x1])
        return added

    # Define the custom TensorNode that uses the wrapped Keras Add layer
    layer3 = nengo_dl.TensorNode(
        add,
        shape_in=(layer1.size_out + layer2.size_out,),
        shape_out=(layer1.size_out,),
        pass_time=False,
    )

    # Make the connections from the input layers to the TensorNode. Note the slicing
    # operations used to project the outputs of layer1 and layer2 onto their respective
    # portions of the combined input to layer3. Also, not that `synapse=None` is set to
    # avoid filtering the outputs of layers 1 and 2.
    nengo.Connection(layer1, layer3[: layer1.size_out], synapse=None)
    nengo.Connection(layer2, layer3[layer1.size_out :], synapse=None)

I’ve implemented this code in an example NengoDL model here: test_dl_add.py (2.4 KB)
You can play around with the code to test adding 4 layers together (my example code only adds 2 layers together). :smiley:

1 Like

Yep! This works! Thanks a tonne.