RuntimeError: Expected 4-dimensional input for 4-dimensional weight [64, 3, 4, 4], but got 3-dimensional input of size [3, 64, 64] instead

username: banikr
cooked: <p>I think the Dataloader is not taking 4-dimensions(missing batch_size).</p>
<p>The images are 64x64 RGB.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/f/ff0aa20cebbcb9c4abd2112a1873e911827e6b09.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/ff0aa20cebbcb9c4abd2112a1873e911827e6b09" title="Capture"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/f/ff0aa20cebbcb9c4abd2112a1873e911827e6b09_2_690x204.jpeg" alt="Capture" data-base62-sha1="AocxeHv5YhMg5ZIZSsDf76ny0Od" width="690" height="204" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/f/ff0aa20cebbcb9c4abd2112a1873e911827e6b09_2_690x204.jpeg, https://discuss.pytorch.org/uploads/default/optimized/2X/f/ff0aa20cebbcb9c4abd2112a1873e911827e6b09_2_1035x306.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/f/ff0aa20cebbcb9c4abd2112a1873e911827e6b09.jpeg 2x" data-dominant-color="303030"><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">Capture</span><span class="informations">1044×309 36 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>

username: ptrblck
cooked: <p>It seems you are manually slicing the returned <code>sample</code>, which might be the data tensor, since you are creating the <code>label</code> manually.<br>
Could you check <code>b_size</code> and the shape of <code>sample</code>?</p>

username: banikr
cooked: <p>sample size is torch.Size([128, 3, 64, 64]) <span class="hashtag">#128</span> is batch_size<br>
b_size is 3</p>

username: ptrblck
cooked: <p>In that case, just assign <code>real_cpu</code> to the <code>sample</code> or use <code>sample</code> directly for your forward pass.</p>

username: banikr
cooked: <p>I was thinking about that, but the DCGAN tutorial here in pytorch is using the same type of code. Though they used data.ImageFolder to create dataloader which is not working in my case so I manually created the dataloader by taking self.item in dataset class and then parameters like batch_size, shuffle…<br>
Please take a look</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/6/62112060207816945f199b38b9652566059d591d.png" data-download-href="https://discuss.pytorch.org/uploads/default/62112060207816945f199b38b9652566059d591d" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/6/62112060207816945f199b38b9652566059d591d_2_690x293.png" alt="image" data-base62-sha1="dZxu5PZzhZM8PCCQ9mtnO9s1vFr" width="690" height="293" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/6/62112060207816945f199b38b9652566059d591d_2_690x293.png, https://discuss.pytorch.org/uploads/default/optimized/2X/6/62112060207816945f199b38b9652566059d591d_2_1035x439.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/6/62112060207816945f199b38b9652566059d591d.png 2x" data-dominant-color="ECEDEE"><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">image</span><span class="informations">1203×512 37.9 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>

username: ptrblck
cooked: <p>In the DCGAN code, <code>data</code> contains the input and target tensor as a <code>tuple</code>.<br>
Thus the code uses indexing to get the input sample using <code>data[0]</code>.<br>
If your <code>DataLoader</code> and <code>Dataset</code> only returns the input sample without a target, you don’t need this indexing.</p>

username: banikr
cooked: <p>Hey, it worked fine! Thanks for the hack!</p>