CAddTable replacement in PyTorch


#1

I was porting the ResNet code from Torch (Lua Script) to PyTorch. I want to port the CAddTable module which adds two tensors:

return nn.Sequential()
   :add(nn.ConcatTable()
      :add(s)
      :add(shortcut(nInputPlane, n, stride)))
   :add(nn.CAddTable(true))
   :add(ReLU(true))

Apparently PyTorch used to have CAddTable until version 0.4 which has now been deprecated (link).

What is the most suitable replacement for CAddTable in PyTorch? Goes without saying but I have two branches of nn.Sequential() modules which I want to merge together using CAddTable into one nn.Sequential() module.

I tried to create another module like this but the output is a Torch tensor, not an nn.Sequential() module:

class my_CAddTable(nn.Module):
	def __init__(self):
		super(my_CAddTable, self).__init__()
	def forward(self, x, y):
		return torch.add(x, y)

Any leads are highly appreciated.


#2

I found the solution to this and would like to share here for anyone reading in the future:

nn.Sequential() is not suitable in this case. Instead, you can use nn.ModuleList and manually create a module that performs the addition.