Why pytorch module needs input shape

Is it always necessary to specify the input shape of any module that is part of a Sequential Layers.
Like in case of keras if you are building a seq layers u dont need to give the input shape of hidden layers just the output shape.

1 Like

I have just worked with CNN so i will just say for that. In CNN u just specify the input and output channels along with other parameters. Input size of image is not specified anywhere.

To the best of my knowledge, PyTorch does not perform implicit shape inference (which is what Keras does) unless each module in the Sequential model has implicit shape inference encoded in its forward method.

I’m not sure of the reasoning behind this, but it may be because TensorFlow (what Keras is built on) uses a static computational graph, while PyTorch uses a dynamic one. This would make it impossible for PyTorch to know the output shape of the 1st layer before it runs, while Tensorflow can analyze the static graph it builds to infer input sizes.

In short: you’ll have to specify input and output sizes.

So, if I load someone else’s model and if I don’t know anything about that model, there is no way I can find out the input shape for that model?

I wouldn’t say there is no way to make the model work and you could of course try out different shapes and check for shape mismatches. Of course, it would be easier, if the author provides this shape information as well as all used preprocessing steps etc.