【问题标题】:torch.manual_seed(seed) get RuntimeError: CUDA error: device-side assert triggeredtorch.manual_seed(seed) 获取 RuntimeError:CUDA 错误:触发了设备端断言
【发布时间】:2022-04-19 17:03:11
【问题描述】:

当我收到此错误时,我正在使用 GOOGLE COLAB。这是我的代码,我没有发现任何错误,这些代码在几个小时前是正确的,但突然出错了,我不知道为什么

import torch
if torch.cuda.is_available():       
    device = torch.device("cuda")
    print('There are %d GPU(s) available.' % torch.cuda.device_count())
    print('We will use the GPU:', torch.cuda.get_device_name(0))
else:
    print('No GPU available, using the CPU instead.')
    device = torch.device("cpu")
seed=1
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True 

错误是

There are 1 GPU(s) available.
We will use the GPU: Tesla P100-PCIE-16GB
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-121-436d9d8bb120> in <module>()
      9 seed=1
     10 np.random.seed(seed)
---> 11 torch.manual_seed(seed)
     12 torch.cuda.manual_seed_all(seed)
     13 torch.backends.cudnn.deterministic = True

3 frames
/usr/local/lib/python3.7/dist-packages/torch/cuda/random.py in cb()
    109         for i in range(device_count()):
    110             default_generator = torch.cuda.default_generators[i]
--> 111             default_generator.manual_seed(seed)
    112 
    113     _lazy_call(cb, seed_all=True)

RuntimeError: CUDA error: device-side assert triggered
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.

谁能帮帮我?

【问题讨论】:

    标签: python pytorch google-colaboratory


    【解决方案1】:

    根据我的经验,发生此错误的原因可能是目标中的标签数量与模型中的类数量之间存在某种不一致。

    要解决它,您可以尝试:

    1. 确保目标数据中的标签从 0 开始。如果数据中有 n 个类,则目标类应为 [0, 1, 2,..., n-1]
    2. 确保您使用的模型设置为可与 n 个类一起使用

    【讨论】:

      猜你喜欢
      • 2019-04-15
      • 2021-10-14
      • 2020-04-07
      • 2020-02-03
      • 2019-09-10
      • 2022-09-24
      • 2021-09-10
      • 2021-09-17
      • 2023-01-13
      相关资源
      最近更新 更多