安装 pytorch 环境
作者:解琛
时间:2020 年 8 月 20 日
一、安装 Annaconda
sh Anaconda3-5.3.1-Linux-x86_64.sh
二、安装 cuda 和 cudann
2.1 检测 NVIDIA 图形卡是否支持 GPU 运算
jerome@jerome:~/software/0.软件包$ ubuntu-drivers devices
== /sys/devices/pci0000:00/0000:00:1c.5/0000:05:00.0 ==
modalias : pci:v00008086d000008B3sv00008086sd00008070bc02sc80i00
vendor : Intel Corporation
model : Wireless 3160 (Dual Band Wireless AC 3160)
manual_install: True
driver : backport-iwlwifi-dkms - distro free
== /sys/devices/pci0000:00/0000:00:1c.4/0000:04:00.0 ==
modalias : pci:v000010DEd0000139Asv0000103Csd000080DDbc03sc02i00
vendor : NVIDIA Corporation
model : GM107M [GeForce GTX 950M]
driver : nvidia-driver-435 - distro non-free
driver : nvidia-driver-440-server - distro non-free recommended
driver : nvidia-driver-440 - distro non-free
driver : nvidia-driver-390 - distro non-free
driver : nvidia-driver-418-server - distro non-free
driver : xserver-xorg-video-nouveau - distro free builtin
我的显卡是:[GeForce GTX 950M],拿着显卡型号去 NAVIDIA官网 去查一下是否支持 GPU 运算。
2.2 安装推荐的显卡驱动程序
对照显卡对应版本的 关系表 ,进行安装。
GPU | Compute Capability |
---|---|
GeForce GTX 960 | 5.0 |
在 软件和更新 里面,选择专有驱动进行安装。安装好之后重启电脑,输入以下指令确认安装完成。
jerome@jerome:~$ sudo nvidia-smi
[sudo] jerome 的密码:
Tue Jul 21 00:05:12 2020
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.100 Driver Version: 440.100 CUDA Version: 10.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 950M Off | 00000000:04:00.0 Off | N/A |
| N/A 47C P5 N/A / N/A | 245MiB / 4046MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 3334 G /usr/lib/xorg/Xorg 85MiB |
| 0 3496 G /usr/bin/gnome-shell 98MiB |
| 0 3938 G ...AAAAAAAAAAAACAAAAAAAAAA= --shared-files 58MiB |
+-----------------------------------------------------------------------------+
我的 cuda 版本是 10.2,对照 关系表 确认各个软件的版本。
2.3 安装 cudann
sudo apt install nvidia-cuda-toolkit
2.4 安装 pyTorch
pip install torch torchvision
由于包体太大,这种方式安装容易断线,不知道后面会不会修复。
可以使用 conda 进行安装。
conda install pytorch torchvision cudatoolkit=10.2 -c pytorch
直接使用离线安装的方式,目前来看更加稳妥。官方提供了 下载选项。
git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
# if you are updating an existing checkout
git submodule sync
git submodule update --init --recursive
接着配置环境变量并开始安装。
echo 'export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}' >> ~/.bashrc
python3 setup.py install
三、 测试
import torch as t
x = t.rand(5,3)
y = t.rand(5,3)
if t.cuda.is_available():
x = x.cuda()
y = y.cuda()
print(x+y)
成功使用 gpu 运算,环境安装完毕。
0.0624 1.3100 1.0462
1.3511 1.1635 1.1907
0.8890 1.0524 1.0477
1.5921 1.0428 0.6046
0.3175 0.9421 1.4520
[torch.cuda.FloatTensor of size 5x3 (GPU 0)]