Issue about applying 1D CNN on time series Data

I have time series data with sample dim of 1024x1 which one-dimensional input with 1024 length, I am trying to apply conv1d by specifying the number of input channels as 1 and the output channels as 5. while running I got an error, I think i miss understood something, I will be grateful for any help.

This is my layer architecture : self.feature_extractor = nn.Sequential(
#1024x1 —> 5x1024
nn.Conv1d(1, 5,3),
#5x1024----> 5x512
nn.MaxPool1d(2),
nn.ReLU(),
#5x512---->10x512
nn.Conv1d(5, 10, kernel_size=3, padding=1),
#10x512—>10x256
nn.MaxPool1d(2),
#10x512—>10x256
nn.Conv1d(10, 20, kernel_size=3,padding=1),
#20256—>20128
nn.MaxPool1d(2),
nn.Dropout2d(),
)

====
And here is the Error:

Expected 3-dimensional input for 3-dimensional weight [5, 1, 3], but got input of size [1024, 1] instead

1 Like

In pytorch the order of dimensions are b-c-l with:

  • b is the “mini batch” dimension
  • c is the “channel” or the actual dimension of each 1d point
  • l is the length of your c-dimensional signal.
    In your case, you have a single example, thus b=1. You have 1024 samples, thus l=1024 and the dimension of each point is 1, thus c=1.
    Pytorch expects your 1024x1 input to actually be of size 1x1x1024.
    Use view and transpose to get from 1024x1 to 1x1x1024 and you should be okay.
2 Likes

Thanks a lot for your help, it works

1 Like