Reputation: 69
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)```
Upvotes: 3
Views: 4275
Reputation: 32992
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.
Upvotes: 2