Dimension specified as 0 but tensor has no dimensions?

ky_Pa: <p>I load src_image and src_label from src_dataloader and convert them into variables. Then enter the classifier and use cross entropy to calculate the loss, but this error occurs. How should I solve it?</p>
<pre><code class="lang-auto">###### Classifier A ######
        optimizer_C_A.zero_grad()

        # Classifier loss
        pred = netC_A(netG_A2B(real_A))
        loss_C = criterion_class(pred, label_A)
        loss_C.backward()

        optimizer_C.step()
        ###################################
</code></pre>
<pre><code class="lang-auto">for step, (src_image, src_label) in enumerate(src_dataloader):
    real_A = make_variable(src_image)
    label_A = make_variable(src_label.squeeze_())
</code></pre>
<pre><code class="lang-auto">def make_variable(tensor):
    """Convert Tensor to Variable."""
    if torch.cuda.is_available():
        tensor = tensor.cuda()
    return Variable(tensor)
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/2/b2ec23b9b318adc51ffebb839ad04dec5cb44c93.png" data-download-href="https://discuss.pytorch.org/uploads/default/b2ec23b9b318adc51ffebb839ad04dec5cb44c93" title="微信图片_20200629091741"><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/2/b2ec23b9b318adc51ffebb839ad04dec5cb44c93.png" alt="微信图片_20200629091741" data-base62-sha1="pwOUODaiuwjFYuBNwj9vTizAbdN" width="690" height="171" data-dominant-color="101010"><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">微信图片_20200629091741</span><span class="informations">1015×253 23.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>

ptrblck: <p>Either <code>pred</code> or <code>label_A</code> seem to be a scalar, while a tensor with at least one dimension is required.<br>
Based on your code, I guess that <code>label_A</code> might be the scalar tensor, since you are <code>squeezing</code> it.</p>
<p>For a multi-class classification, the output should have the shape <code>[batch_size, nb_classes]</code> and the target <code>[batch_size]</code>.</p>
<p>Also, <code>Variables</code> are deprecated, so you should use tensors now.</p>

ky_Pa: <p>Hi, ptrblck!<br>
Both real_A and label_A are the images and labels that I loaded using ImageFolder. They should be in the form of tensors. So do I no longer need to use Variables to convert to variables?But I see that there are steps to convert variables in many similar codes. If I remove it, will pred or label_A still be able to enter the built network smoothly?<br>
Can you tell me how to modify the code? Thank you!</p>

ptrblck: <aside class="quote no-group" data-username="ky_Pa" data-post="3" data-topic="87245">
<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/k/eada6e/48.png" class="avatar"> ky_Pa:</div>
<blockquote>
<p>They should be in the form of tensors.</p>
</blockquote>
</aside>
<p>They are most likely tensors, but the <code>squeeze_</code> operation could remove the single dimension as seen here:</p>
<pre data-code-wrap="python"><code class="lang-python">x = torch.tensor([1])
print(x.shape)
&gt; torch.Size([1])
print(x.squeeze().shape)
&gt; torch.Size([])
</code></pre>
<p>Could you explain, why <code>label_A = make_variable(src_label.squeeze_())</code> is needed?<br>
Are you trying to squeeze a specific dimension? If so, you should use the <code>dim</code> argument.</p>
<aside class="quote no-group" data-username="ky_Pa" data-post="3" data-topic="87245">
<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/k/eada6e/48.png" class="avatar"> ky_Pa:</div>
<blockquote>
<p>So do I no longer need to use Variables to convert to variables?But I see that there are steps to convert variables in many similar codes.</p>
</blockquote>
</aside>
<p>These code snippets are also using the deprecated <code>Variables</code>.</p>
<aside class="quote no-group" data-username="ky_Pa" data-post="3" data-topic="87245">
<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/k/eada6e/48.png" class="avatar"> ky_Pa:</div>
<blockquote>
<p>If I remove it, will pred or label_A still be able to enter the built network smoothly?<br>
Can you tell me how to modify the code?</p>
</blockquote>
</aside>
<p>Yes, you can simply pass tensors to the model.<br>
If your input does not require gradients (which is the usual case), then just remove the <code>Variable(tensor)</code> call completely.</p>

