Hi,
I am trying to understand how to process batches in an nn.Linear layer. Since the nn.Linear function is defined using (in_features, out_features) I am not sure how I should handle them when I have batches of data. I am currently processing all batches at once in the forward pass, using
# input_for_linear has the shape [nr_of_observations, batch_size, in_features]
input_for_linear.view(-1, batch_size * in_features)                     

as my input - i.e. flattening all the batches out. My linear layer is defined as:
linear = nn.Linear(batch_size * in_features, out_features)

This process however saves an unnecessary amount of parameters in the linear layer as it differentiates between observations in each batch. With lots of data and small batch sizes it averages out over many epochs so it is maybe not so crucial to change? (right?)
Should I instead do a for-loop over my batches in the input_for_linear and use these in a layer defined with:
linear = nn.Linear(in_features, out_features)

And then lastly combine each of the outputs back to a batch? The results from the linear layers are used in a Variational Auto Encoder with LSTM’s which are capable of handling batched data.
Perhaps there is a smarter solution?
Kind Regards,
Jonas