項目場景
最近在學pytorch
,先用官網提供的conda
命令安裝了一下:
conda install pytorch torchvision cudatoolkit=10.2 -c pytorch
然后按照官網的方法測試是否安裝成功,能不能正常使用:
import torch
x = torch.rand(5, 3)
print(x)
若能正常打印出x
的值,則說明可以正常使用,否則反之。
問題描述
代碼運行報錯:ImportError: numpy.core.multiarray failed to import
D:\miniconda3\envs\pytorch\lib\site-packages\numpy\__init__.py:138: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL
initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not
from . import _distributor_init
Traceback (most recent call last):
File "c:/Users/ghgxj/Desktop/pytorch/1_get_started.py", line 1, in <module>
import torch
File "D:\miniconda3\envs\pytorch\lib\site-packages\torch\__init__.py", line 189, in <module>
from torch._C import *
ImportError: numpy.core.multiarray failed to import
原因分析
python
環境自帶一個numpy
,會與安裝的pytorch
帶的numpy
版本間發生沖突。
解決方案
- 先卸載
numpy
conda uninstall numpy
- 再重裝
numpy
conda install numpy
最終結果
按照上述方案處理后正常打印出了x
的值:
tensor([[0.8338, 0.1541, 0.0379],
[0.4348, 0.0145, 0.3586],
[0.4098, 0.2363, 0.5405],
[0.7372, 0.7418, 0.3703],
[0.5668, 0.9512, 0.8041]])
我們再來看看GPU
驅動和CUDA
是否能用:
import torch
print(torch.cuda.is_available())
控制台打印顯示True
,則說明能正常使用。
引用參考
https://pytorch.org/get-started/locally/
https://blog.csdn.net/wgx571859177/article/details/78273764