nn.Linear() with variable-sized input

#1

Can nn.Linear() work with variable-sized inputs?

I have an input of shape (2048, x) and would like to convert it to be of shape (2048) using a fully-connected layer – is this possible?

When I passed in x as an argument to the model, the only issue I had was with saving/loading the weights since x would change the number of parameters in the layer. Any ideas about how this issue can be resolved?

(tymokvo) #2

Welcome to the forums!

As you noted, it will “work” in that you can dynamically create an nn.Linear with an input dimension that varies with x. But this necessarily changes the shape of the weight tensor, so, once saved, it will not match all of your data points.

I think the typical solution is to make a nn.Linear that matches the max x value of your data and pad the small data points with zeros.

(balamurali) #3

In my experience, the networks with fully connected networks are restricted by their input image dimensions. Change in image dimensions will result in input to the linear layer having different dimensions. This will throw an error. The fully convolutional networks are designed for this problem of handling multiple image sizes. Because not all data can be resized to the expected dimensions. Imagenet the dimensions are restricted by 224 * 224. It is because of fully connected network only. But in general available datasets will have fixed dimension. So the model can be saved for that specific dimension. Resizing is generally done for classification tasks. Padding is also an alternative.