用户警告:无法初始化 NumPy:针对 API 版本 0xf 编译的模块,但此版本的 numpy 为 0xe(在内部触发

UserWarning: Failed to initialize NumPy: module compiled against API version 0xf but this version of numpy is 0xe (Triggered internally at

(my2022) C:\Users\donhu>pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
Looking in indexes: https://pypi.org/simple, https://download.pytorch.org/whl/cu113
Requirement already satisfied: torch in d:\programdata\anaconda3\envs\my2022\lib\site-packages (1.10.2)
Collecting torchvision
  Downloading https://download.pytorch.org/whl/cu113/torchvision-0.12.0%2Bcu113-cp310-cp310-win_amd64.whl (5.4 MB)
     |████████████████████████████████| 5.4 MB 1.1 MB/s
Collecting torchaudio
  Downloading https://download.pytorch.org/whl/cu113/torchaudio-0.11.0%2Bcu113-cp310-cp310-win_amd64.whl (573 kB)
     |████████████████████████████████| 573 kB 6.4 MB/s
Requirement already satisfied: typing_extensions in d:\programdata\anaconda3\envs\my2022\lib\site-packages (from torch) (4.1.1)
Collecting torch
  Downloading https://download.pytorch.org/whl/cu113/torch-1.11.0%2Bcu113-cp310-cp310-win_amd64.whl (2186.0 MB)
     |████████████████████████████████| 2186.0 MB 4.7 kB/s
Requirement already satisfied: numpy in d:\programdata\anaconda3\envs\my2022\lib\site-packages (from torchvision) (1.21.5)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in d:\programdata\anaconda3\envs\my2022\lib\site-packages (from torchvision) (9.0.1)
Requirement already satisfied: requests in d:\programdata\anaconda3\envs\my2022\lib\site-packages (from torchvision) (2.27.1)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in d:\programdata\anaconda3\envs\my2022\lib\site-packages (from requests->torchvision) (1.26.8)
Requirement already satisfied: certifi>=2017.4.17 in d:\programdata\anaconda3\envs\my2022\lib\site-packages (from requests->torchvision) (2021.5.30)
Requirement already satisfied: idna<4,>=2.5 in d:\programdata\anaconda3\envs\my2022\lib\site-packages (from requests->torchvision) (3.3)
Requirement already satisfied: charset-normalizer~=2.0.0 in d:\programdata\anaconda3\envs\my2022\lib\site-packages (from requests->torchvision) (2.0.4)
Installing collected packages: torch, torchvision, torchaudio
  Attempting uninstall: torch
    Found existing installation: torch 1.10.2
    Uninstalling torch-1.10.2:
      Successfully uninstalled torch-1.10.2
Successfully installed torch-1.11.0+cu113 torchaudio-0.11.0+cu113 torchvision-0.12.0+cu113

(my2022) C:\Users\donhu>python
Python 3.10.4 | packaged by conda-forge | (main, Mar 30 2022, 08:38:02) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
D:\ProgramData\Anaconda3\envs\my2022\lib\site-packages\torch\_masked\__init__.py:223: UserWarning: Failed to initialize NumPy: module compiled against API version 0xf but this version of numpy is 0xe (Triggered internally at  ..\torch\csrc\utils\tensor_numpy.cpp:68.)
  example_input = torch.tensor([[-3, -2, -1], [0, 1, 2]])
>>> torch.FloatTensor([31, 1, 1989], [26, 8, 1987])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: new() received an invalid combination of arguments - got (list, list), but expected one of:
 * (*, torch.device device)
      didn't match because some of the arguments have invalid types: (list, list)
 * (torch.Storage storage)
 * (Tensor other)
 * (tuple of ints size, *, torch.device device)
 * (object data, *, torch.device device)

>>> torch.FloatTensor([[31, 1, 1989], [26, 8, 1987]])
tensor([[3.1000e+01, 1.0000e+00, 1.9890e+03],
        [2.6000e+01, 8.0000e+00, 1.9870e+03]])
>>> import torchtorch.FloatTensor([[31, 1, 1989], [26, 8, 1987]])
  File "<stdin>", line 1
    import torchtorch.FloatTensor([[31, 1, 1989], [26, 8, 1987]])
                                 ^
SyntaxError: invalid syntax
>>> import torchtorch.FloatTensor([[31, 1, 1989], [26, 8, 1987]])
  File "<stdin>", line 1
    import torchtorch.FloatTensor([[31, 1, 1989], [26, 8, 1987]])
                                 ^
SyntaxError: invalid syntax
>>> import torch
>>> torch.FloatTensor([[31, 1, 1989], [26, 8, 1987]])
tensor([[3.1000e+01, 1.0000e+00, 1.9890e+03],
        [2.6000e+01, 8.0000e+00, 1.9870e+03]])
>>> jupyter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'jupyter' is not defined
>>> notebook
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'notebook' is not defined
>>> jupyter-notebook
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'jupyter' is not defined
>>> exit()

(my2022) C:\Users\donhu>python -m notebook
[I 15:14:15.768 NotebookApp] The port 8888 is already in use, trying another port.
[I 15:14:15.769 NotebookApp] Serving notebooks from local directory: C:\Users\donhu
[I 15:14:15.770 NotebookApp] Jupyter Notebook 6.4.8 is running at:
[I 15:14:15.770 NotebookApp] http://localhost:8889/?token=a354cd600920030068b4020dfc955b40d721d0da3e749421
[I 15:14:15.770 NotebookApp]  or http://127.0.0.1:8889/?token=a354cd600920030068b4020dfc955b40d721d0da3e749421
[I 15:14:15.770 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 15:14:15.828 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///C:/Users/donhu/AppData/Roaming/jupyter/runtime/nbserver-14140-open.html
    Or copy and paste one of these URLs:
        http://localhost:8889/?token=a354cd600920030068b4020dfc955b40d721d0da3e749421
     or http://127.0.0.1:8889/?token=a354cd600920030068b4020dfc955b40d721d0da3e749421
[I 15:15:03.168 NotebookApp] 302 GET /?token=a354cd600920030068b4020dfc955b40d721d0da3e749421 (::1) 1.000000ms
[W 15:15:09.559 NotebookApp] Notebook Untitled.ipynb is not trusted
[I 15:15:09.849 NotebookApp] Kernel started: d123ea3f-ccd3-4d2c-8bb0-2265c8610c76, name: python3
[I 15:15:17.878 NotebookApp] Starting buffering for d123ea3f-ccd3-4d2c-8bb0-2265c8610c76:c8907cbab1e34c409557d30a3227e5a6
[W 15:15:20.088 NotebookApp] Notebook Untitled.ipynb is not trusted
[I 15:17:20.272 NotebookApp] Saving file at /Untitled.ipynb
[W 15:17:20.272 NotebookApp] Notebook Untitled.ipynb is not trusted

运行

import torch
print(torch.__version__)

错误

1.11.0+cu113
D:\ProgramData\Anaconda3\envs\my2022\lib\site-packages\torch\_masked\__init__.py:223: UserWarning: Failed to initialize NumPy: module compiled against API version 0xf but this version of numpy is 0xe (Triggered internally at  ..\torch\csrc\utils\tensor_numpy.cpp:68.)
  example_input = torch.tensor([[-3, -2, -1], [0, 1, 2]])

如何解决?

问题由

引起
  • 使用最新版本的 PyTorch(太新)
  • 使用最新版本的 conda(不支持 PyTorch 1.11.0 需要的 NumPy),然后从 conda 的虚拟环境中 运行 Python。

解决方案:

  • https://www.python.org/
  • 下载 Python 3.10.4
  • 像问题中的命令一样安装PyTorch,instlal Jupyter notebook。
  • 没用conda,Anaconda Navigator。

一切都会好起来的。

NumPy 版本似乎导致了这个问题;因此,升级 NumPy 可以解决这个问题。更具体地说,您应该将 NumPy 从 1.21.X 升级到 1.22.X.

示例:

pip install numpy --upgrade