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

I am confused, that I am getting a message -

Automatically created module for IPython interactive environment
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-5ffbb87ce56b> in <module>()
     17 roc_auc = dict()
     18 for i in range(nb_classes):
---> 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(
--> 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
--> 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 
--> 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__'):
--> 140         y = np.asarray(y)
    141     if not (hasattr(y, "shape") and y.ndim == 2 and y.shape[1] > 1):
    142         return False

/usr/local/lib/python3.6/dist-packages/numpy/core/numeric.py in asarray(a, dtype, order)
    490 
    491     """
--> 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:
--> 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.

I am trying to make a multi-class ROC curve from my dataset. Some friend told me that, in the documentation for sklearn.metrics.roc_curve says:

“Note: this implementation is restricted to the binary classification task.”

that is -

# Binarize the output
y = label_binarize(y, classes=[0, 1, 2])
n_classes = y.shape[1]

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 -

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()

Can any one of you guys please tell me how to print ROC curve from it? Thanks a lot.

In that function, you have to use roc_curve(labels.view(-1).cpu(), prediction.view(-1).cpu())