Calculation for the input to the Fully Connected Layer

Hi,

Let’s say you have already implemented your model, but you do not know what is the proper input number of neurons for linear layer.

The manual approach would be like this:
You can run your model and add a print(x.shape) in forward function right after self.pool. It will print final shape something like [batch, channel, height, width]. So, you just multiply them as the number of inputs.

About automatic way, you can do this way:

class Net(nn.Module):
    def __init__(self):
        ...

    def forward(self, x):
        ...
        x = ...  # let's say x is the output of self.pool
        self.linear_input_size = x.view(-1, x.shape[-1]).shape[0]
        ...

This thread may be able to help.

bests

1 Like