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

username: sasha
cooked: <p>Hi,<br>
I am working on a times series classification. I want to do this classification using CNN.<br>
My data has 25 features. The input size that I made is: [batch_size, time_stamp, number_of_features]–&gt; [10, 1, 25].</p>
<ol>
<li>
<p>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]?</p>
</li>
<li>
<p>Assuming the input with dimension [batch_size, time_stamp, number_of_features]<br>
I made the CNN as follows:</p>
</li>
</ol>
<pre><code class="lang-auto"> 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):
        print(x.shape)
        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)
        print(out.shape)
        out = self.fc1(out)
        out = self.dropout(out)
        out = self.fc2 (out)
        print(out.shape)
        out = self.sigmoid(out)
        return out 
</code></pre>
<p>do I apply my Conv1d correctly?<br>
As I explained in (1) I wan to use CNN to extract features.</p>
<p>P.S.<br>
for testing the CNN you can use this code:</p>
<pre><code class="lang-auto">a= torch.rand(10,1,25)
print(a.shape)
traget = torch.empty(10, 1).uniform_(0, 1)
target = torch.bernoulli(target)
print(target.shape)
model = CNN()
out = model(a)
print(ou.shape)
</code></pre>
<p>I really appreciate any help!!</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="sasha" data-post="1" data-topic="146852">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/54ee81/48.png" class="avatar"> sasha:</div>
<blockquote>
<p>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]?</p>
</blockquote>
</aside>
<p><code>nn.Conv1d</code> expects an input in the shape <code>[batch_size, channels, temp_dim]</code> where each kernel will “slide” in the <code>temp_dim</code>.<br>
This is comparable to an <code>nn.Conv2d</code> layer which “slides” in the spatial dimensions <code>height</code> and <code>width</code> in an input of <code>[batch_size, channels, height, width]</code>.<br>
Based on your explanation, I would thus think that an input in the shape <code>[batch_size, nb_features, time_stamp]</code> would be the correct approach.</p>

username: sasha
cooked: <p>Many Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a> for your support!</p>
<aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="146852">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p><code>nn.Conv1d</code> expects an input in the shape <code>[batch_size, channels, temp_dim]</code> where each kernel will “slide” in the <code>temp_dim</code>.</p>
</blockquote>
</aside>
<p>I got confused as I want to extract features from my 25 features. <img src="https://discuss.pytorch.org/images/emoji/apple/thinking.png?v=12" title=":thinking:" class="emoji" alt=":thinking:" loading="lazy" width="20" height="20"><br>
In cov2d, features are in <code>height</code> and <code>width</code>, and the channel is for the colors.<br>
In my case, if I consider <code>[batch_size, nb_features, time_stamp]</code>, 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?</p>

username: ptrblck
cooked: <p>If you want to slide through the “feature” dimension, you could of course move <code>nb_features</code> to <code>dim2</code>.<br>
However, this would mean that <code>time_stamp</code> is used in <code>dim1</code>, so the channel dimension, is thus fixed, and you won’t be able to change the sequence length.<br>
In this use case the <code>nb_features</code> can change in their size since the kernel is sliding in this dimension.<br>
I’m not familiar with your use case, but if this uncommon approach sounds right, then go for it <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

username: sasha
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>, sorry for my late response, and thanks for sharing the details!</p>
<p>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).<br>
I use CNN to classify my binary labels.</p>
<p>This <a href="https://stackoverflow.com/questions/43396572/dimension-of-shape-in-conv1d" rel="noopener nofollow ugc">StackOverflow</a> helped me understand the dimension for Con1D<br>
It may help others with the same confusion regarding the Con1D dimension.  <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>