Ubuntu18.04安装CUDA10.0及TensorFlow-gpu

显卡驱动

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

1
sudo apt install nvidia-driver-430

NVIDIA设置

安装

官网有安装所需软件要求

安装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)

运行结果

# ubuntu
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×