Search code examples
deep-learningneural-networkpytorchsequential

Rearrange neural network layers in torch.nn.Sequential


I'm looking forward to finding a way for rearranging a sequential, because I'm trying to build a reversible convolutional neural network and I have many layers and I just want to reverse the order of layers in that sequential. For example

self.features.append(nn.Conv2d(1, 6, 5))
self.features.append(nn.LeakyReLU())
self.features = nn.Sequential(*self.features)

and then I just want to reverse that and first have activation and then convolution. I know this sample is easy but In my case I have many layers and I can't do it by writing the reverse path.


Solution

  • Try this:

    nn.Sequential(*reversed([layer for layer in original_sequential]))
    

    For example:

    >>> original_sequential = nn.Sequential(nn.Conv2d(1,6,5), nn.LeakyReLU())
    >>> original_sequential
    Sequential(
      (0): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1))
      (1): LeakyReLU(negative_slope=0.01)
    )
    >>> nn.Sequential(*reversed([layer for layer in original_sequential]))
    Sequential(
      (0): LeakyReLU(negative_slope=0.01)
      (1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1))
    )