Pytorch official example running error example?

username: hpf
cooked: <p>I tried to run the example of <em><strong>CrossEntropyLoss</strong></em> in the picture and there was an error<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/8/28355a97b9885aed8a01e8260b4d5ea5ebe52290.png" data-download-href="https://discuss.pytorch.org/uploads/default/28355a97b9885aed8a01e8260b4d5ea5ebe52290" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/8/28355a97b9885aed8a01e8260b4d5ea5ebe52290_2_690x361.png" alt="image" data-base62-sha1="5JHpm5RnZrnDfd58KbrHsqtlG24" width="690" height="361" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/8/28355a97b9885aed8a01e8260b4d5ea5ebe52290_2_690x361.png, https://discuss.pytorch.org/uploads/default/optimized/3X/2/8/28355a97b9885aed8a01e8260b4d5ea5ebe52290_2_1035x541.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/2/8/28355a97b9885aed8a01e8260b4d5ea5ebe52290.png 2x" data-dominant-color="F5F6F6"><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">1186×622 45.3 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><strong>The code is as follows</strong></p>
<pre><code class="lang-auto"># Example of target with class indices
loss = nn.CrossEntropyLoss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.empty(3, dtype=torch.long).random_(5)
output = loss(input, target)
output.backward()

# Example of target with class probabilities
input = torch.randn(3, 5, requires_grad=True)
target = torch.randn(3, 5).softmax(dim=1)
output = loss(input, target)
output.backward()
</code></pre>
<p><strong>The error is as follows</strong></p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_32/2605795972.py in &lt;module&gt;
      9 input = torch.randn(3, 5, requires_grad=True)
     10 target = torch.randn(3, 5).softmax(dim=1)
---&gt; 11 output = loss(input, target)
     12 output.backward()

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050                 or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used
   1053         full_backward_hooks, non_full_backward_hooks = [], []

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
   1119     def forward(self, input: Tensor, target: Tensor) -&gt; Tensor:
   1120         return F.cross_entropy(input, target, weight=self.weight,
-&gt; 1121                                ignore_index=self.ignore_index, reduction=self.reduction)
   1122 
   1123 

/opt/conda/lib/python3.7/site-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
   2822     if size_average is not None or reduce is not None:
   2823         reduction = _Reduction.legacy_get_string(size_average, reduce)
-&gt; 2824     return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
   2825 
   2826 

RuntimeError: 1D target tensor expected, multi-target not supported
</code></pre>
<p><strong>question:</strong></p>
<pre><code class="lang-auto">1)  Obviously, the error is reported in line 11. Does this indicate that there is an error in this example?
2)  In the classification problem,does the label data structure of the input  crossentropyloss() need to be [n, 1]?
</code></pre>

username: Matias_Vasquez
cooked: <p>Hi,</p>
<p>which <code>__version__</code> of pytorch do you have?</p>
<p>For version <code>1.9.1</code> and backwards you only get this <a href="https://pytorch.org/docs/1.9.1/generated/torch.nn.CrossEntropyLoss.html?highlight=crossentropy#torch.nn.CrossEntropyLoss" rel="noopener nofollow ugc">example</a>.</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/d/4/d40af9381a0ec480cabf262d4cbc4a939f805fd0.png" alt="image" data-base62-sha1="ufOJwWzgp3xuzlgcAxgAyoVh4hq" width="525" height="186"></p>
<p>So if you have an older version you might want to consider updating it.</p>

username: hpf
cooked: <p>thanks for your reply.I noticed the problem with the version.But I use version 1.11.</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/9/99a6433ceaadaece327329fa6ffad749548df772.png" alt="image" data-base62-sha1="lVfgGgtsbIyydvHv9jKBBVtPldw" width="443" height="319"></p>
<p>And I found that starting from version 1.10, the code case is consistent with the screenshot of my running error.<br>
So maybe the example supplemented by the new version is not applicable?</p>

username: Matias_Vasquez
cooked: <p>I am using <code>1.11.0</code> and the example is working. (as shown on the image below)</p>
<p>If you are also using this version, then there is something wrong.</p>
<p>Even if you are using this version, maybe you can try and run the following commands to see if the error goes away.</p>
<pre data-code-wrap="python"><code class="lang-python">!pip install --upgrade torch
!pip install --upgrade torchvision
</code></pre>
<p>If this does not help, then maybe you can post your environment and settings.</p>
<p>This image is just to show that it works.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/3/b/3b53b57d039dc89f282530e371394dc909a92aa6.png" alt="image" data-base62-sha1="8sPw0M4X74FN2qLiHyZaYP8RTX8" width="478" height="346"></p>

username: hpf
cooked: <aside class="quote no-group" data-username="Matias_Vasquez" data-post="4" data-topic="150213">
<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/matias_vasquez/48/47847_2.png" class="avatar"> Matias_Vasquez:</div>
<blockquote>
<pre><code class="lang-auto">!pip install --upgrade torch
!pip install --upgrade torchvision
</code></pre>
</blockquote>
</aside>
<p>thank you，you are right. My problem was solved perfectly</p>