常见问题

创建日期:2024-06-21
更新日期:2025-03-03

1、按照官网方法安装pytorch报错:pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='download.pytorch.org', port=443): Read timed out.。

按照官网方法安装PyTorch:

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

会出现 torch-2.3.0+cu121-cp312-cp312-win_amd64.whl 的下载地址。

Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://download.pytorch.org/whl/cu121
Collecting torch==2.2.2
  Downloading https://download.pytorch.org/whl/cu121/torch-2.2.2%2Bcu121-cp312-cp312-win_amd64.whl (2454.8 MB)
     ---------------------------------------- 0.0/2.5 GB 21.3 kB/s eta 31:52:56

使用下载工具下载该文件,然后使用pip安装本地whl包。

pip3 install .\torch-2.3.0+cu121-cp312-cp312-win_amd64.whl

然后用官网的方法安装其他包即可。

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

2、CommandNotFoundError: Command Error: error with command 'activate'. Command not found: 'activate'. 'activate' is not a conda command.。

解决方法:升级conda版本。

3、如何使用pandas转独热编码。

import pandas as pd

train_data = pd.read_csv('./csv/iris_training.csv')
print(train_data.head())

dummy_field = 'virginica'
dummies = pd.get_dummies(
    train_data[dummy_field], prefix=dummy_field, drop_first=False)
print(dummies.head())

train_data = pd.concat([train_data, dummies], axis=1)
print(train_data.head())

train_data = train_data.drop(dummy_field, axis=1)
print(train_data.head())

4、OSError: [WinError 126] 找不到指定的模块。 Error loading "C:\Users\liteng\AppData\Roaming\Python\Python312\site-packages\torch\lib\shm.dll" or one of its dependencies.

不要安装torch 2.3.0,改为安装torch 2.2.x。卸载原来的pytorch。

pip uninstall torchvision torchaudio torch

安装pytorch 2.2.2。

pip3 install torch==2.2.2 torchvision==0.17.2+cu121 torchaudio==2.2.2+cu121 --index-url https://download.pytorch.org/whl/cu121

5、在Windows上,DataLoader num_workers大于0时报错: An attempt has been made to start a new process before the current process has finished its bootstrapping phase.

