Ubuntu18.04 + NVidia顯卡 + Anaconda3 + Tensorflow-GPU 安裝、配置、測試 (無需手動安裝CUDA)


其中其決定作用的是這篇文章  https://www.pugetsystems.com/labs/hpc/Install-TensorFlow-with-GPU-Support-the-Easy-Way-on-Ubuntu-18-04-without-installing-CUDA-1170/

注意兼容版本:https://devtalk.nvidia.com/default/topic/1047898/cuda-setup-and-installation/cuda-10-1-tensorflow-1-13/2

1-安裝顯卡驅動

在終端執行如下命令,建議先切換到國內源,如huaweicloud mirrors。

sudo apt purge nvidia*
ubuntu-drivers devices            # 可以看到顯卡等設備,和推薦的驅動
sudo ubuntu-drivers autoinstall   # 安裝推薦驅動,通常是最新版

如果通過ubuntu-drivers devices看不到NVidia顯卡,則添加

sudo add-apt-repository ppa:graphics-drivers
sudo apt-get update

安裝完后,重啟系統, 啟動后,在圖形界面運行Nvidia X Server Settings,可以看到顯卡情況,如下圖。

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 --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
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
conda activate tf-gpu       # 注意運行此命令后,命令行開頭的提示變為(tf-gpu) user@computer:~$,表示tf-gpu環境處於激活狀態
# 后面的命令,都在tf-gpu環境下執行,我保留了命令行的提示,以示區別
(tf-gpu) user@computer:~$ conda
install tensorflow-gpu -y # install TensorFlow with GPU acceleration and all of the dependencies.

為Tensorflow環境創建Jupyter Notebook Kernel

(tf-gpu) user@computer:~$ conda install ipykernel -y
(tf-gpu) user@computer:~$ conda install jupyter (tf-gpu) user@computer:~$ python -m ipykernel install --user --name tf-gpu --display-name "TensorFlow-GPU"

 安裝keras

(tf-gpu) user@computer:~$ 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) dbk@i9:~$ tensorboard --logdir=./logs --port 6006

 效果如下圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM