Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to auto-size the subsequent input of a layer following torch.nn.Flatten within torch.nn.Sequential in PyTorch?

If I have the following model class for example:

class MyTestModel(nn.Module):

    def __init__(self):
        super(MyTestModel, self).__init__()

        self.seq1 = nn.Sequential(
            nn.Conv2d(3, 6, 3),
            nn.MaxPool2d(2, 2),
            nn.Conv2d(6, 16, 3),
            nn.MaxPool2d(2, 2),
            nn.Flatten(),
            nn.Linear(myflattendinput(), 120), # how to automate this?
            nn.ReLU(),
            nn.Linear(120, 84),
            nn.ReLU(),
            nn.Linear(84, 2),
        )
        self.softmax = nn.Softmax(dim=1)

    def forward(self, x):

        x = self.seq1(x)
        x = self.softmax(x)
        return x

I know, normally you would let the data loader give a fixed size input to the model, thus having a fixed size for the input of the layer after nn.Flatten(), however I was wondering if you could somehow compute this automatically?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

PyTorch (>=1.8) has LazyLinear which infers the input dimension.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading