Best input size of a the first layer 1D CNN
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(Simple1DCNN5, self).__init__()
        self.sequence = nn.Sequential(
            torch.nn.Conv1d(in_channels=50, 
                                          out_channels=64, 
                                          kernel_size=5, 
                                          stride=2),
            torch.nn.ReLU(),
            torch.nn.Conv1d(in_channels=64, 
                                          out_channels=128, 
                                          kernel_size=3),
            torch.nn.ReLU(),
            torch.nn.Conv1d(in_channels=128, 
                                          out_channels=256, 
                                          kernel_size=1),
            torch.nn.ReLU(),
        )
        self.fc1 = nn.Linear(256, 2)

        

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

net = Simple1DCNN3()

input_try = np.random.uniform(-10, 10, 500)
input_try = torch.from_numpy(input_try).float()
net(input_try)
print(&quot;input successfull passed to net&quot;)
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(Simple1DCNN5, self).__init__()
        self.sequence = nn.Sequential(
            torch.nn.Conv1d(in_channels=500, 
                                          out_channels=64, 
                                          kernel_size=5, 
                                          stride=2),
            torch.nn.ReLU(),
            torch.nn.Conv1d(in_channels=64, 
                                          out_channels=128, 
                                          kernel_size=3),
            torch.nn.ReLU(),
            torch.nn.Conv1d(in_channels=128, 
                                          out_channels=256, 
                                          kernel_size=1),
            torch.nn.ReLU(),
        )
        self.fc1 = nn.Linear(256, 2)

        

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

net = Simple1DCNN4()

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


Error message:
RuntimeError: Given groups=1, weight of size [64, 500, 5], expected input[1, 50, 10] to have 500 channels, but got 50 channels instead

EDIT
Thanks to the answer of @ghchoi, here is the code of the working kernel. For this, I also had to change the kernel size to all the convolutional layers to 1.
class Simple1DCNN5(torch.nn.Module):
    def __init__(self):
        super(Simple1DCNN5, self).__init__()
        self.sequence = nn.Sequential(
            torch.nn.Conv1d(in_channels=500, 
                                          out_channels=64, 
                                          kernel_size=1, 
                                          stride=2),
            torch.nn.ReLU(),
            torch.nn.Conv1d(in_channels=64, 
                                          out_channels=128, 
                                          kernel_size=1),
            torch.nn.ReLU(),
            torch.nn.Conv1d(in_channels=128, 
                                          out_channels=256, 
                                          kernel_size=1),
            torch.nn.ReLU(),
        )
        self.fc1 = nn.Linear(256, 2)

        

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

The kind of data I have is mono-derivation ECG (electrocardiogram) signal of 2 seconds. This is a recording of the electrical signal of the hear. Here is the idea of what a sample could look like (ploted on a 2D graphs), where you have time on the x-axis and voltage/amplitude on the y-axis


Try
    def forward(self, x):
        x = x.view(1, 500, -1) 

        ...

net = Simple1DCNN4()

input_try = np.random.uniform(-10, 10, 5000)

In this way, the input for the first Conv1d will have the 500 channels.
