I’m trying to build a 1D CNN with time series. The input is of length 500. There are (only) 2 labels. The architecture which I built so far is the following: there are 3 convolution layers each, of them followed by an activation layer. The first convolution layer takes 50 channels as input.

import torch
import torch.nn as nn
import numpy as np
import random

class Simple1DCNN3(torch.nn.Module):
    def __init__(self):
        super(Simple1DCNN3, self).__init__()
        self.sequence = nn.Sequential(
            torch.nn.Conv1d(in_channels=50, 
                                          out_channels=50, 
                                          kernel_size=5, 
                                          stride=2),
            torch.nn.ReLU(),
            torch.nn.Conv1d(in_channels=50, 
                                          out_channels=20, 
                                          kernel_size=3),
            torch.nn.ReLU(),
            torch.nn.Conv1d(in_channels=20, 
                                          out_channels=10, 
                                          kernel_size=1),
            torch.nn.ReLU(),
        )
        self.fc1 = nn.Linear(10, 2)

        

    def forward(self, x):
        x = x.view(1, 50,-1)
        
        for layer in self.sequence:
            x = layer(x)
        x = x.view(1,-1)
        x = self.fc1(x)
        return x

net = Simple1DCNN3()

input_try = np.random.uniform(-10, 10, 500)
input_try = torch.from_numpy(input_try).float()
net(input_try)
print("input successfull passed to net")
input_try_modif = input_try.view(1, 50,-1)
print(input_try.shape)
print(input_try_modif.shape)


As far as I understood, that forced me to segment the input in 10 segments of 50 timepoints. Am I understanding it wrong ? Wouldn’t it be wiser to construct the first layer with 500 channels as inputs and have a sliding window kernel? I tried it in the following other script but got the following error message

import torch
import torch.nn as nn
import numpy as np
import random

class Simple1DCNN4(torch.nn.Module):
    def __init__(self):
        super(Simple1DCNN4, self).__init__()
        self.sequence = nn.Sequential(
            torch.nn.Conv1d(in_channels=500, 
                                          out_channels=50, 
                                          kernel_size=5, 
                                          stride=2),
            torch.nn.ReLU(),
            torch.nn.Conv1d(in_channels=50, 
                                          out_channels=20, 
                                          kernel_size=3),
            torch.nn.ReLU(),
            torch.nn.Conv1d(in_channels=20, 
                                          out_channels=10, 
                                          kernel_size=1),
            torch.nn.ReLU(),
        )
        self.fc1 = nn.Linear(10, 2)

        

    def forward(self, x):
      #x = x.view(1, 1,-1)
        
        for layer in self.sequence:
            x = layer(x)
        x = x.view(1,-1)
        x = self.fc1(x)
        return x

net = Simple1DCNN4()

input_try = np.random.uniform(-10, 10, 500)
input_try = torch.from_numpy(input_try).float()
net(input_try)
print("input successfull passed to net")
input_try_modif = input_try.view(1, 50,-1)
print(input_try.shape)
print(input_try_modif.shape)


Error message:
RuntimeError: Expected 3-dimensional input for 3-dimensional weight [50, 500, 5], but got 1-dimensional input of size [500] instead