TypeError: sum() received an invalid combination of arguments - got (axis=NoneType, out=NoneType, ),

训练深度学习代码时遇到的一些报错(持续报错,持续更新…)
当然希望最好不报错…

1、TypeError: sum() received an invalid combination of arguments - got (axis=NoneType, out=NoneType, ), but expected one of:
(*, torch.dtype dtype)
didn’t match because some of the keywords were incorrect: axis, out
(tuple of names dim, bool keepdim, *, torch.dtype dtype)
(tuple of ints dim, bool keepdim, *, torch.dtype dtype)

可能的解决方法:
报错信息显示 np.sum 方法在接收到 PyTorch tensor 时出现了问题。它似乎接收到了一个 PyTorch tensor,但 numpy 的函数并不支持直接处理 PyTorch tensor。
用以下方法将 PyTorch tensor 转换为 numpy array:

numpy_array = tensor.cpu().numpy()
#给所有的numpy前面都加上cpu()

2、RuntimeError: Invalid device string: ‘cuda: 0’
当在 PyTorch 中引用 CUDA 设备时,通常会使用 'cuda' 或者 'cuda:0'(如果有多个 GPU,使用 'cuda:1''cuda:2',等等)。
当前使用的设备字符串'cuda: 0'中间有一个空格。将设备字符串改为 'cuda:0',将所有的 'cuda: 0' 替换为 'cuda:0',应该就可以解决这个问题。

disc_net.to(device='cuda: 0')

应改写为:

disc_net.to(device='cuda:0')

3、RuntimeError: CUDA error:
CUBLAS_STATUS_NOT_INITIALIZED when calling cublasCreate(handle)

①先检查CUDA版本:请确保你正在使用的 CUDA 版本与你的 pytorch 版本兼容。可以在 pytorch 官网上找到建议的 CUDA 版本。
清理GPU内存:在每次运行代码之前,尝试清理你的GPU内存。你可以使用下面的代码来释放未使用的内存。

torch.cuda.empty_cache()

③检查代码:确保你的代码没有在多个GPU上并行运行时产生不一致性。有时,将模型放在一个GPU上运行而不分配给多个GPU可以解决问题。你可以用 torch.device(“cuda:0”) 来尝试分配你的模型和数据到一个特定的GPU。
④检查系统资源:如果你运行了一些占用大量GPU资源的程序,可能会影响到这个代码的运行。试着关闭这些资源密集型程序,看看是否解决了问题。
⑤重启:最后,如果上述操作都不起作用,尝试重新启动机器。有时候,这个简单的操作能够解决许多看似复杂的问题。
(我是用②解决的)

猜你喜欢

转载自blog.csdn.net/change_xzt/article/details/134577664
今日推荐