Missing the batch dimension as well as the channels [Error: Expected 4-dimensional input for 4-dimensional weight 32 1 5 5, but got 2-dimensional input of size [2048, 2048]]

trn_phong: <p>Hi, I’m trying to implement a CNN model as below:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/9/99ccdb0e5c9d3166095dc8527507d11493d7b0e5.png" data-download-href="https://discuss.pytorch.org/uploads/default/99ccdb0e5c9d3166095dc8527507d11493d7b0e5" title="CNN-Architecture"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/9/99ccdb0e5c9d3166095dc8527507d11493d7b0e5_2_690x283.png" alt="CNN-Architecture" data-base62-sha1="lWzXacgt7dTV7kUdnXf8I1qZcWh" width="690" height="283" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/9/99ccdb0e5c9d3166095dc8527507d11493d7b0e5_2_690x283.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/9/99ccdb0e5c9d3166095dc8527507d11493d7b0e5_2_1035x424.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/9/9/99ccdb0e5c9d3166095dc8527507d11493d7b0e5_2_1380x566.png 2x" data-dominant-color="FBF0D6"><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">CNN-Architecture</span><span class="informations">1762×724 35.8 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>However, I got the error <strong>[Expected 4-dimensional input for 4-dimensional weight 32 1 5 5, but got 2-dimensional input of size [2048, 2048]</strong> and I don’t know how to solve the problem, Can anybody help me? Thạnk you really much !!!</p>
<p>My CNN code:</p>
<pre><code>    MLP_modules = []
MLP_modules.append(nn.Conv2d(1, 32 , kernel_size =  5))
MLP_modules.append(nn.ReLU())
MLP_modules.append(nn.Conv2d(32, 1, kernel_size = 3))
MLP_modules.append(nn.ReLU())
MLP_modules.append(nn.Conv2d(32, 1, kernel_size = 2))
MLP_modules.append(nn.ReLU())
self.MLP_layers = nn.Sequential(*MLP_modules) 
</code></pre>
<p>forward() func:</p>
<p>embed_user_MLP = self.embed_user_GMF(user)<br>
embed_item_MLP = self.embed_item_GMF(item)<br>
interaction_matrix = torch.ger(embed_user_MLP.reshape(-1), embed_item_MLP.reshape(-1))<br>
output_MCLP = self.MLP_layers(interaction_matrix)</p>

trn_phong: <p>Data of User embedding <strong>outer product</strong> item embedding</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/9/b912c5052d0bfba998081a8b7d51236437f9feb5.png" data-download-href="https://discuss.pytorch.org/uploads/default/b912c5052d0bfba998081a8b7d51236437f9feb5" title="user_item_outerproduct"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/9/b912c5052d0bfba998081a8b7d51236437f9feb5_2_690x176.png" alt="user_item_outerproduct" data-base62-sha1="qpexPOx3QknVUh0L94oBEYGEo3X" width="690" height="176" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/9/b912c5052d0bfba998081a8b7d51236437f9feb5_2_690x176.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/9/b912c5052d0bfba998081a8b7d51236437f9feb5_2_1035x264.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/b/9/b912c5052d0bfba998081a8b7d51236437f9feb5_2_1380x352.png 2x" data-dominant-color="F4F2F2"><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">user_item_outerproduct</span><span class="informations">1798×460 88.5 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>

ptrblck: <p>Based on the error message, it seems the input to the first conv layer is given as a 2-dimensional tensor.<br>
This is probably your embedding.<br>
An <code>nn.Conv2d</code> layer expects a 4-dimensional input with the shapes <code>[batch_size, channels, height, width]</code>.<br>
Based on the setup of your first conv layer and your drawing, I assume you would like to use a single channel with a spatial size of <code>[8, 8]</code>.</p>
<p>If that’s the case, try to unsqueeze the batch and channel dimension using:</p>
<pre><code class="lang-python"># x.shape == [8, 8]
x = x.unsqueeze(0).unsqueeze(1)
# x.shape == [1, 1, 8, 8]
</code></pre>
<p>However, could you also post the first code snippet including the embedding, as apparently your batch dimension went missing, which could be another (hidden) error in your code.</p>

trn_phong: <p>I use the embedding layer of torch like this:</p>
<p>self.embed_user_GMF = nn.Embedding(user_num, factor_num)<br>
self.embed_item_GMF = nn.Embedding(item_num, factor_num)</p>
<p>with factor_num = 8.</p>
<p>I tried your solution but I got the new error: <strong>Given groups=1, weight of size 1 32 2 2, expected input[1, 1, 2042, 2042] to have 32 channels, but got 1 channels instead</strong></p>
<p>My code:<br>
embed_user_MLP = self.embed_user_GMF(user)<br>
embed_item_MLP = self.embed_item_GMF(item)<br>
interaction_matrix = torch.ger(embed_user_MLP.reshape(-1), embed_item_MLP.reshape(-1))<br>
interaction_matrix = interaction_matrix.unsqueeze(0).unsqueeze(1)<br>
output_MCLP = self.MLP_layers(interaction_matrix)</p>

ptrblck: <p>Thanks for the code.<br>
I’m a bit confused about the <code>torch.ger</code> call.<br>
You are currently reshaping the output of both embedding layers to a 1-dimensional tensor.<br>
This would also mean that you are collapsing all dimensions, including the batch dimension.<br>
Is this what you plan to do?</p>
<p>Usually, you would keep the batch dimension intact, so could you explain the use case a bit, so that we could debug a bit more?</p>

trn_phong: <p>Thank you for your response.</p>
<p>In theory, I have a 1D vector (tensor) of user embedding and a 1D vector item embedding. Then I want to make an outer product for these two 1D vectors to obtain a 2D matrix (considered as an image) - This matrix will be the input of CNN model.</p>
<p>I reshaped the user &amp; item tensors because I see that torch.ger required 1D tensor input, i’m not sure it is my case or not <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=9" title=":frowning:" class="emoji" alt=":frowning:"></p>

ptrblck: <p>Your approach might be right for your use case, but you could also calculate the outer product of batched tensors by using:</p>
<pre><code class="lang-python">batch_size = 10
m, n = 3, 4
a = torch.randn(batch_size, m)
b = torch.randn(batch_size, n)

result = a.unsqueeze(2) * b.unsqueeze(1)
print(result.shape)
&gt; torch.Size([10, 3, 4])
</code></pre>

trn_phong: <p>Thank you really really much for your response, <a class="mention" href="/u/ptrblck">@ptrblck</a></p>