RuntimeError: The size of tensor a (224) must match the size of tensor b (8) at non-singleton dimension 3

username: naychelynn
cooked: <p>Hello, I’m training a vision transformer on the custom dataset for regression purpose.<br>
The predicted size resulted from the network torch.Size([8]), but the input label size is torch.Size([8, 3, 224, 224]) which have different sizes.<br>
I’ve tried with the resize() function but got AttributeError: ‘Tensor’ object has no attribute ‘Resize’.<br>
I also tested with torch.resize() and _resize() functions too but not work.<br>
So, pls kindly suggest to me the standard way to resize.</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/f/5f87fbdda23d47017ed19239d171300a70aa985a.png" alt="image" data-base62-sha1="dD6HDd4H9bPi5kWvyD2duhxGIoW" width="420" height="160"></p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="naychelynn" data-post="1" data-topic="164374">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/e68b1a/48.png" class="avatar"> naychelynn:</div>
<blockquote>
<p>The predicted size resulted from the network torch.Size([8]), but the input label size is torch.Size([8, 3, 224, 224]) which have different sizes.</p>
</blockquote>
</aside>
<p>Could you describe what the target represents as it seems you are trying to reuse the model input as the target tensor?<br>
I would not recommend to resize the tensors but to check what your use case is and what the dimensions of the target represent as they don’t seem to fit a classification or segmentation use case.</p>

username: naychelynn
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="164374">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>Could you describe what the target represents as it seems you are trying to reuse the model input as the target tensor?</p>
</blockquote>
</aside>
<p>The target represents the class label tensor object.<br>
Firstly with the data train loader, the dataset is read with img_data and label.<br>
Then, img_data is trained in the net, and the <strong>labels</strong> tensor object is just as it is.<br>
The <strong>pred_d</strong> is the tensor outputted from the trained net (vision transformer in the current case).<br>
If my explanation doesn’t meet ur question, please kindly point me again.<br>
Thank you.</p>

username: ptrblck
cooked: <p>Your explanation doesn’t explain why the target tensors have 4 dimensions:</p>
<aside class="quote no-group" data-username="naychelynn" data-post="1" data-topic="164374">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/e68b1a/48.png" class="avatar"> naychelynn:</div>
<blockquote>
<p>but the input label size is torch.Size([8, 3, 224, 224])</p>
</blockquote>
</aside>
<p>If the targets are plain labels, I would expect a 1D tensor in the shape <code>[batch_size]</code> containing class indices while the model should output a 2D tensor in the shape <code>[batch_size, nb_classes]</code> for a multi-class classification use case.</p>

username: naychelynn
cooked: <p>My case is the regression case and the labels (scores) are in floating points.<br>
4 dimensions of targe tensors are supposed to [outputted tensors from Dropout, dimensions of the image, width of the image, height of the image].</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/c/cc9b9d68f31c6b02c3505e4fab1991159e22237c.png" alt="image" data-base62-sha1="tc2O3kYnTvIn2pS3A7daNSEHRoM" width="596" height="236"></p>
<p>Now, I am about to compute the cross-entropy loss with the criterion function for the target and label as:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/7/e/7e33693474a620c567a7420e2fe5a0ce120e8cab.png" alt="image" data-base62-sha1="i0qj8O9HphC2tJfaA8kVlmtw2fN" width="478" height="41"></p>
<p>It works now with the long awaiting time.</p>