Hello Deb!



 Deb_Prakash_Chatterj:

Well, there is a new problem though, can you help in this case?


First, in general, you should probably start a new thread for
things like this.  (I do have some comments, below.)


Automatically created module for IPython interactive environment
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-37-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/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 have no idea where the “CUDA tensor” error is coming from.  I
suspect it might be a misleading error – not that it isn’t technically
true, but that it’s a (not really relevant) side effect of the “real” error.


I have seen that it causes due to not using argmax, however, I have used it -
...

    images, labels = images.to(device), labels.to(device)



(Would I be right that device is a gpu?  You might try using just
the cpu, but I bet you would still get an error – just a different one.)


...
      p = torch.nn.functional.softmax(output, dim=1)
      prediction = torch.argmax(p, dim=1)
      loss = criterion(output, labels)
...

I am confused now how to print my ROC, I want to print a mutli-class ROC. I have not used any unuseful numpy array. I am new to this. can you please look after this? Thanks a lot.


You say that you are working on a “multi-class classification”
problem, that is, that you have more than two classes.  I suspect
that your prediction = torch.argmax(p, dim=1) returns values
greater than 1.  (That is, if you had five classes, prediction would
take on values from [0, 1, 2, 3, 4].)
The documentation for sklearn.metrics.roc_curve says:
“Note: this implementation is restricted to the binary classification
task.”
The sample code you linked to (an example of how to use
roc_curve() with a multi-class classifier) has things like:
# Binarize the output
y = label_binarize(y, classes=[0, 1, 2])
n_classes = y.shape[1]

and I don’t see anything like that in the code you posted.
So I’m guessing that your error is that you are passing labels
(predictions) to roc_curve() that have values other than just
[0, 1], and that that is triggering a (not very helpful) error
message.
If this doesn’t help, please start a new thread and maybe
someone who (unlike me) actually knows how to use
sklearn.metrics.roc_curve could help.
Best.
K. Frank