Tensorflow 是Google基于DistBelief进行研发的第二代人工智能学习系统,是采用数据流图(data flow graphs),用于数值计算的开源软件库。架构灵活,可以部署于一块或多块CPU、GPU卡的各种平台。最初用由Google大脑小组开发出来,用于机器学习和深度神经网络方面的研究,系统的通用性使其可以广泛用于其他计算领域,是目前使用最广的深度学习框架之一。
本教程主要介绍TensorFlow 在Centos 7上的安装和使用,包括安装过程,基本使用和简单示例运行。
根据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 |
- +-----------------------------------------------------------------------------+:
从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/
从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 命令安装。
- 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
来源: https://juejin.im/post/5a0d30df51882554b83719d1