解決pytorch報錯ImportError: numpy.core.multiarray failed to import


項目場景

最近在學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版本間發生沖突。

解決方案

  1. 先卸載numpy
conda uninstall numpy
  1. 再重裝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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM