在Ubuntu上安裝TensorFlow-GPU開發環境


深度學習是一個比較復雜的體系,今天記錄一下開發環境的搭建步驟。
全新安裝Ubuntu 20.10,系統默認安裝的是python3,查看python的版本;

mango@ubuntu:~$ python3 --version
Python 3.8.6

接下來我們安裝pip3


mango@ubuntu:~$ sudo apt-get update
mango@ubuntu:~$ sudo apt-get upgrade
mango@ubuntu:~$ sudo apt install python3-pip

檢測安裝的pip版本信息

mango@ubuntu:~$ pip --version
pip 20.1.1 from /usr/lib/python3/dist-packages/pip (python 3.8)

安裝OpenBLAS

mango@ubuntu:~$ sudo apt install build-essential cmake git unzip \pkg-config libopenblas-dev liblapack-dev

安裝Numpy、SciPy和Matplotlib,方便進行機器學習或科學計算

mango@ubuntu:~$ sudo apt install python3-numpy python3-scipy python3-matplotlib python3-yaml

安裝HDF5,用來高效的二進制格式來保存數值數據的大文件。它可以讓你將Keras模型快速高效地保存到磁盤。

mango@ubuntu:~$ sudo apt install libhdf5-serial-dev python3-h5py

安裝Graphviz和pydot-ng,這兩個包可以將Keras模型可視化

mango@ubuntu:~$ sudo apt install graphviz
mango@ubuntu:~$ sudo pip3 install pydot-ng

打開software&updates,安裝顯卡驅動

安裝重啟之后查看顯卡型號及支持的CUDA的版本

去https://developer.nvidia.com/zh-cn/cuda-downloads下載對應版本的CUDA工具包

按照如下的步驟進行安裝

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pinsudo 
mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.1.0/local_installers/cuda-repo-ubuntu2004-11-1-local_11.1.0-455.23.05-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2004-11-1-local_11.1.0-455.23.05-1_amd64.deb
sudo apt-key add /var/cuda-repo-ubuntu2004-11-1-local/7fa2af80.pub
sudo apt-get update
sudo apt-get -y install cuda

去https://developer.nvidia.com/zh-cn/cudnn下載對應版本的cuDNN,分別下載x86_64對應的運行時庫和開發人員庫的deb包

分別安裝運行時庫和開發人員庫

sudo dpkg -i libcudnn8_8.1.1.33-1+cuda11.2_amd64.deb
sudo dpkg -i libcudnn8-dev_8.1.1.33-1+cuda11.2_amd64.deb

由於Keras已經內置於tensorflow2中,我們直接安裝tensorflow即可

mango@ubuntu:~$ sudo pip3  install tensorflow-gpu -i https://mirrors.aliyun.com/pypi/simple

測試tensorflow2
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, verbose=2)
執行結果
2021-03-28 16:09:23.765265: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
2021-03-28 16:09:36.281358: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-03-28 16:09:36.302026: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcuda.so.1
2021-03-28 16:09:36.548766: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:941] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-03-28 16:09:36.565114: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 965M computeCapability: 5.2
coreClock: 1.15GHz coreCount: 8 deviceMemorySize: 1.96GiB deviceMemoryBandwidth: 74.65GiB/s
2021-03-28 16:09:36.565233: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
2021-03-28 16:09:36.821405: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcublas.so.11
2021-03-28 16:09:36.821485: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcublasLt.so.11
2021-03-28 16:09:36.903521: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcufft.so.10
2021-03-28 16:09:36.963249: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcurand.so.10
2021-03-28 16:09:36.963863: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'libcusolver.so.10'; dlerror: libcusolver.so.10: cannot open shared object file: No such file or directory
2021-03-28 16:09:37.022882: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcusparse.so.11
2021-03-28 16:09:37.038066: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudnn.so.8
2021-03-28 16:09:37.038165: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1757] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2021-03-28 16:09:37.038929: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-03-28 16:09:37.040468: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-03-28 16:09:37.040562: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1261] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-03-28 16:09:37.040594: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1267]
2021-03-28 16:09:37.934591: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
2021-03-28 16:09:38.020144: I tensorflow/core/platform/profile_utils/cpu_utils.cc:112] CPU Frequency: 2599990000 Hz
Epoch 1/5
1875/1875 [] - 3s 924us/step - loss: 0.4852 - accuracy: 0.8609
Epoch 2/5
1875/1875 [
] - 2s 890us/step - loss: 0.1477 - accuracy: 0.9570
Epoch 3/5
1875/1875 [] - 2s 865us/step - loss: 0.1033 - accuracy: 0.9681
Epoch 4/5
1875/1875 [
] - 2s 862us/step - loss: 0.0865 - accuracy: 0.9726
Epoch 5/5
1875/1875 [==============================] - 2s 845us/step - loss: 0.0754 - accuracy: 0.9763
313/313 - 0s - loss: 0.0753 - accuracy: 0.9764


免責聲明!

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



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