Ubuntu16.04 + NVidia显卡 + Anaconda3 + Tensorflow-GPU 安装、配置、测试 (无需手动安装CUDA)
Step.1移除旧驱动:
sudo apt-get purge nvidia*
部分 Debian 系发行版版本可能不支持 apt-get purge ,可尝试使用 apt-get remove –purge 或 apt-get autoremove –purge 替代
Step.2 添加显卡驱动的PPA:
# Add the graphics-driver PPA
sudo add-apt-repository ppa:graphics-drivers
# And update
sudo apt-get update
Step.3 安装(启用)Nvidia 显卡驱动
sudo apt-get install nvidia-396(驱动最好用最新驱动但要和CUDA版本对上,不然后面老爱出现问题)
Step.4 重启电脑以使新驱动生效
你可以确认安装后的驱动状态是否正常:
lsmod | grep nvidia
如果没有输出,说明安装有问题。也可能是因为该驱动在你的系统驱动数据库中未被启用。你可以运行下面的命令检验开源驱动 nouveau 是否在运行。如果没有显示,说明你的安装是没问题的
Step.5 避免自动升级以防破坏驱动
有两种方法可以避免自动升级:
a. 从软件源里移除显卡驱动 PPA 。不同发行版这步的操作有所不同。在 Ubuntu 里,找到你的软件源配置,移除所有显卡驱动 PPA 。
b. 或者通过禁用次要更新。输入一下命令即可:
sudo apt-mark hold nvidia-396
Step.6 移除
如果新驱动运行出问题了,你可以很容易地移除它:
a. 按上面的讲述方法移除显卡驱动 PPA 。
b. 输入一下命令完全移除驱动
sudo apt-get purge nvidia*
c. 重启电脑以使开源驱动 nouveau 生效。
2-安装Anaconda+Tensorflow-GPU
安装 Anaconda
bash Anaconda3-5.3.0-Linux-x86_64.sh # make sure append the Anaconda executable directory to your PATH environment variable in .bashrc
source ~/.bashrc
python --version # to show the python version
anaconda的源改为国内镜像, 配置文件是~/.condarc
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源改为国内镜像, 配置文件是~/.pip/pip.conf, 该后的文件内容如下:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
[install]
trusted-host=https://pypi.tuna.tsinghua.edu.cn
update conda(最好不要升级)
conda update conda -y
conda update anaconda -y
conda update python -y
conda update --all -y
安装tensorflow
conda create --name tf-gpu # Create a Python "virtual environment" for TensorFlow using conda
source activate tf-gpu # 注意运行此命令后,命令行开头的提示变为(tf-gpu) [email protected]:~$,表示tf-gpu环境处于**状态
# 后面的命令,都在tf-gpu环境下执行,我保留了命令行的提示,以示区别
(tf-gpu) [email protected]:~$ conda install tensorflow-gpu -y # install TensorFlow with GPU acceleration and all of the dependencies.
为Tensorflow环境创建Jupyter Notebook Kernel
(tf-gpu) [email protected]:~$ conda install ipykernel -y
(tf-gpu) [email protected]:~$ python -m ipykernel install --user --name tf-gpu --display-name "TensorFlow-GPU"
安装keras
(tf-gpu) [email protected]:~$ conda install keras -y
3-测试安装结果
用Keras 例程(Keras内部会用到Tensorflow)
打开Jupyter Notebook
jupyter notebook
创建新笔记: New下拉菜单 -> 选择TensorFlow-GPU
输入如下测试代码,并运行:
# Import dependencies
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.layers import Flatten, MaxPooling2D, Conv2D
from keras.callbacks import TensorBoard
# Load and process the MNIST data
# 推荐先下载mnist.npz到目录~/.keras/datasets/
(X_train,y_train), (X_test, y_test) = mnist.load_data(path="mnist.npz")
X_train = X_train.reshape(60000,28,28,1).astype('float32')
X_test = X_test.reshape(10000,28,28,1).astype('float32')
X_train /= 255
X_test /= 255
n_classes = 10
y_train = keras.utils.to_categorical(y_train, n_classes)
y_test = keras.utils.to_categorical(y_test, n_classes)
# Create the LeNet-5 neural network architecture
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=(28,28,1)) )
model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(n_classes, activation='softmax')) # Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # Set log data to feed to TensorBoard for visual analysis
tensor_board = TensorBoard('./logs/LeNet-MNIST-1') # Train the model
model.fit(X_train, y_train, batch_size=128, epochs=15, verbose=1,
validation_data=(X_test,y_test), callbacks=[tensor_board])
运行完后查看误差曲线
(tf-gpu) [email protected]:~$ tensorboard --logdir=./logs --port 6006
效果如下图