Automatic calculation of layer sizes
🚀 Feature
Calculate number of layer inputs (especially linear layers) automatically, without having to resort to manual calculation or print
Motivation
I have a model that receives multiple images as inputs. Each input goes through some number of convolution/pool layers, each with potentially different kernel sizes/stride/etc. All of these are hyperparameters that are being tuned
The output from those layers gets flattened, merged, and passed into a linear layer
The problem here is that during init, the linear layer needs to know how many input connections it needs. I cant hard code this value because hyperparameter tuning means I wont know ahead of time what the size of each image will be post-convolution/pool. I cant resort to using print to check the size, because the size will change between each tuning run. print is fine if you're dealing with a fixed architecture, but doesnt cut it when tuning parameters that end up changing the architecture
I was working on a personal project recently where I had to calculate and combine all the output sizes and it ended up being about 100 lines in total: https://github.com/sho-87/webcam-eye-tracker/blob/master/Models.py#L299-L397
Am I just doing this slowly/inefficiently? Is there a saner way to do this without all the hand calculation? This is a relatively simple case without tuning things like stride. I expect it to get more complicated in those situations.
If this is the best we have currently, then consider this a feature request for automatic calculation of those sizes, because this feels quite excessive in terms of boilerplate. I would essentially like to just replace line 391 in my code with something like self.fc1 = nn.Linear(None, config["dense_nodes"]), and have the None replaced for me
As a side note, we can set self.example_input_array to log the graph to tensorboard, so arent these layer sizes already stored somewhere in a lightning module? Maybe we can use that, or a similar idea, to avoid hand calculating everything?