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

for epoch in range(start_epoch, start_epoch + epochs):

    print('\n\n\nEpoch: {}\n<Train>'.format(epoch))
    net.train(True)
    loss = 0
    learning_rate = learning_rate * (0.5 ** (epoch // 4))
    for param_group in optimizer.param_groups:
        param_group["learning_rate"] = learning_rate
    torch.set_grad_enabled(True)
    for idx, (inputs, targets, paths) in enumerate(trainloader):
        inputs, targets = inputs.to(device), targets.to(device)
        outputs = net(inputs)
        if type(outputs) == tuple:
            outputs = outputs[0]
        batch_loss = dice_coef(outputs, targets)
        optimizer.zero_grad()
        batch_loss.backward()
        optimizer.step()
        loss += float(batch_loss)
        progress_bar(idx, len(trainloader), 'Loss: %.5f, Dice-Coef: %.5f'
                     % ((loss / (idx + 1)), (1 - (loss / (idx + 1)))))
    log_msg = '\n'.join(['Epoch: %d  Loss: %.5f,  Dice-Coef:  %.5f' \
                         % (epoch, loss / (idx + 1), 1 - (loss / (idx + 1)))])
    logging.info(log_msg)

def dice_coef(preds, targets, backprop=True):
smooth = 1.0
class_num = 2
if backprop:
for i in range(class_num):
pred = preds[:,i,:,:]
target = targets[:,i,:,:]
intersection = (pred * target).sum()
loss_ = 1 - ((2.0 * intersection + smooth) / (pred.sum() + target.sum() + smooth))
if i == 0:
loss = loss_
else:
loss = loss + loss_
loss = loss/class_num
return loss
else:
# Need to generalize
targets = np.array(targets.argmax(1))
if len(preds.shape) > 3:
preds = np.array(preds).argmax(1)
for i in range(class_num):
pred = (preds==i).astype(np.uint8)
target= (targets==i).astype(np.uint8)
intersection = (pred * target).sum()
loss_ = 1 - ((2.0 * intersection + smooth) / (pred.sum() + target.sum() + smooth))
if i == 0:
loss = loss_
else:
loss = loss + loss_
loss = loss/class_num
return loss

I facing this type of error anyone knows how to solve it?

As the error message suggests, you would have to push the tensor to the CPU first before converting it to a numpy array via tensor.cpu().
In particular np.array(targets.argmax(1)) seems to raise the error to use:

targets = targets.argmax(1).cpu().numpy()

instead.

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.

1 Like

Thanks for your help.

Oic, thanks for your advise I will try to use it ^^

I have a similar error that I think is arising when I try to use sklearn’s roc_auc_score:

Exception has occurred: TypeError
Caught TypeError in replica 0 on device 0.
Original Traceback (most recent call last):
  File "/home/ubuntu/deep-behavior-embedding/.virtualenv/lib/python3.7/site-packages/torch/nn/parallel/parallel_apply.py", line 61, in _worker
    output = module(*input, **kwargs)
  File "/home/ubuntu/deep-behavior-embedding/.virtualenv/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/ubuntu/deep-behavior-embedding/.virtualenv/lib/python3.7/site-packages/pytorch_lightning/overrides/data_parallel.py", line 77, in forward
    output = super().forward(*inputs, **kwargs)
  File "/home/ubuntu/deep-behavior-embedding/.virtualenv/lib/python3.7/site-packages/pytorch_lightning/overrides/base.py", line 57, in forward
    output = self.module.validation_step(*inputs, **kwargs)
  File "/home/ubuntu/deep-behavior-embedding/src/model/lightning_model.py", line 144, in validation_step
    auc = roc_auc_score(y_true=y, y_score=torch.sigmoid(x))
  File "/home/ubuntu/deep-behavior-embedding/.virtualenv/lib/python3.7/site-packages/sklearn/utils/validation.py", line 63, in inner_f
    return f(*args, **kwargs)
  File "/home/ubuntu/deep-behavior-embedding/.virtualenv/lib/python3.7/site-packages/sklearn/metrics/_ranking.py", line 522, in roc_auc_score
    y_type = type_of_target(y_true)
  File "/home/ubuntu/deep-behavior-embedding/.virtualenv/lib/python3.7/site-packages/sklearn/utils/multiclass.py", line 261, in type_of_target
    if is_multilabel(y):
  File "/home/ubuntu/deep-behavior-embedding/.virtualenv/lib/python3.7/site-packages/sklearn/utils/multiclass.py", line 147, in is_multilabel
    y = np.asarray(y)
  File "/home/ubuntu/deep-behavior-embedding/.virtualenv/lib/python3.7/site-packages/numpy/core/_asarray.py", line 102, in asarray
    return array(a, dtype, copy=False, order=order)
  File "/home/ubuntu/deep-behavior-embedding/.virtualenv/lib/python3.7/site-packages/torch/_tensor.py", line 643, in __array__
    return self.numpy()
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
  File "/home/ubuntu/deep-behavior-embedding/finetune.py", line 108, in <module>
    trainer.fit(model, datamodule=dm)

The code is here:

    def validation_step(self, batch, batch_idx):
        x = batch['src']
        y = batch['label']
        mask = batch['mask']

        x = self.base_model(x, mask)
        x = self.linear(x).mean(axis=1).squeeze(1)
        
        loss = F.binary_cross_entropy_with_logits(input=x,
                               target=y)
        self.log('val_loss', loss)
        try:
            auc = roc_auc_score(y_true=y, y_score=torch.sigmoid(x))
            self.log('auc', auc)
        except ValueError:
            self.log('auc', 0)
        return loss

Should I be converting to numpy arrays? I’m afraid this might slow things down.