Beginner question: How to handle tensor input shapes for a simple FFNN?


I’m new to NNs and pytorch!

I have some data being provided to my model via a DataLoader with the batch size set to 8. The shape of my input is (8, 15, 50).

I want to make a simple FFNN to classify my data, there are 50 different classes for my data.
So, I’m trying to do Linear, ReLU, Linear, ReLU, Linear (two hidden layers and then a final Linear that outputs a 50-dimensional vector).

Given that the initial input is (batch_size, 15, 50), what do I need to do to the shape so that the initial Linear will work on it? I thought it should be 15*50 on the first layer? But I think I need to reshape the (8, 15, 50) input?

Thank you.

I don’t know what the two dimensions in your input mean, but assuming they represent independent features, you could use:

x = x.view(x.size(0), -1)

to flatten the input into the shape [batch_size=8, features=15*50].
The in_features of the first linear layer would also be set to 15*50 in this case.

Note that nn.Linear layers accept multiple dimensions, where the last dim would correspond to the in_features while the other additional dimensions would be used to reapply the linear layer on them.
This could be used in case your dim1 dimension represent a temporal dimension and you would like to apply the model on all time steps in it.