Ubuntu安装tf-gpu

显卡驱动

最新的 18.04.3 已经可以安装 430 驱动

1
sudo apt install nvidia-driver-430

安装要求

官网有安装所需软件要求

安装 cuda 及其组件

去官网下载cuda 安装 runfile 及其补丁

1
2
3
4
# Add NVIDIA package repository
chmod +x ./cuda_10.0.130_410.48_linux.run ./cuda_10.0.130.1_linux.run
sudo ./cuda_10.0.130_410.48_linux.run
sudo ./cuda_10.0.130.1_linux.run

注意不要重复安装 nvidia 显卡驱动。
然后下载cudnn.

1
2
3
4
tar -zxvf cudnn-10.0-linux-x64-v7.6.2.24.tgz
sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

然后设置环境变量(cuda 安装完成时会提示)

1
2
export PATH=/usr/local/cuda/bin/:$PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/extras/CUPTI/lib64

检验下安装

1
2
3
4
5
wmc@omen:~$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:01_CDT_2018
Cuda compilation tools, release 10.0, V10.0.130

Anaconda

Anaconda 安装十分简单.去喜闻乐见的tuna下载。

1
2
chmod +x Anaconda3-2019.07-Linux-x86_64.sh
./Anaconda3-2019.07-Linux-x86_64.sh

更换 anaconda 和 pip 镜像源

1
2
3
4
5
6
7
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes


pip install pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

安装完成后,我们使用创建一个新的 python 虚拟环境,安装 tensorflow-gpu.

1
2
3
conda create -n tensor pip python=3.6
source activate tensor
pip install --upgrade tensorflow-gpu

安装 jupyter 插件

1
2
conda install -c conda-forge jupyter_contrib_nbextensions
jupyter contrib nbextension install --user

将 conda 虚拟环境作为 jupyter 内核

1
2
3
conda activate tensorflowenv
pip install ipykernel
python -m ipykernel install --user --name tensorflowenv --display-name "Python (tensorflowenv)"

示例测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)

运行结果

作者

Naruto210

发布于

2019-02-01

更新于

2021-04-29

许可协议

评论