解决:A module that was compiled using NumPy 1.x cannot be run in NumPy 2.0.2 as it may crash.
·
问题:NumPy 版本不兼容
在import torch发生以下报错

A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.0.2 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.
If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.
Traceback (most recent call last): File "<stdin>", line 1, in <module>
File "D:\Anaconda\anaconda3\envs\d2l\lib\site-packages\torch\__init__.py", line 1471, in <module>
from .functional import * # noqa: F403
File "D:\Anaconda\anaconda3\envs\d2l\lib\site-packages\torch\functional.py", line 9, in <module>
import torch.nn.functional as F
File "D:\Anaconda\anaconda3\envs\d2l\lib\site-packages\torch\nn\__init__.py", line 1, in <module>
from .modules import * # noqa: F403
File "D:\Anaconda\anaconda3\envs\d2l\lib\site-packages\torch\nn\modules\__init__.py", line 35, in <module>
from .transformer import TransformerEncoder, TransformerDecoder, \
File "D:\Anaconda\anaconda3\envs\d2l\lib\site-packages\torch\nn\modules\transformer.py", line 20, in <module>
device: torch.device = torch.device(torch._C._get_default_device()), # torch.device('cpu'),
D:\Anaconda\anaconda3\envs\d2l\lib\site-packages\torch\nn\modules\transformer.py:20: UserWarning: Failed to initialize NumPy: _ARRAY_API not found (Triggered internally at C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\torch\csrc\utils\tensor_numpy.cpp:84.)
device: torch.device = torch.device(torch._C._get_default_device()), # torch.device('cpu'),
NumPy 版本不兼容:当前的 NumPy 版本是 2.0.2,而一些使用 NumPy 1.x 编译的模块无法在 NumPy 2.x 上正常运行。
解决:降低numpy的版本
- python版本为:3.9.20
- pytorch版本:2.2.0+cu121
安装低版本的numpy
pip install numpy==1.25.0
可以正常导入torch

更多推荐
所有评论(0)