Tom Hale
Tom Hale

Reputation: 46715

PyTorch: get input layer size

I want to programatically find the size of my input layer.

If my first layer is called fc1, how do I find out its input?

Upvotes: 5

Views: 4702

Answers (1)

Tom Hale
Tom Hale

Reputation: 46715

Assuming your model is called model, this will give the number of input features of the fc1 layer:

model.fc1.in_features

This is useful inside the .forward() method:

def forward(self, x):
    x = x.view(-1, self.fc1.in_features)  # resize the input to match the input layer
    ...

Upvotes: 4

Related Questions