Expected object of device type cuda but got device type cpu for argument #2 'target' in call to _thnn_nll_loss_forward

Omroth: <p>I’m trying to train with CrossEntropyLoss but my argmax method is failing with</p>
<p>RuntimeError: Expected object of device type cuda but got device type cpu for argument <span class="hashtag">#2</span> ‘target’ in call to _thnn_nll_loss_forward</p>
<p>at the loss = criterion(prediction, target_index) line.  Can anyone point to my mistake?</p>
<pre><code class="lang-auto">    training_dataset_generator = torch.utils.data.DataLoader(traning_dataset, **params)
    criterion = torch.nn.CrossEntropyLoss(reduction='sum', weight=torch.Tensor([1.0, 10.0]))
    optimizer = torch.optim.SGD(model.parameters(), lr=ml_def.learning_rate)

    for epoch in range(max_epochs):
        print("doing epoch")
        epoch_loss = 0.0
        for batch, labels in training_dataset_generator:
            # transfer to gpu
            local_batch, local_labels = batch.to(ml_def.device), labels.to(ml_def.device)

            prediction = model(local_batch)

            local_labels_float = local_labels.float()

            values, indices = local_labels.max(0)

            target_index = Variable(torch.LongTensor([indices[0]]))

            loss = criterion(prediction, target_index)

            epoch_loss += loss

            #print("val", local_batch[0], "original", labels[0], "prediction", prediction.detach()[0], "loss", loss)
            
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
</code></pre>

harsha_g: <aside class="quote no-group" data-username="Omroth" data-post="1" data-topic="86913">
<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/o/fbc32d/48.png" class="avatar"> Omroth:</div>
<blockquote>
<p><code> local_labels_float = local_labels.float()</code></p>
</blockquote>
</aside>
<p>I am not entirely sure what this line is doing in this code here. <code>local_labels_float</code> is not being used at all.</p>
<aside class="quote no-group" data-username="Omroth" data-post="1" data-topic="86913">
<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/o/fbc32d/48.png" class="avatar"> Omroth:</div>
<blockquote>
<p><code>target_index = Variable(torch.LongTensor([indices[0]]))</code></p>
</blockquote>
</aside>
<p>Also, why not just transfer <code>target_index</code> to the gpu directly instead of sending <code>labels</code> and then doing max and indexing operations. Maybe that’s the problem.</p>
<pre><code class="lang-auto">for batch, labels in training_dataset_generator:
            values, indices = local_labels.max(0)
            target_index = indices[0]
            # transfer to gpu
            local_batch, target_index = batch.to(ml_def.device), target_index.to(ml_def.device)

            prediction = model(local_batch)
            loss = criterion(prediction, target_index)
</code></pre>
<p>Note that <code>indices</code> is already a <code>Longtensor</code>.  By the looks of it, you may have to (although I am not entirely sure) use an <code>unsqueeze</code> operation on the <code>target_index</code>, i.e. <code>target_index = target_index.unsqueeze(0)</code> before transferring to the GPU.</p>