Calculating shape of conv1d layer in Pytorch

浪尽此生 提交于 2020-07-10 01:52:10

问题


How we can calculate the shape of conv1d layer in PyTorch. IS there any command to calculate size and shape of these layers in PyTorch.

            nn.Conv1d(depth_1, depth_2, kernel_size=kernel_size_2, stride=stride_size),
            nn.ReLU(),
            nn.MaxPool1d(kernel_size=2, stride=stride_size),
            nn.Dropout(0.25)```

回答1:


The output size can be calculated as shown in the documentation nn.Conv1d - Shape:

The batch size remains unchanged and you already know the number of channels, since you specified them when creating the convolution (depth_2 in this example).

Only the length needs to be calculated and you can do that with a simple function analogous to the formula above:

def calculate_output_length(length_in, kernel_size, stride=1, padding=0, dilation=1):
    return (length_in + 2 * padding - dilation * (kernel_size - 1) - 1) // stride + 1

The default values specified are also the default values of nn.Conv1d, therefore you only need to specify what you also specify to create the convolution. It uses an integer division //, because the numerator might be not be divisible by stride, in which case it just gets rounded down (indicated by the brackets that are only closed at towards the bottom).

The same formula also applies to nn.MaxPool1d, but keep in mind that it automatically sets stride = kernel_size if stride is not specified.



来源:https://stackoverflow.com/questions/62162576/calculating-shape-of-conv1d-layer-in-pytorch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!