Shern
Shern

Reputation: 831

Why is CNN convolution output size in PyTorch DQN tutorial computed with `kernel_size -1`?

Based on my understanding, CNN output size for 1D is

output_size = (input_size - kernel_size + 2*padding)//stride + 1

Refer to PyTorch DQN Tutorial. In the tutorial, it uses 0 padding, which is fine. However, it computes the output size as follows:

        def conv2d_size_out(size, kernel_size = 5, stride = 2):
            return (size - (kernel_size - 1) - 1) // stride  + 1

It the above a mistake or is there something I missed?

Upvotes: 0

Views: 280

Answers (1)

ddoGas
ddoGas

Reputation: 871

No, it's not a mistake because

size - (kernel_size - 1) - 1 = size - kernel_size + 2 * 0

with 0 as padding
(it's not code, its an equation sorry for the formatting)
I think the tutorial is using the formula for the output size from the official document which is

output_size = ((input_size + 2 * padding - dialation * (kernel_size - 1) - 1) // stride + 1

official doc for conv1d

Upvotes: 2

Related Questions