Trying to implement a NN described in a paper. Don't understand what is meant by size

pascalm: <p>Hello,</p>
<p>This is the first time I’m trying to implement a NN which is described in a paper. So far I just did my own thing by trial and error. I don’t fully understand how I should read the table found here: <a href="https://i.imgur.com/FCFb216.png" rel="noopener nofollow ugc">https://i.imgur.com/FCFb216.png</a></p>
<p>Note that the table only contains the first few layers.</p>
<p>Now I want to use <code>torch.nn.Conv1d(in_channels, out_channels, kernel_size,...)</code> for that but I have no idea how to read the table in the image. It is clear that it is a sequential NN but what exactly is meant by size? It says the size of the 2nd layer (conv1d) is 32x40945 and the size of the 3th layer (pooling) is 32x10236. What is now <code>in_channels</code>, <code>out_channels</code> and <code>kernel_size</code> for the 2nd layer exactly? Note that in and out channels are integers.</p>
<p>Thanks</p>

JuanFMontesinos: <p>It seems to be the size of the input tensor.<br>
so it starts being a vector of 40960 elements.<br>
Then becomes a matrix of 1x40960.<br>
Then theyh apply a 1d conv with inchannels=1 and out_channels=32 and kernel=16<br>
(Still if I were you I would use 15 as kernels are better to be odd.I</p>

pascalm: <p>First a bit more context. The input is a timeseries and we want to do signal processing. We sample the signal for 4096 Hz and apparently 10 seconds. Thus the input size of 40960.</p>
<p>Now the size in the 2nd layer (conv1d) is 32 x 40945</p>
<p>What exactly describes 40945 and how do I get this number?</p>
<p>For me, a CNN takes an array (1d) or an image (2d) and apply some stencil/kernel to it, by doing so reduced the dimension. I can’t see why we have a difference of 15 here (40945 vs 40960).</p>
<p>Edit: How do you see the kernel is of size 16?</p>

JuanFMontesinos: <p>I just see it by expertise <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">  but maths can be used too!</p>
<p>Basically when you apply a convolution the resulting signal 's size is gonna be reduced unless you pad the input. (You can google about it).<br>
In this case the size of the output is 40945. Since it’s more or less the same size as the input, we can know the stride=1. For stride 2 the signal size would be halved.<br>
If you check the docs, you have the mathematical expression to calculate the size:<br>
<a href="https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html#torch.nn.Conv1d" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html#torch.nn.Conv1d</a><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/c/fc471c47b4b8610d9af12f2ec77dc49320713cb6.png" data-download-href="https://discuss.pytorch.org/uploads/default/fc471c47b4b8610d9af12f2ec77dc49320713cb6" title="imagen"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/c/fc471c47b4b8610d9af12f2ec77dc49320713cb6_2_690x140.png" alt="imagen" data-base62-sha1="zZKFUF2aAcsBE0nfnfklrHZlTMO" width="690" height="140" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/c/fc471c47b4b8610d9af12f2ec77dc49320713cb6_2_690x140.png, https://discuss.pytorch.org/uploads/default/original/3X/f/c/fc471c47b4b8610d9af12f2ec77dc49320713cb6.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/f/c/fc471c47b4b8610d9af12f2ec77dc49320713cb6.png 2x" data-dominant-color="F9F9F9"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">imagen</span><span class="informations">938×191 11.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>In this case we can see the following:</p>
<ul>
<li>Input signal’s size ~= output’s size → stride=1</li>
<li>Dilation=1 as they say nothing about it. They could use diletion but, if that were the case, they could just downsample the signal beforehand.</li>
<li>Padding=0 as the size of the input is similar to the size of the output. Still they could pad and use a bigger kernel size.<br>
Then if we apply the formula we have 40960 - 2*(16-1)+1-1= 490945.</li>
</ul>
<p>So in short 40945 describes the number of elements in the time series after you apply the convolution.<br>
32 describes the amount of filters used.</p>

pascalm: <p>Okay, thanks a lot. So the size described in the image is basically C_out x L_out.</p>
<p>I guess that makes sense. Still think it’s a bad way of representing your NN thought.</p>
<p>Maybe a follow-up question. Nothing here takes into account the batch size right? (I have another thread where I try to figure out the implementation side of this)</p>
<p>Thanks again!</p>

JuanFMontesinos: <p>Yep, neural networks are usually described by the kernel size, padding, stride and so on… Such a strange work the one you found.</p>
<p>All the pytorch layers expect a batch dimension. If you have a single 1d sample, it has to be of shape NxCxT.<br>
So:<br>
1D signals–&gt; NxCxT usually c=1 for mono audio or time series, c=2 for dual audio<br>
2D signals → NxCxHxW usuall c=1 for b&amp;w images, c=3 for color…<br>
fully conected (linear layers)–&gt; Nx*xT It expects the first dimension to be batch, then you can have as many dimensions as you want (it will recursively apply the fc among them all like in a nested for loop)</p>
<p>So basically the, for a 1D signal, if you apply a convolution to a batched tensor NxCxT<br>
it’s equivalent to do a for loop from n=0 to n=N. They are independent.</p>
<p>In the papers you don’t describe the batch dimension, still both pytorch and tensorflow use it in practice.</p>

pascalm: <p>thanks a lot. I’ll make a new thread about how I implement the CNN with variable batch sizes because something I do is wrong and I just can’t find it in the docs.</p>

pascalm: <p>Just as a note: the 2* is wrong, should be 1*.</p>