(Input Output dim size question) A pass through Conv2d convolves over multiple batches instead of on a single batch

username: pregenRobot
cooked: <p>Hope you all are havign a fine day.</p>
<p>I have been experimenting with using BERT output embeddings as inputs to a CNN for text classification.</p>
<p>I have set the max number of tokenizers to 256, and batch size is 4. Bert output layer gives a 1d tensor with 768 values.</p>
<p>(Essentially, I am just stacking (1x768) embedding vectors 256 times for each of the 256 tokens in a single sentence that will be used for classification. Each batch has 4 sentences )<br>
Perhaps the following image is going to get the point across:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/8/388342dc9b3a6e92869d8219565e12c0c6d76ee3.png" data-download-href="https://discuss.pytorch.org/uploads/default/388342dc9b3a6e92869d8219565e12c0c6d76ee3" title="1*-vDcDVuM2DoDnA7BsDONCQ"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/8/388342dc9b3a6e92869d8219565e12c0c6d76ee3_2_497x500.png" alt="1*-vDcDVuM2DoDnA7BsDONCQ" data-base62-sha1="83VXM5Cw7jo0LEylLZ3ZuP8XFNp" width="497" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/8/388342dc9b3a6e92869d8219565e12c0c6d76ee3_2_497x500.png, https://discuss.pytorch.org/uploads/default/original/3X/3/8/388342dc9b3a6e92869d8219565e12c0c6d76ee3.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/3/8/388342dc9b3a6e92869d8219565e12c0c6d76ee3.png 2x" data-dominant-color="E6E4DC"><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">1*-vDcDVuM2DoDnA7BsDONCQ</span><span class="informations">612×615 40.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>The following are the dimensions of a single batch:</p>
<p>IN</p>
<pre><code class="lang-auto">batch = torch.stack([torch.unsqueeze(torch.rand((256,768)),dim=0) for i in range(4)])
</code></pre>
<p>OUT</p>
<pre><code class="lang-auto">torch.Size([4, 1, 256, 768])
#a single batch with 4 samples, 1 input channel, 256 tokens with 768 rowed embedding tensors
</code></pre>
<p>The following is just a snippet of my model and forward method:</p>
<pre><code class="lang-auto">class EmbeddingCNN(nn.Module):
    def __init__(self):
        super(EmbeddingCNN,self).__init__()
        self.conv1 = nn.Conv2d(
            1,
            3, 
            (5, 768))
        
        self.pool1 = nn.MaxPool2d(kernel_size=(4,))
        
    def forward(self,embedding):
        
        conv1out = self.conv1(embedding).squeeze(3)
        print(conv1out.shape)
        
        return conv1out
</code></pre>
<p>Instead of convolving with square kernels like in image classification, in the case of text, I am convolving with a 5x768 kernel (which is essentially going from top to bottom if 256 was the height and 768 was the width. The intuition is to apply filters over 5 consecutive words in a sentence) There are 3 output channels in first conv2d layer.</p>
<p>This results in the following dimensions</p>
<pre><code class="lang-auto">#conv1out.shape
[1,3,252,1]
</code></pre>
<p>Here we come to my question. Why isn’t the length of the first dimension 4 (num of batches)? Why has it reduced to 1? I would assume this convolution assume the following are the meanings of the dimension sizes</p>
<pre><code class="lang-auto">1 : ???
3 : Output channel count
4 : "Height" of the convolved layer
5 : "Width" of the convolved layer.
</code></pre>
<p>Is this understanding correct? If not, what do the dimensions mean?</p>

username: Abhilash_Srivastava
cooked: <aside class="quote no-group" data-username="pregenRobot" data-post="1" data-topic="100156">
<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/pregenrobot/48/29431_2.png" class="avatar"> pregenRobot:</div>
<blockquote>
<pre><code class="lang-auto">        self.conv1 = nn.Conv2d(
            1,
            3, 
            (5, 768))
</code></pre>
</blockquote>
</aside>
<p>Here the three input params mean: (in_channels, out_channels, kernel_size). Since your in_channels=1,<br>
conv1out.shape = [1,3,252,1] is expected.</p>

username: pregenRobot
cooked: <p>Thanks for your input.</p>
<p>My input to the Conv2d layer was torch.Size([4, 1, 256, 768])<br>
where 4 is the number of samples in a single batch. Why isn’t the output: [4,3,252,1] ?<br>
(4 samples, 3 output channels, 252 height, 1 width&gt;)</p>