简体   繁体   中英

With PyTorch, how is my Conv1d dimension reducing when I have padding?

My conv module is:

        return torch.nn.Sequential(
            torch.nn.Conv1d(
                in_channels=in_channels,
                out_channels=in_channels,
                kernel_size=2,
                stride=1,
                dilation=1,
                padding=1
            ),
            torch.nn.ReLU(),
            torch.nn.Conv1d(
                in_channels=in_channels,
                out_channels=in_channels,
                kernel_size=2,
                stride=1,
                dilation=2,
                padding=1
            ),
            torch.nn.ReLU(),
            torch.nn.Conv1d(
                in_channels=in_channels,
                out_channels=in_channels,
                kernel_size=2,
                stride=1,
                dilation=4,
                padding=1
            ),
            torch.nn.ReLU()
        )

And in forward , I have:

down_out = self.downscale_time_conv(inputs)

inputs has a .size of torch.Size([8, 161, 24]) . I'd expect down_out to have the same size, but instead it has: torch.Size([8, 161, 23])

Where did that last element go?

The answer can be found on Pytorch documentation online ( here ). For every operation the output shape is expressed with respect to the input parameters:

在此处输入图像描述

For each conv1D:

- L1 = 25 → int((24 + 2*1 - 1*(2 - 1) - 1) / 1 + 1)
- L2 = 25 → int((25 + 2*1 - 2*(2 - 1) - 1) / 1 + 1)
- L3 = 23 → int((25 + 2*1 - 4*(2 - 1) - 1) / 1 + 1)

Do not forget that Lin is the previous size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM