Convolution 1D in Pytorch

Hemraj Choudhary
2 min readAug 1, 2023

--

In this article we will understand the convolution 1d and how to implement it in pytorch.

Mathematical formula is following for conv1d

Math behind covolution 1d operation

We take first kernel with number of channels same as number of input channels and apply cross-correlation which is moving window dot product. We get output with one channel, similarly we take all kernel one by one and apply cross-correlation on input. Finally we concatenate all the output of kernels which is our result.

from torch import nn
num_channels_in_input = 3
length_of_input = 10
num_of_examples = 5
_input = torch.randn(num_of_examples, num_channels_in_input, length_of_input)

output_channels = 5 #It is number of kerels which will be applied on input
kernel_size = 3 #length of single channel in kernel

conv1d = nn.Conv1d(num_channels_in_input, output_channels, kernel_size)
output = conv1d(_input)

print(output.shape)
# (num_of_examples, output_channels, length_of_input - kernel_size + 1)
# (5, 5, 10 - 3 + 1)
# (5, 5, 8)

I hope you find the article usefull for applying conv1d operation on your desired input.

Follow on twitter Linkedin

Thank you

--

--

Hemraj Choudhary
Hemraj Choudhary

Written by Hemraj Choudhary

Computer Vision Scientist. Deep Learning for Vision and NLP. RudraAIHub

No responses yet