在CentOS 7上安裝Tensorflow

美團雲發表於2017-11-17

簡介

Tensorflow 是Google基於DistBelief進行研發的第二代人工智慧學習系統,是採用資料流圖(data flow graphs),用於數值計算的開源軟體庫。架構靈活,可以部署於一塊或多塊CPU、GPU卡的各種平臺。最初用由Google大腦小組開發出來,用於機器學習和深度神經網路方面的研究,系統的通用性使其可以廣泛用於其他計算領域,是目前使用最廣的深度學習框架之一。

本教程主要介紹TensorFlow 在Centos 7上的安裝和使用,包括安裝過程,基本使用和簡單示例執行。

GPU驅動安裝

根據GPU型號從相應網站下載驅動,例如使用NVIDIA Tesla M60,從NVIDIA網站選擇對應的型號和作業系統,CUDA Toolkit版本,下載驅動檔案,如NVIDIA-Linux-x86_64-375.66.run,執行驅動檔案,根據提示安裝:

sh  NVIDIA-Linux-x86_64-375.66.run
複製程式碼

安裝完成後可以通過NVIDIA命令工具nvidia-smi檢視GPU情況:

nvidia-smi
Wed Jun 28 11:39:28 2017
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 375.39                 Driver Version: 375.39                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla M60           Off  | 0000:00:02.0     Off |                  Off |
| N/A   36C    P0    38W / 150W |      0MiB /  8123MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID  Type  Process name                               Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+:
複製程式碼

TensorFlow 安裝過程

1.安裝CUDA

NVIDIA網站選擇最新的驅動版本,選擇Linux,x86_64,CentOS 7,下載rpm(local)驅動檔案,並安裝

sudo rpm -i cuda-repo-rhel7-8-0-local-ga2-8.0.61-1.x86_64.rpm
sudo yum clean all
sudo yum install cuda
複製程式碼

設定環境變數

export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:/usr/local/cuda-8.0/extras/CUPTI/lib64:$LD_LIBRARY_PATH
export CUDA_HOME=/usr/local/cuda-8.0/
複製程式碼

2.安裝cuDNN

NVIDIA網站下載cuDNN 安裝包,根據GPU及CUDA版本選擇對應cuDNN版本,下載cuDNN v5.1 for CUDA8.0,解壓拷貝到CUDA安裝目錄

cp include/* /usr/local/cuda/include
cp lib64/* /usr/local/cuda/lib64
複製程式碼

3.安裝Python 環境

安裝命令如下:

sudo yum install python-pip python-wheel
sudo pip install --upgrade pip
複製程式碼

4.安裝Tensorflow

可以選擇不同的環境安裝Tensorflow,如:virtualenv,pip, Docker,Anaconda。如需要使TensorFlow 成為系統多使用者可直接使用的服務,推薦通過原生pip命令安裝。 安裝命令如下:

pip install tensorflow #python2.7 CPU版本
pip install tensorflow-gpu #python2.7 GPU版本
複製程式碼

pip 命令安裝失敗時,可通過Tensorflow 網站選擇下載whl 檔案安裝,不同版本whl檔案地址。 下載到本地後通過pip 命令安裝。

pip install tensorflow_gpu-1.1.0-cp27-none-linux_x86_64.whl
複製程式碼

TensorFlow 使用示例

1.TensorFlow模組使用

執行Python SHELL,import TensorFlow模組,進行簡單的功能驗證:

python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
2017-06-28 16:42:53.518877: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-28 16:42:53.518906: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-28 16:42:53.518914: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-06-28 16:42:53.518921: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-28 16:42:53.518929: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2017-06-28 16:42:54.099744: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:901] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2017-06-28 16:42:54.100218: I tensorflow/core/common_runtime/gpu/gpu_device.cc:887] Found device 0 with properties:
name: Tesla M60
major: 5 minor: 2 memoryClockRate (GHz) 1.1775
pciBusID 0000:00:02.0
Total memory: 7.93GiB
Free memory: 7.86GiB
2017-06-28 16:42:54.100243: I tensorflow/core/common_runtime/gpu/gpu_device.cc:908] DMA: 0
2017-06-28 16:42:54.100251: I tensorflow/core/common_runtime/gpu/gpu_device.cc:918] 0:   Y
2017-06-28 16:42:54.100266: I tensorflow/core/common_runtime/gpu/gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: Tesla M60, pci bus id: 0000:00:02.0)
>>> print(sess.run(hello))
Hello, TensorFlow!
複製程式碼

2.MNIST例子

執行Tensorflow tutorial 中MNIST例子,下載程式碼到本地並執行。
訓練輸出示例如下:

python mnist_softmax.py
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting /tmp/tensorflow/mnist/input_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting /tmp/tensorflow/mnist/input_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting /tmp/tensorflow/mnist/input_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting /tmp/tensorflow/mnist/input_data/t10k-labels-idx1-ubyte.gz
2017-06-28 16:13:17.165681: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-28 16:13:17.165727: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-28 16:13:17.165736: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-06-28 16:13:17.165744: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-28 16:13:17.165751: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2017-06-28 16:13:17.742066: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:901] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2017-06-28 16:13:17.742503: I tensorflow/core/common_runtime/gpu/gpu_device.cc:887] Found device 0 with properties:
name: Tesla M60
major: 5 minor: 2 memoryClockRate (GHz) 1.1775
pciBusID 0000:00:02.0
Total memory: 7.93GiB
Free memory: 7.86GiB
2017-06-28 16:13:17.742529: I tensorflow/core/common_runtime/gpu/gpu_device.cc:908] DMA: 0
2017-06-28 16:13:17.742538: I tensorflow/core/common_runtime/gpu/gpu_device.cc:918] 0:   Y
2017-06-28 16:13:17.742553: I tensorflow/core/common_runtime/gpu/gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: Tesla M60, pci bus id: 0000:00:02.0)
0.9176
複製程式碼

參考資料


相關文章