Adi Nishad
Adi Nishad

Reputation: 23

pytorch layer input, output shape calculation

Can anyone help me to understand when I use conv1d and then a linear layer, What will be the inputs of the linear layer? How to calculate how many input features should I have to pass in pytorch

Upvotes: 0

Views: 949

Answers (1)

C-3PO
C-3PO

Reputation: 1213

In Pytorch, Linear layers operate using only the last dimension of the input tensor: [*features_in] -> [*,features_out].

However, Conv1D layers consider the last 2 dimensions of the input tensor: [batches,channels_in, length_in] -> [batches,channels_out, length_out].

Therefore, if no pre-processing is used, Linear layers will only work with the signals defined for every channel, i.e., [batches,channels_in,features_in] -> [batches,channels_in,features_out]. This behavior is rarely desired, so people usually flatten tensors before passing them to a Linear layer. For example, it's common to use Linear(x.view(n_batches,-1)).

The behavior you need depends on the details of your application. Good luck,

Sources:

https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html (Conv1d) https://pytorch.org/docs/stable/generated/torch.nn.Linear.html (Linear)

Upvotes: 1

Related Questions