TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first

Deb_Prakash_Chatterj: <p>I am confused, that I am getting a message -</p>
<pre><code class="lang-auto">Automatically created module for IPython interactive environment
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-7-5ffbb87ce56b&gt; in &lt;module&gt;()
     17 roc_auc = dict()
     18 for i in range(nb_classes):
---&gt; 19    fpr[i], tpr[i], _ = roc_curve(labels[i], prediction[i]) # here change y_test to labels
     20    roc_auc[i] = auc(fpr[i], tpr[i])
     21 # Compute micro-average ROC curve and ROC area

/usr/local/lib/python3.6/dist-packages/sklearn/metrics/ranking.py in roc_curve(y_true, y_score, pos_label, sample_weight, drop_intermediate)
    616     """
    617     fps, tps, thresholds = _binary_clf_curve(
--&gt; 618         y_true, y_score, pos_label=pos_label, sample_weight=sample_weight)
    619 
    620     # Attempt to drop thresholds corresponding to points in between and

/usr/local/lib/python3.6/dist-packages/sklearn/metrics/ranking.py in _binary_clf_curve(y_true, y_score, pos_label, sample_weight)
    392     """
    393     # Check to make sure y_true is valid
--&gt; 394     y_type = type_of_target(y_true)
    395     if not (y_type == "binary" or
    396             (y_type == "multiclass" and pos_label is not None)):

/usr/local/lib/python3.6/dist-packages/sklearn/utils/multiclass.py in type_of_target(y)
    247         raise ValueError("y cannot be class 'SparseSeries'.")
    248 
--&gt; 249     if is_multilabel(y):
    250         return 'multilabel-indicator'
    251 

/usr/local/lib/python3.6/dist-packages/sklearn/utils/multiclass.py in is_multilabel(y)
    138     """
    139     if hasattr(y, '__array__'):
--&gt; 140         y = np.asarray(y)
    141     if not (hasattr(y, "shape") and y.ndim == 2 and y.shape[1] &gt; 1):
    142         return False

/usr/local/lib/python3.6/dist-packages/numpy/core/numeric.py in asarray(a, dtype, order)
    490 
    491     """
--&gt; 492     return array(a, dtype, copy=False, order=order)
    493 
    494 

/usr/local/lib/python3.6/dist-packages/torch/tensor.py in __array__(self, dtype)
    448     def __array__(self, dtype=None):
    449         if dtype is None:
--&gt; 450             return self.numpy()
    451         else:
    452             return self.numpy().astype(dtype, copy=False)

TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
</code></pre>
<p>I am trying to make a multi-class ROC curve from my dataset. Some friend told me that, in the documentation for  <code>sklearn.metrics.roc_curve</code>  says:</p>
<blockquote>
<p>“Note: this implementation is restricted to the binary classification task.”</p>
</blockquote>
<p>that is -</p>
<pre><code class="lang-auto"># Binarize the output
y = label_binarize(y, classes=[0, 1, 2])
n_classes = y.shape[1]
</code></pre>
<p>In my code I haven’t binarized it, how can I do it? I have no clue about it, I have followed some post about it and found out that, it can cause due to argmax function, So here is the use of it -</p>
<pre><code class="lang-auto">for epoch in range(epochs):
  
  running_loss = 0
  model.train()
  for images, labels in dataloader_train:
    
    #steps += 1
    images, labels = images.to(device), labels.to(device)
    
    optimizer.zero_grad()
    
    output = model.forward(images)
    p = torch.nn.functional.softmax(output, dim=1)
    prediction = torch.argmax(p, dim=1)
    #loss = torch.nn.functional.nll_loss(torch.log(p), y)
    loss = criterion(output, labels)
    loss.backward()
    optimizer.step()
    
    running_loss += loss.item()
    
  #if steps % print_every == 0:
  valid_loss = 0
  accuracy = 0
  model.eval()
  for images, labels in dataloader_test:
    optimizer.zero_grad()
    with torch.no_grad():
       
      images, labels = images.to(device), labels.to(device)

      output = model.forward(images)
      p = torch.nn.functional.softmax(output, dim=1)
      prediction = torch.argmax(p, dim=1)
      loss = criterion(output, labels)
          
      valid_loss += loss.item()
          
      ps = torch.exp(output)
         
      top_p, top_class = ps.topk(1, dim = 1)
      equals = top_class == labels.view(*top_class.shape)
      accuracy += torch.mean(equals.type(torch.FloatTensor))
        
  print("Epoch: {}/{} " .format(epoch+1, epochs))
  print("Train loss: {:.4f}.. " .format(running_loss/len(dataloader_train)))
  print("Valid loss: {:.4f}.. " .format(valid_loss/len(dataloader_test)))
  print("Accuracy: {:.4f}.. " .format(accuracy/len(dataloader_test)))
  model.train()
</code></pre>
<p>Can any one of you guys please tell me how to print ROC curve from it? Thanks a lot.</p>

Ahsan_Habib: <aside class="quote no-group" data-username="Deb_Prakash_Chatterj" data-post="1" data-topic="40305">
<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/deb_prakash_chatterj/48/10121_2.png" class="avatar"> Deb_Prakash_Chatterj:</div>
<blockquote>
<p>fpr[i], tpr[i], _ = roc_curve(labels[i], prediction[i]) # here change y_test to labels</p>
</blockquote>
</aside>
<p>In that function, you have to use <code>roc_curve(labels.view(-1).cpu(), prediction.view(-1).cpu())</code></p>