Tensorflow 是Google基於DistBelief進行研發的第二代人工智能學習系統,是採用數據流圖(data flow graphs),用於數值計算的開源軟件庫。架構靈活,能夠部署於一塊或多塊CPU、GPU卡的各類平臺。最初用由Google大腦小組開發出來,用於機器學習和深度神經網絡方面的研究,系統的通用性使其能夠普遍用於其餘計算領域,是目前使用最廣的深度學習框架之一。node
本教程主要介紹TensorFlow 在Centos 7上的安裝和使用,包括安裝過程,基本使用和簡單示例運行。python
根據GPU型號從相應網站下載驅動,例如使用NVIDIA Tesla M60,從NVIDIA網站選擇對應的型號和操做系統,CUDA Toolkit版本,下載驅動文件,如NVIDIA-Linux-x86_64-375.66.run,運行驅動文件,根據提示安裝:linux
sh NVIDIA-Linux-x86_64-375.66.run
複製代碼
安裝完成後能夠經過NVIDIA命令工具nvidia-smi查看GPU狀況:git
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 |
+-----------------------------------------------------------------------------+:
複製代碼
從NVIDIA網站選擇最新的驅動版本,選擇Linux,x86_64,CentOS 7,下載rpm(local)驅動文件,並安裝github
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
複製代碼
設置環境變量bash
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/
複製代碼
從NVIDIA網站下載cuDNN 安裝包,根據GPU及CUDA版本選擇對應cuDNN版本,下載cuDNN v5.1 for CUDA8.0,解壓拷貝到CUDA安裝目錄網絡
cp include/* /usr/local/cuda/include
cp lib64/* /usr/local/cuda/lib64
複製代碼
安裝命令以下:架構
sudo yum install python-pip python-wheel
sudo pip install --upgrade pip
複製代碼
能夠選擇不一樣的環境安裝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 命令安裝。python2.7
pip install tensorflow_gpu-1.1.0-cp27-none-linux_x86_64.whl
複製代碼
運行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! 複製代碼
運行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 複製代碼