SoFunction
Updated on 2024-11-15

Detailed usage of nn.Conv1d in pytorch

Paste a paragraph from the official guide first:nn.conv1d official

I was stuck with in_channels, out_channels for a long time at first, but it turned out to be a dime a dozen with conv2d. Without further ado, let's stick the code (self-cultivation for rookies)

class CNN1d():

  def __init__(self):
    super(CNN1d,self).__init__()
    self.layer1 = (
          nn.Conv1d(1,100,2),
          nn.BatchNorm1d(100),
          (),
          nn.MaxPool1d(8))
    self.layer2 = (
          nn.Conv1d(100,50,2),
          nn.BatchNorm1d(50),
          (),
          nn.MaxPool1d(8))
     = (300,6)
  def forward(self,x):
    #:(16,1,425)
    out = self.layer1(x)
    out = ((0),-1)
    out = (out)
    return out

The input data format is (batch_size,word_vector,sequence_length), I set batch=16, the feature engineering sample is 1x425, applying that format it should be (16, 1, 425). Corresponding to nn.Conv1d's in_channels=1, out_channels is what you set yourself, I chose 100.

Since I'm doing a classification scenario, I have to add a linear layer after doing two 1D convolutions.

Above this pytorch in nn.Conv1d usage details is all I share with you, I hope to give you a reference, and I hope you support me more.