1D convolution for 1D feature input

username: dsp_torch
cooked: <p>Hello Everyone,</p>
<p>I am using a time-series data for binary class classification.  I have a training dataset of 4917 x 244 where 244 are the feature columns and 4917 are the onsets. For example if I am using a sliding window of 2048 then it calculates 1 x 244 feature vector for one window. Therefore we have such 4917 windows and its respective feature columns.<br>
Now I am using a batch size to divide 4917 onsets into number of batches of size 256 each. Therefore we have feature vector 256 x 244 for one batch.</p>
<p>I want to implement 1D convolution to process this data and classify each window into 0 or 1 class. At my initial efforts I am very much confused in input_channel, output_channel and kernel size. I tried my best but eventually I am getting error of dimension mismatch or out of range during training.</p>
<p>I would appreciate if anyone could clearly explain the steps that I can follow to implement 1D CNN for my dataset.</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="dsp_torch" data-post="1" data-topic="211919">
<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/d/d07c76/48.png" class="avatar"> dsp_torch:</div>
<blockquote>
<p>For example if I am using a sliding window of 2048 then it calculates 1 x 244 feature vector for one window. Therefore we have such 4917 windows and its respective feature columns.</p>
</blockquote>
</aside>
<p>I don’t understand this approach completely. Are you padding the input and are you using the “feature” dimension as the channels? Are you then striding over the first dimension of the size <code>4917</code>?</p>
<aside class="quote no-group" data-username="dsp_torch" data-post="1" data-topic="211919">
<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/d/d07c76/48.png" class="avatar"> dsp_torch:</div>
<blockquote>
<p>Now I am using a batch size to divide 4917 onsets into number of batches of size 256 each. Therefore we have feature vector 256 x 244 for one batch.</p>
</blockquote>
</aside>
<p>I’m also unsure how this operation is performed as it cannot be a <code>reshape</code> operation so could you also explain in more detail how the input is processed?</p>

username: dsp_torch
cooked: <p>Sorry may be I could not explain it better.</p>
<p>For example: I have a time series acoustic signal of an event of class 0 with a total number of samples let say 204800.  Now I am using a window size of 2048 to slide over the total samples with zero overlap length. Then we have such 100 windows to scan all 204800 samples. Now for each window I am calculating 1D 244 features (Temporal, spectral, avg mffcs etc.). It means for each window I have a feature vector of 1 x 244. Now for a total of 100 windows I have 100 x 244 feature vectors.  Now read the next .wav file let say for class 1 we have 20480 samples. Then the window size of 2048 with zero overlap length will return 10 x 244 feature vectors. When I append this feature vector to the previous one and now I have a feature size of 110 x 244. Repeating the same for all .wav files of class 0 and 1.<br>
In the above query I said that I have such 4917 x 244 feature vectors that is evaluated on the window size of 2048 for all the .wav file of class 0 and 1. In other word I have training set of x_train = 4917 x 244  and y_train = 4917 x 1.<br>
Now I am using the concept of batch size to divide the training set into number of batches and created a train_dataloader.<br>
train_dl = DataLoader(CageData(X_train, y_train),<br>
batch_size = 256,<br>
shuffle = True,<br>
num_workers = 2,<br>
pin_memory = False)</p>
<p>Thus for 1 epoch it will iterate 19 times 4917/256 = ~19 to complete training of 4917 examples.<br>
Similarly I have a test set of X_test = 626 x 244 and labels y_test = 626 x 1.<br>
test_dl = DataLoader(CageData(X_test, y_test),<br>
batch_size = 256,<br>
shuffle = True,<br>
num_workers = 2,<br>
pin_memory = False)</p>
<p>Now I want to know how to process this data to my 1D CNN to train and test. What will be my channel_in , channel_out, kernel_size and what will be the dimension of fully connected layer.</p>

username: ptrblck
cooked: <p>Thanks for the update.<br>
I assume the preprocessing is already done and your <code>X_train/test</code> as well as <code>y_train/test</code> datasets are already created.</p>
<p>If I understand your question correctly you now want to pass this data (from the <code>DataLoader</code>) into a 1d-CNN.</p>
<p><code>nn.Conv1d</code> layers expect a 3D input in the shape <code>[batch_size, channels, seq_len]</code> where <code>channels</code> corresponds to the number of input channels and <code>seq_len</code> to the size of the temporal dimension which will be used for the actual convolution.<br>
Your current input has a shape of <code>[batch_size, nb_features]</code> and it’s unclear what exactly these features represent.<br>
<code>nn.Conv1d</code> layers can be used for e.g. audio signals and the signal input could be a stereo audio data tensor in the shape <code>[batch_size, nb_channels=2, seq_len=number_of_timesteps]</code>.<br>
I thus don’t know if you want to treat the feature dimension in your input as a temporal dimension and thus move the conv filters through it (you would expect a temporal dependency between these data points in this case) or as channels.</p>
<p>Based on:</p>
<aside class="quote no-group" data-username="dsp_torch" data-post="3" data-topic="211919">
<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/d/d07c76/48.png" class="avatar"> dsp_torch:</div>
<blockquote>
<p>Now for each window I am calculating 1D 244 features (Temporal, spectral, avg mffcs etc.)</p>
</blockquote>
</aside>
<p>it seems you are computing a variety of features and since these were created using a sliding window I would expect they are correlated in the temporal dimension.<br>
If so, you could <code>unsqueeze(1)</code> another additional dimension creating a single channel so that your input would have the shape <code>[batch_size, nb_channels=1, seq_len=244]</code>.<br>
In this case the <code>nn.Conv1d</code> layer would use <code>in_channels=1</code> and an arbitrary number of <code>out_channels</code>. The same goes for the <code>kernel_size</code>, <code>stride</code>, etc. as you can define it yourself.<br>
The number of <code>in_features</code> in the first linear layer used after flattening the intermediate activation would thus also depend on the setup used for all conv layers, which you should pick.</p>

username: dsp_torch
cooked: <p>Thank you for detailed explanation. I got the clarity and believe that it should work.</p>