gpt4 book ai didi

numpy - TypeError : can’t convert CUDA tensor to numpy. 首先使用 Tensor.cpu() 将张量复制到主机内存 (fastai)

转载 作者:行者123 更新时间:2023-12-04 10:12:50 28 4
gpt4 key购买 nike

我正在关注这里的代码:

https://www.kaggle.com/tanlikesmath/diabetic-retinopathy-with-resnet50-oversampling

但是,在指标计算期间,我收到以下错误:

File "main.py", line 50, in <module>
learn.fit_one_cycle(4,max_lr = 2e-3)
...
File "main.py", line 39, in quadratic_kappa
return torch.tensor(cohen_kappa_score(torch.argmax(y_hat,1), y, weights='quadratic'),device='cuda:0')
...
File "/pfs/work7/workspace/scratch/ul_dco32-conda-0/conda/envs/resnet50/lib/python3.8/site-packages/torch/tensor.py", line 486, in __array__
return self.numpy()
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

以下是指标和模型:
def quadratic_kappa(y_hat, y):
return torch.tensor(cohen_kappa_score(torch.argmax(y_hat,1), y, weights='quadratic'),device='cuda:0')

learn = cnn_learner(data, models.resnet50, metrics = [accuracy,quadratic_kappa])
learn.fit_one_cycle(4,max_lr = 2e-3)


正如讨论中所说的那样 https://discuss.pytorch.org/t/typeerror-can-t-convert-cuda-tensor-to-numpy-use-tensor-cpu-to-copy-the-tensor-to-host-memory-first/32850/6 ,我得把数据带回 cpu .但我有点迷失了该怎么做。

我尝试添加 .cpu()所有指标,但到目前为止无法解决。

最佳答案

我假设这两个 yy_hat是 CUDA 张量,这意味着您需要将它们都带到 CPU 以获取 cohen_kappa_score ,不止一个。

def quadratic_kappa(y_hat, y):
return torch.tensor(cohen_kappa_score(torch.argmax(y_hat.cpu(),1), y.cpu(), weights='quadratic'),device='cuda:0')
# ^^^ ^^^

调用 .cpu()对已经在 CPU 上的张量没有影响,因此在任何情况下都可以安全使用。

关于numpy - TypeError : can’t convert CUDA tensor to numpy. 首先使用 Tensor.cpu() 将张量复制到主机内存 (fastai),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61236178/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com