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 复制代码