Output shape of model?

Is there a good way to get the output shape of a nn.Module object without knowing the input shape? Everything I can come up with seems to need a few extra assumptions on the structure of the network.


Given that the forward function can contain arbitrary python code, I don’t think this is possible. :confused:
You will need extra assumption about the structure of the Module and the submodules it can contain.

Thanks for the response. That’s what I was thinking but I hoped I was wrong. Related to the first question, do you know what defines the order of the state dict? If the last layer will be made up of the last tensors in the state dict then I might be able to get a “close enough” solution.

There is no strict order. It might be the same order as the registration. But someone can register the last layer first and then this will be wrong :confused:

Note that for some models, the output size will also depend on the input that is given (fully convolutional neural nets for example).
But if you know the input size, you can create a dummy input Tensor that you forward to see the output size. That will work for most network (except for the weird ones where the output depends on the values in the input Tensor).

Yeah, I think that’s what I will have to do. I was hoping to avoid the requirement of knowing the shape of the input tensor. Thanks for the help!

1 Like