How make the input size for Multivariate time series data and use it for Conv1d?

I am working on a times series classification. I want to do this classification using CNN.
My data has 25 features. The input size that I made is: [batch_size, time_stamp, number_of_features]–> [10, 1, 25].

  1. my first question is: Do I make a correct input? I would like to extract features using CNN, however, I am not sure that I need to transpose my input to [batch_size, number_of_features, time_stamp]?

  2. Assuming the input with dimension [batch_size, time_stamp, number_of_features]
    I made the CNN as follows:

 class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Conv1d(in_channels =1, out_channels = 32, kernel_size =3, stride=1, padding =1 )
        self.relu = nn.ReLU()

        self.maxpool = nn.MaxPool1d(2) #output 25/2 = 12
        self.dropout = nn.Dropout(0.25)

        self.conv2 = nn.Conv1d(in_channels =32, out_channels = 12, kernel_size =3, stride=1, padding =1 )
        self.batchnormal = nn.BatchNorm1d(12)
        self.relu = nn.ReLU()
        self.maxpool = nn.MaxPool1d(2) #output 25/2 = 5
        # 5*12
#         self.flat = nn.Flatten()
        self.fc1 = nn.Linear(72, 32)
        self.dropout = nn.Dropout(0.1)
        self.fc2 = nn.Linear(32, 1)
        self.sigmoid =nn.Sigmoid()
    def forward(self, x):
        out = self.conv1(x)
        out = self.relu(out)
        out = self.maxpool(out)
        out = self.dropout(out)
        out =self.conv2(out)
        out = self.batchnormal(out)
        out = self.relu(out)
        out = self.maxpool(out)
        out = out.view(-1,72)
        out = self.fc1(out)
        out = self.dropout(out)
        out = self.fc2 (out)
        out = self.sigmoid(out)
        return out 

do I apply my Conv1d correctly?
As I explained in (1) I wan to use CNN to extract features.

for testing the CNN you can use this code:

a= torch.rand(10,1,25)
traget = torch.empty(10, 1).uniform_(0, 1)
target = torch.bernoulli(target)
model = CNN()
out = model(a)

I really appreciate any help!!

nn.Conv1d expects an input in the shape [batch_size, channels, temp_dim] where each kernel will “slide” in the temp_dim.
This is comparable to an nn.Conv2d layer which “slides” in the spatial dimensions height and width in an input of [batch_size, channels, height, width].
Based on your explanation, I would thus think that an input in the shape [batch_size, nb_features, time_stamp] would be the correct approach.

Many Thanks @ptrblck for your support!

I got confused as I want to extract features from my 25 features. :thinking:
In cov2d, features are in height and width, and the channel is for the colors.
In my case, if I consider [batch_size, nb_features, time_stamp], Conv1d will slide on time_stamp (I will consider 1, a row of my data) which is not my features, and nb_features would be my channels. did I understand it correctly? if yes so how I can interpret that the conv1d will go through my features?

If you want to slide through the “feature” dimension, you could of course move nb_features to dim2.
However, this would mean that time_stamp is used in dim1, so the channel dimension, is thus fixed, and you won’t be able to change the sequence length.
In this use case the nb_features can change in their size since the kernel is sliding in this dimension.
I’m not familiar with your use case, but if this uncommon approach sounds right, then go for it :slight_smile:

@ptrblck, sorry for my late response, and thanks for sharing the details!

My use case is doing classification on multivariate time series data. So whatever time window I have, I will reshape my data points and make an array. Let’s say I have a tensor of (1, 3, 25) (which is for t times stamp each with 25 features) it will reshape to (1,75).
I use CNN to classify my binary labels.

This StackOverflow helped me understand the dimension for Con1D
It may help others with the same confusion regarding the Con1D dimension. :slight_smile: