最近在学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