PyTorch equivalent of Keras 'Add' type layer

My keras summary model shows that it has a add layer to sum few layers. Screenshot in drive link below -

I am a beginner and I don’t know how to implement the add layer in PyTorch. Actually I need to sum few layers convolution layers with previous layers. Please see the attached screenshot. Screenshot from 2021-10-28 17-21-56

I am converting a model from keras to PyTorch. All other conv2d, batch_normalization2d layers are similar in pytorch but I am not getting the equivalent of ‘Add’ type layer of Keras. Please help!

You can directly add PyTorch tensors in your forward without the need to use a layer:

def forward(self, x):
    out1 = self.layer1(x)
    out2 = self.layer1(x)
    out3 = self.layer1(x)
    out = out1 + out2 + out3
    ...

Alternatively, you could also create a custom nn.Module, pass the inputs to this module, and add them in the forward (although it sounds quite cumbersome given that a + will also do it :wink: ).

1 Like