在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

posted @ 2021-03-28 17:07  无风听海  阅读(1499)  评论(0编辑  收藏  举报