Trying to understand the input shape convention and notation

denexo: <p>I am confused with the input shape convention that is used in Pytorch in some cases:</p>
<ol>
<li>
<p>The nn.Layer’s input is of shape (N,∗,H_in) where N is the batch size, H_in is the number of features and ∗ means “any number of additional dimensions”. What exactly are these additional dimensions and how the nn.Linear is applied on them?</p>
</li>
<li>
<p>The nn.Conv1d’s input is of shape (N, C_in, L) where N is the batch size as before, C_in the number of input channels, L is the length of signal sequence.</p>
</li>
<li>
<p>The nn.Conv2d’s input is of shape (N, C_in, H, W) where N is the batch size as before, C_in the number of input channels, H is the height and W the width of the image.</p>
</li>
<li>
<p>The nn.BatchNorm1d’s input is of shape (N, C) or (N, C, L) where N is the batch size as before. However what does the C and L denote here? It seems that C = number of features, L = number of channels, based on the description in documentation “2D or 3D input (a mini-batch of 1D inputs with optional additional channel dimension)”. This is inconsistent with the nn.Conv1d notation.</p>
</li>
<li>
<p>The nn.BatchNorm2d’s input is of shape (N, C, H, W) where N is the batch size as before, H and W are the height and width of the image respectively. What does the C denote here? Is it the number of features as in nn.BatchNorm1d or the number of channels as in nn.Conv2d? It seems to be the number of channels since we are talking about “a 4D input (a mini-batch of 2D inputs with additional channel dimension)”, but then in documentation we have the line “num_features – C from an expected input of size (N, C, H, W)”, so C is both number of channels and number of features which is weird. So perhaps num_features should be renamed to num_channels.</p>
</li>
</ol>

ptrblck: <ol>
<li>Any number of dimensions are supported and the linear layer would use them as if you would loop through these dimensions as seen here:</li>
</ol>
<pre data-code-wrap="python"><code class="lang-python">
lin = nn.Linear(10, 10)
x = torch.randn(2, 10, 10)
out = lin(x)
out2 = torch.stack([lin(x_.squeeze(1)) for x_ in x.split(1, dim=1)], dim=1)
print((out - out2).abs().max())
# &gt; tensor(0., grad_fn=&lt;MaxBackward1&gt;)
</code></pre>
<ol start="2">
<li>
<p>and 3. yes, that’s correct.</p>
</li>
<li>
<p><code>nn.BatchNorm1d</code> accepts inputs in <code>[N, C, L]</code>, where <code>C</code> is optional. <code>C</code> is the channel dimension and <code>num_features</code> in the batchnorm layers will use either <code>C</code> or <code>L</code> depending on the input shape as described in the docs:</p>
</li>
</ol>
<blockquote>
<ul>
<li><strong>num_features</strong> – C from an expected input of size (N,C,L) or L from input of size (N,L)</li>
</ul>
</blockquote>
<p>The part in the <code>Shape</code> section is inconsistent, as it mentions the inputs as <code>(N, C)</code> or <code>(N, C, L)</code>. A PR is welcome in case you want to fix it.</p>
<ol start="5">
<li><code>C</code> denotes the channel dimension and thus the features in the batchnorm layer.</li>
</ol>
<aside class="quote no-group" data-username="denexo" data-post="1" data-topic="141680">
<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/denexo/48/19672_2.png" class="avatar"> denexo:</div>
<blockquote>
<p>so C is both number of channels and number of features which is weird.</p>
</blockquote>
</aside>
<p>Why is this weird? This dimensions is used as the channel dimension in the input and named features in the normalization layer.</p>
<aside class="quote no-group" data-username="denexo" data-post="1" data-topic="141680">
<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/denexo/48/19672_2.png" class="avatar"> denexo:</div>
<blockquote>
<p>So perhaps num_features should be renamed to num_channels.</p>
</blockquote>
</aside>
<p>This might be a possibility for batchnorm, but wouldn’t match other norm layers, which do not necessarily use the channel dimension only.</p>

denexo: <p>Thank you <a class="mention" href="/u/ptrblck">@ptrblck</a> for your detailed answer. It has clarified a lot for me.</p>
<p>It’s clear that for <code>nn.Conv1d</code>, <code>nn.Conv2d</code>, <code>nn.Batchnorm2d</code> and <code>nn.Batchnorm1d</code> (with a 3D input of shape (B, C, L)):</p>
<ul>
<li>The letter C denotes the <strong>number of channels</strong> which is the same as the <strong>number of features</strong>.</li>
<li>The letter L denotes the <strong>length</strong> of signal sequence (as described in the <code>nn.Conv1d</code> documentation).</li>
<li>The letters H and W denote the <strong>height</strong> and <strong>width</strong> of image respectively. This is consistent with the 1d because we could flatten an HxW image to a sequence of length L = H*W.</li>
</ul>
<p>The confusion arises in the case of a 2D input for <code>nn.Batchnorm1d</code>. I agree that the shape of a 2D input should be changed to (B, L) in the shape section of the documentation, however in this case the meaning of L changes automatically from “length of a signal sequence” to “number of features” (and the notion of channels doesn’t exist anymore). In my opinion, a different letter should be used for this case, e.g. (B, H_in) as in <code>nn.Linear</code>, to avoid confusion.</p>
<p>In conclusion, the 2D input and the 3D input to <code>nn.Batchnorm1d</code> are two different things. The first is a mini-batch of 1D vectors of features, the second is a mini-batch of signal sequences each of length L and with C channels/features. We cannot say that the first is equivalent to the second with shape (B, C=1, L). So in my opinion the shape of the first should be denoted by (B, H_in) and of the second by (B, C, L).</p>