# Transpose convolution

In the U-net videos where the transpose convolution is explained, i don’t understand how the filter size, the padding and the strides are chosen to match the output size, is there a formula for that (like there was for the normal convolution) ?

1 Like

Hi @houzefa303 ,
Yes, there is a formula to calculate the output that you can check in the video Stride Conv, Week1.
Anyway, the formula is

((n + 2p -f )/s) + 1

Where:
n: input size
f: filter size
s: stride

I think that’s the formula for the Normal Convolution
Is there a similar one for the Transpose Convolution ?

This paper has lots of detail about the arithmetic of convolutions.

2 Likes

As the name suggests, the formula for the transpose convolution is the “opposite” of the one for the regular (forward) convolution. Following @carlosrl’s notations:

Convolution: nout = ((n + 2p - f)/s + 1
Transpose convolution: nout = s(n - 1) + f - 2p

2 Likes

There is some misunderstanding: if n+2p-f is not divisible by s, then what formula can we use?

There is some misunderstanding: if n+2p-f is not divisible by s, then what formula can we use

You should really set n, p, and f so that the sum n+2p-f will be divisible by s. That’s how you get nout

1 Like

I think there is a misunderstanding of the formula for computing n_out in the convolution case. It is not required that n + 2p - f is divisible by s. That is why there is the “floor” function in the formula. Here is the correct formula:

n_{out} = \displaystyle \lfloor \frac {n_{in} +2p - f}{s} \rfloor + 1

So you just perform the division and then truncate it to the next lowest integer and add 1. Prof Ng gave the formula this way in the lectures and it’s also given in the notebook in ConvNets Week 1 Step by Step Assignment. Here’s a screenshot of that section of the notebook:

yanivhMentor maybe he is right, what do you think?