How to calculate the output size after Conv2d in pytorch?

I am new about pytorch.
the example like this

inputs = torch.rand(1,1,10,10)
mod = nn.Conv2d(1,32,3,2,1)
out = mod(inputs)
print(out.shape)

the output is torch.Size([1, 32, 5, 5])
I think new_width = (old_width+2*padding-kernal_size)/stride +1.
but it cann’t divisible.
So how to calculate it in pytorch?

The complete formula for the output size is given in the docs. If it’s not divisible, the output size seems to be rounded down.

EDIT: new link to the Conv2d docs.

Is there a built in way to compute the output size of layers, without actually running the layer? I am looking for something like:

output_tensor_size = compute_output_size(layer, input_tensor_size)

You could take a look at the different approaches in this thread.

Following package calculates the output dimensions of common torch.nn operations:

You can install it by:

pip install torchshape

Disclaimer: I’m the creator of this package.