Converting 2D to 1D module

The nn.sequential module simplifies writing models very nicely. However, when I need to convert from 2D tensors to 1D tensors before the fully connected layers I use view function which cannot be used within the sequential. I know I can create my own 2d_to_1d module and feed it into sequential but wanted to know if there is a better way to do this. So my question is, what is the best way to change dimensions of a tensor and still use sequential to create a single module that does the whole pipeline?

I have a same problem. did you find any solution for this?

You could create a wrapper module like this:

class Flatten(torch.nn.Module):
    def __init__(self):
        super().__init__()

    def forward(x) 
        return x.view(x.size(0), -1) 

And pass an instance of this module to the Sequential model before the fully connected layers.

2 Likes