完整错误:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "d:\Python\Python312\Lib\multiprocessing\spawn.py", line 122, in spawn_main
    exitcode = _main(fd, parent_sentinel)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\multiprocessing\spawn.py", line 131, in _main     
    prepare(preparation_data)
  File "d:\Python\Python312\Lib\multiprocessing\spawn.py", line 246, in prepare   
    _fixup_main_from_path(data['init_main_from_path'])
  File "d:\Python\Python312\Lib\multiprocessing\spawn.py", line 297, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\runpy.py", line 286, in run_path
    return _run_module_code(code, init_globals, run_name,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\runpy.py", line 98, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "d:\Python\Python312\Lib\runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "d:\Project\LearnPyTorch\dogs_vs_cats\cnn.py", line 139, in <module>
    for i, (image, label) in enumerate(train_loader):
                             ^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\torch\utils\data\dataloader.py", line 484, in __iter__
    return self._get_iterator()
           ^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\torch\utils\data\dataloader.py", line 415, in _get_iterator
    return _MultiProcessingDataLoaderIter(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\torch\utils\data\dataloader.py", line 1138, in __init__
    w.start()
  File "d:\Python\Python312\Lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
                  ^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\multiprocessing\context.py", line 337, in _Popen
    return Popen(process_obj)
           ^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\multiprocessing\popen_spawn_win32.py", line 46, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\multiprocessing\spawn.py", line 164, in get_preparation_data
    _check_not_importing_main()
  File "d:\Python\Python312\Lib\multiprocessing\spawn.py", line 140, in _check_not_importing_main
    raise RuntimeError('''
RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

        To fix this issue, refer to the "Safe importing of main module"
        section in https://docs.python.org/3/library/multiprocessing.html

在Windows上,应该只允许主线程创建子线程,不允许子线程创建线程。把训练和for循环放到if ~_~_name~_~_ == '~_~_main~_~_'里即可。示例代码:

if __name__ == "__main__":
    for epoch in range(num_epochs):
        for i, (images, labels) in enumerate(train_loader):
            images = images.to(device)
            labels = labels.to(device)

            output = model(images)
            loss = criterion(output, labels)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            if (i + 1) % 100 == 0:
                print(
                    "Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}".format(
                        epoch + 1, num_epochs, i + 1, len(train_loader), loss.item()
                    )
                )

model.eval()

if __name__ == "__main__":
    with torch.no_grad():
        total = 0
        correct = 0
        for images, labels in test_loader:
            images = images.to(device)
            labels = labels.to(device)

            output = model(images)
            _, predict = torch.max(output, 1)

            total += labels.size(0)
            correct += (predict == labels).sum().item()

        print("Accuracy of 10000 test images: {} %".format(correct / total * 100))

6、加载vgg16模型报错:UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.

完整错误:

d:\Python\Python312\Lib\site-packages\torchvision\models\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
d:\Python\Python312\Lib\site-packages\torchvision\models\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=VGG16_Weights.IMAGENET1K_V1`. You can also use `weights=VGG16_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)

把代码由 model = models.vgg16(pretrained=True) 改为以下代码即可:

import torch.nn
from torchvision import models

model = models.vgg16(weights=models.VGG16_Weights.IMAGENET1K_V1)
print(model)

7、使用vgg16迁移学习进行猫狗分类报错:torch.OutOfMemoryError: CUDA out of memory.

完整错误:

Traceback (most recent call last):0-4f93-9a90-3b8b355cbdc6
  File "d:\Python\Python312\Lib\runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\launcher/../..\debugpy\__main__.py", line 71, in <module>
    cli.main()
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 501, in main
    run()
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 351, in run_file
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 310, in run_path
    return _run_module_code(code, init_globals, run_name, pkg_name=pkg_name, script_name=fname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 127, in _run_module_code
    _run_code(code, mod_globals, init_globals, mod_name, mod_spec, pkg_name, script_name)
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 118, in _run_code
    exec(code, run_globals)
  File "d:\Project\LearnPyTorch\dogs_vs_cats\vgg19.py", line 102, in <module>
    output = model(image)
             ^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\torch\nn\modules\module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\torch\nn\modules\module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\torchvision\models\vgg.py", line 66, in forward
    x = self.features(x)
        ^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\torch\nn\modules\module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\torch\nn\modules\module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\torch\nn\modules\container.py", line 250, in forward
    input = module(input)
            ^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\torch\nn\modules\module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\torch\nn\modules\module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    return self._conv_forward(input, self.weight, self.bias)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\torch\nn\modules\conv.py", line 549, in _conv_forward
    return F.conv2d(
           ^^^^^^^^^
torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 614.00 MiB. GPU 0 has a total capacity of 4.00 GiB of which 0 bytes is free. Of the allocated memory 2.54 GiB is allocated by PyTorch, and 422.32 MiB is reserved by PyTorch but unallocated. If reserved but unallocated 
memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation.  See documentation for Memory Management  (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)

batch_size由 50 改为 10 即可。

7、import torchtext报错:OSError: [WinError 127] 找不到指定的程序。

完整错误:

Traceback (most recent call last):
  File "d:\Python\Python312\Lib\runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\launcher/../..\debugpy\__main__.py", line 71, in <module>
    cli.main()
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 501, in main
    run()
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 351, in run_file
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 310, in run_path
    return _run_module_code(code, init_globals, run_name, pkg_name=pkg_name, script_name=fname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 127, in _run_module_code
    _run_code(code, mod_globals, init_globals, mod_name, mod_spec, pkg_name, script_name)
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 118, in _run_code
    exec(code, run_globals)
  File "d:\Project\LearnPyTorch\misc\versions.py", line 3, in <module>
  File "d:\Python\Python312\Lib\site-packages\torchtext\__init__.py", line 18, in <module>
    from torchtext import _extension  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\torchtext\_extension.py", line 64, in <module>
    _init_extension()
  File "d:\Python\Python312\Lib\site-packages\torchtext\_extension.py", line 58, in _init_extension
    _load_lib("libtorchtext")
  File "d:\Python\Python312\Lib\site-packages\torchtext\_extension.py", line 50, in _load_lib
    torch.ops.load_library(path)
  File "d:\Python\Python312\Lib\site-packages\torch\_ops.py", line 1350, in load_library
    ctypes.CDLL(path)
  File "d:\Python\Python312\Lib\ctypes\__init__.py", line 379, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [WinError 127] 找不到指定的程序。

torchtext版本比较高,查看升级指南:text/examples/legacy_tutorial/migration_tutorial.ipynb at master · pytorch/text

8、使用torchtext报错:Can't find model 'en_core_web_sm'. It doesn't seem to be a Python package or a valid path to a data directory.

完整错误:

Traceback (most recent call last):
  File "d:\Python\Python312\Lib\runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\launcher/../..\debugpy\__main__.py", line 71, in <module>
    cli.main()
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 501, in main
    run()
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 351, in run_file
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 310, in run_path
    return _run_module_code(code, init_globals, run_name, pkg_name=pkg_name, script_name=fname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 127, in _run_module_code
    _run_code(code, mod_globals, init_globals, mod_name, mod_spec, pkg_name, script_name)
  File "c:\Users\liteng\.vscode\extensions\ms-python.debugpy-2025.0.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 118, in _run_code
    exec(code, run_globals)
  File "d:\Project\LearnPyTorch\imdb\lstm.py", line 12, in <module>
    TEXT = data.Field(
           ^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\torchtext\data\field.py", line 163, in __init__
    self.tokenize = get_tokenizer(tokenize, tokenizer_language)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\torchtext\data\utils.py", line 114, in get_tokenizer
    spacy = spacy.load(language)
            ^^^^^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\spacy\__init__.py", line 51, in load
    return util.load_model(
           ^^^^^^^^^^^^^^^^
  File "d:\Python\Python312\Lib\site-packages\spacy\util.py", line 472, in load_model
    raise IOError(Errors.E050.format(name=name))
OSError: [E050] Can't find model 'en_core_web_sm'. It doesn't seem to be a Python package or a valid path to a data directory.

spaCy没有正确安装,执行以下代码即可:

pip install spacy
python -m spacy download en_core_web_sm

9、Assertion `t >= 0 && t < n_classes` failed.

完整错误:

C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\aten\src\ATen\native\cuda\Loss.cu:250: block: [0,0,0], thread: [31,0,0] Assertion `t >= 0 && t < n_classes` failed.

当使用CrossEntryLoss时,模型最后的线性层out_features应该等于标签数量。CrossEntryLoss要求标签是从0开始的整数,例如:0,1,2,3。

新版PyTorch IMDB数据集返回的标签是1和2,检查标签是否忘记减1。

10、RuntimeError: Couldn't find appropriate backend to handle uri data\waves_yesno\0_0_0_0_1_1_1_1.wav and format None.

完整错误

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.2\plugins\python-ce\helpers\pydev\pydevd.py", line 1570, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.3.2\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "D:\Project\LearnPyTorch\torchaudio\yesno.py", line 7, in <module>
    waveform, sample_rate, labels = yesno_data[0]
                                    ~~~~~~~~~~^^^
  File "D:\Python\Python312\Lib\site-packages\torchaudio\datasets\yesno.py", line 85, in __getitem__
    item = self._load_item(fileid, self._path)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\Python312\Lib\site-packages\torchaudio\datasets\yesno.py", line 65, in _load_item
    waveform, sample_rate = torchaudio.load(file_audio)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\Python312\Lib\site-packages\torchaudio\_backend\utils.py", line 204, in load
    backend = dispatcher(uri, format, backend)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\Python312\Lib\site-packages\torchaudio\_backend\utils.py", line 116, in dispatcher
    raise RuntimeError(f"Couldn't find appropriate backend to handle uri {uri} and format {format}.")
RuntimeError: Couldn't find appropriate backend to handle uri data\waves_yesno\0_0_0_0_1_1_1_1.wav and format None.

解决方法:(1)安装soundfile。

pip install soundfile

(2)在代码中设置后端。

import torchaudio

torchaudio.set_audio_backend('soundfile')