前言
配置Ubuntu18.04 CUDA10.1 gcc4.8 pytorch
本文参考Ubuntu18.04下安装深度学习框架Pytorch(GPU加速)
一、安装显卡驱动
1、查看自己的系统类型和架构
uname -a
输出显示
Linux jack-System-Product-Name 5.0.0-32-generic #34~18.04.2-Ubuntu SMP Thu Oct 10 10:36:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
系统18.04.2-Ubuntu
##################
架构
x86_64
2、查看电脑显卡型号
ubuntu-drivers devices
输出显示
== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd00001C03sv000010DEsd000011D7bc03sc00i00
vendor : NVIDIA Corporation
model : GP106 [GeForce GTX 1060 6GB]
driver : nvidia-driver-440-server - distro non-free recommended
driver : nvidia-driver-440 - distro non-free
driver : nvidia-driver-435 - 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
可以看到我的图形卡类型是GTX 1060
按照图中non-free recommended表示推荐驱动,所以安装驱动方法如下
sudo apt install nvidia-driver-440-server
最后输入下面命令看是否安装成功
sudo nvidia-smi
输出
二、安装Anaconda
mkdir /Anaconda
cd /Anaconda
wget https://repo.continuum.io/archive/Anaconda3-4.4.0-Linux-x86_64.sh
bash Anaconda3-4.4.0-Linux-x86_64.sh
添加环境变量
su root
gedit ~/.bashrc
#在文件最后面添加下面命令
export PATH=$PATH:/home/jack/anaconda3/bin
#最后执行下面这句命令
source ~/.bashrc
在终端上输入python
出现python3.7表示安装成功
创建虚拟环境
参考Anaconda创建、激活、退出、删除虚拟环境
conda create -n env_jack python=3.7
三、安装pytorch
conda install pytorch==版本号
###conda会自动帮你匹配cudnn和cuda
四、测试GPU是否成功加速
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加速,否则没有成功调用
tensor([[0.5016, 1.0193, 0.9459],
[1.1659, 0.8567, 1.3380],
[0.7841, 1.0144, 0.6522],
[0.4233, 1.1272, 1.4869],
[0.5090, 0.0481, 1.3041]], device='cuda:0')