Device-side assert triggered,can not find the solution

username: huihuiM
cooked: <p>When I try to calcuate the loss value of the expected value and the label value,I got the exception, even though there are some simliar topic, but I still cannot found the solution.<br>
Can you help me?</p>
<p>File “/usr/local/lib/python3.6/site-packages/torch/tensor.py”, line 71, in <strong>repr</strong><br>
return torch._tensor_str._str(self)<br>
File “/usr/local/lib/python3.6/site-packages/torch/_tensor_str.py”, line 286, in _str<br>
tensor_str = _tensor_str(self, indent)<br>
File “/usr/local/lib/python3.6/site-packages/torch/_tensor_str.py”, line 201, in _tensor_str<br>
formatter = _Formatter(get_summarized_data(self) if summarize else self)<br>
File “/usr/local/lib/python3.6/site-packages/torch/_tensor_str.py”, line 87, in <strong>init</strong><br>
nonzero_finite_vals = torch.masked_select(tensor_view, torch.isfinite(tensor_view) &amp; tensor_view.ne(0))<br>
RuntimeError: cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THC/THCReduceAll.cuh:327</p>
<p>My code is <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<pre><code>for step,(x,y1) in enumerate(train_loader):
    
    x_data, y_model = x.to(device), y1.to(device)

    optimizer.zero_grad()

    output_model = model(x_data, h0)

    print("output_model.shape:", output_model.size())
    print("y_model.shape:", y_model.size())

    print("output_model:", output_model)
    print("y_model:", y_model)
    
    loss_model = loss_func(output_model, y_model)
    print("loss_model:", loss_model)    # the error occured in this line.
</code></pre>
<p>the printed result is:<br>
output_model.shape: torch.Size([64, 2])<br>
y_model.shape: torch.Size([64])</p>
<p>output_model: tensor([[-0.9913, -0.4638],<br>
[-0.9701, -0.4765],<br>
[-1.0105, -0.4526],<br>
[-0.9497, -0.4891],<br>
[-1.0548, -0.4281],<br>
[-1.1764, -0.3687],<br>
[-0.9274, -0.5035],<br>
[-0.9197, -0.5086],…<br>
[-0.9032, -0.5197]], device=‘cuda:0’, grad_fn=)]</p>
<p>y_model: tensor([3, 3, 2, 1, 0, 3, 2, 2, 1, 3, 1, 3, 1, 1, 1, 1, 0, 3, 1, 1, 0, 1, 2, 2,<br>
2, 1, 0, 1, 0, 2, 2, 2, 0, 3, 1, 1, 1, 1, 3, 2, 2, 2, 1, 1, 1, 3, 3, 0,<br>
2, 2, 2, 3, 3, 0, 0, 2, 3, 0, 3, 2, 2, 3, 3, 2], device=‘cuda:0’)</p>

username: Oli
cooked: <p>I don’t know why the crash is happening but your model outputs and labels aren’t the same shape. Are you trying to do binary classification? Then I suspect that you want to change the model to only have one output instead of two.</p>

username: huihuiM
cooked: <aside class="quote no-group" data-username="huihuiM" data-post="1" data-topic="52952">
<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/h/c68b51/48.png" class="avatar"> huihuiM:</div>
<blockquote>
<p>[-0.9032, -0.5197]</p>
</blockquote>
</aside>
<p>yes, the problem  is the binary classification. But I have not do the one hot encoding for the output model.<br>
the last operation of the model is:</p>
<p>F.log_softmax(pred_model)</p>
<p>Is there some problem?</p>

username: huihuiM
cooked: <p>Thank you.<br>
I check the model outputs and labels again.<br>
And the problem is that there are not the same shape.<br>
I have fixed the bug.<br>
Thanks</p>