ky_Pa: <p>Thank you for your detailed answer!</p>
<aside class="quote no-group" data-username="ptrblck" data-post="4" data-topic="87245">
<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 explain, why <code>label_A = make_variable(src_label.squeeze_())</code> is needed?</p>
</blockquote>
</aside>
<ol>
<li>I originally wanted to use squeeze_ to convert the label into a one-dimensional tensor, but now I find that this is not necessary. So I removed squeeze_ and did not report the above error again.This is the label_A that I printed after removing squeeze_. Can it be used directly without the need to transform the form?<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/a/7/a7f0f0397d785fa6d6abf2eee70ead97febb29aa.png" alt="微信图片_20200630121547" data-base62-sha1="nXFWD6EFhV9jMoklG6aCosAPDAK" width="300" height="33"><br>
But I received the following warning:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/7/47c96fc8bef9f061fb734ade3fe038bc18e6286e.png" data-download-href="https://discuss.pytorch.org/uploads/default/47c96fc8bef9f061fb734ade3fe038bc18e6286e" title="微信图片_20200630121900"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/7/47c96fc8bef9f061fb734ade3fe038bc18e6286e_2_690x28.png" alt="微信图片_20200630121900" data-base62-sha1="af3txt6vbnoXGI6uwnUIi2inf6m" width="690" height="28" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/7/47c96fc8bef9f061fb734ade3fe038bc18e6286e_2_690x28.png, https://discuss.pytorch.org/uploads/default/optimized/3X/4/7/47c96fc8bef9f061fb734ade3fe038bc18e6286e_2_1035x42.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/4/7/47c96fc8bef9f061fb734ade3fe038bc18e6286e_2_1380x56.png 2x" data-dominant-color="0C0C0C"><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">微信图片_20200630121900</span><span class="informations">1883×79 7.93 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><br>
What caused this?</li>
</ol>
<aside class="quote no-group" data-username="ptrblck" data-post="4" data-topic="87245">
<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>Yes, you can simply pass tensors to the model.<br>
If your input does not require gradients (which is the usual case), then just remove the <code>Variable(tensor)</code> call completely.</p>
</blockquote>
</aside>
<ol start="2">
<li>After I removed Variables, I encountered this error when entering the network. How do I deal with it?</li>
</ol>
<pre><code class="lang-auto">preds = classifier(encoder(images))
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/4/14be10742a436c29b1af8323e46d2994269a8b4c.png" data-download-href="https://discuss.pytorch.org/uploads/default/14be10742a436c29b1af8323e46d2994269a8b4c" title="微信图片_20200630122708"><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/4/14be10742a436c29b1af8323e46d2994269a8b4c.png" alt="微信图片_20200630122708" data-base62-sha1="2XuL8lfIfS0jN8SD4mSNE5rslOI" width="690" height="201" data-dominant-color="353630"><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">微信图片_20200630122708</span><span class="informations">1470×429 30.6 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: <ol>
<li>
<p><code>nn.MSELoss</code> warns you, if your output and target shapes are different, as broadcasting will be applied, which might yield wrong results. If your output has the size <code>[batch_size, 1]</code>, make sure to pass the target is the same shape. Currently dim1 seems to be missing, so you might want to call <code>unsqueeze(1)</code> on the target.</p>
</li>
<li>
<p>The input tensor seems to be on the CPU, while a <code>CUDATensor</code> is expected, so you would need to push the tensor to the device.</p>
</li>
</ol>
<p>PS: It’s better to post code snippets by wrapping them into three backticks ```, as it makes debugging easier. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

ky_Pa: <ol>
<li>This is the code I used for <code>nn.MSELoss</code>. Does output and target shapes here refer to pred_fake and label_real in the code, respectively?I printed out their shapes.</li>
</ol>
<pre><code class="lang-auto">fake_B = netG_A2B(real_A)
        pred_fake = netD_B(fake_B)
        loss_GAN_A2B = criterion_GAN(pred_fake, label_real)
</code></pre>
<pre><code class="lang-auto">criterion_GAN = torch.nn.MSELoss()
</code></pre>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/d/7/d7a69f3eac05f4fa47736966ae7ea700216eadae.png" alt="微信图片_20200630164012" data-base62-sha1="uLJDOMl5H3ZbrrRJYyPhYg1Tr5Q" width="607" height="85"><br>
2. In other words, if CUDA is available, then I need to push the input tensor from the CPU to the GPU. If CUDA is not used, the input tensor on the CPU can directly enter the network without the need to do Variable (tensor) processing, because the effect is the same, right?</p>
<p>PS: In addition to the code snippets, should the result of the operation also be wrapped into three backticks ```? To replace those pictures?</p>

ptrblck: <ol>
<li>
<p>Yes, exactly. Your target has the shape <code>[1]</code>, while the output uses <code>[1, 1]</code>, which will raise this warning. Just to make sure to avoid future issues, use <code>label_real = label_real.unsqueeze(1)</code> before passing it to <code>criterion_GAN</code>.</p>
</li>
<li>
<p>You have to push the model as well as all inputs to the right device. If you are using the GPU, you would thus have to transfer the data via <code>x = x.to('cuda')</code> or <code>x = x.cuda()</code>. To transfer it back, use <code>x = x.to('cpu')</code>. <code>Variables</code> are not needed anymore at all, neither for CUDA operations nor for CPU ops.</p>
</li>
</ol>
<p>Yes, please post all code snippets and the outputs as formatted code, as it’s easier to debug and the forum search can index it. Pictures are not always easy to read, cannot be used to copy the source code for debugging, and the search won’t index it.</p>

ky_Pa: <p>Thank you very much for your patience and detailed help! I understand most of what you said and solved the problem 1.<br>
For question 2, I want to confirm whether the inputs form CPU is tensor or Variable(tensor) before using cuda? Or the two forms are the same, because I understand at what version it seems that Variable and tensor are merged?</p>

ptrblck: <aside class="quote no-group" data-username="ky_Pa" data-post="9" data-topic="87245">
<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/k/eada6e/48.png" class="avatar"> ky_Pa:</div>
<blockquote>
<p>For question 2, I want to confirm whether the inputs form CPU is tensor or Variable(tensor) before using cuda? Or the two forms are the same, because I understand at what version it seems that Variable and tensor are merged?</p>
</blockquote>
</aside>
<p>Yes, they are the same since PyTorch <code>0.4</code>.<br>
You can check the tensor directly, if it requires gradients etc, but there is no <code>Variable</code> usage anymore.</p>

ky_Pa: <p>Ok, I see. Thanks again! <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>