Tensorflow 介绍

tensorflow_icon
tensorflow_icon

TensorFlow™ 是一个开放源代码软件库,用于进行高性能数值计算。借助其灵活的架构,用户可以轻松地将计算工作部署到多种平台(CPU、GPU、TPU)和设备(桌面设备、服务器集群、移动设备、边缘设备等)。TensorFlow™ 最初是由 Google Brain 团队(隶属于 Google 的 AI 部门)中的研究人员和工程师开发的,可为机器学习和深度学习提供强力支持,并且其灵活的数值计算核心广泛应用于许多其他科学领域。

Tensorflow官网 https://tensorflow.google.cn/
注意 该页面部分内容需要科学上网
Tensorflow Github源代码托管仓库 https://github.com/tensorflow/tensorflow

Tensorflow 安装

python_tensorflow_install
python_tensorflow_install

以下教程使用Anaconda创建虚拟环境,为Tensorflor创建专属环境,将软件包安装与系统隔离开来。如图所示,上图Python3.6.2无Tensorflow模块,所以无法导入,下图Python3.6.2安装了Tensorflow,所以能成功编译代码

在进行接下来的操作前请先确认已配置完成Ananconda的系统环境变量,以管理员身份打开Anaconda Prompt,输入以下代码

1
conda create -n tensorflow_test pip python=3.6

创建一个新的虚拟环境,方法是选择 Python 解释器并创建一个 ./venv 目录来存放它,在当前Tensorflow支持Python3.4 3.5 3.6,我们指定为python3.6,另外项目名称请自由命名,我这里示范所以命名为tensorflow_test

install1
install1

Anaconda会对环境进行分析并下载安装相关文件,确认按y即可

注意 Anaconda默认源下载速度感人,请提前换源为清华大学或中国科学研究院源
1 添加清华源,命令行中直接使用以下命令

1
2
3
4
5
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/

conda config --set show_channel_urls yes

2 添加中科大源,命令行中直接使用以下命令

1
2
3
4
5
6
7
8
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/

conda config --set show_channel_urls yes

换源之后基本可达到满速下载,摆脱小水管,回归正题,安装完成之后输入进入环境

1
conda activate tensorflow_test
conda_activate
conda_activate

下一步开始正式安装tensorflow到你的本地,建议使用稳定版,新手可以使用tensorflow,但是仅支持CPU,tensorflow-gpu为支持GPU的版本,后续还需安装NVIDIA的开发套件,但是运行速度相比于CPU版本有大幅度提升,请自行选择

  • tensorflow - 仅支持 CPU 的当前版本(建议新手使用)
  • tensorflow-gpu - 支持 GPU 的当前版本(Ubuntu 和 Windows)
  • tf-nightly - 仅支持 CPU 的每夜版(不稳定)
  • tf-nightly-gpu - 支持 GPU 的每夜版(不稳定,Ubuntu 和 Windows)

在窗口中键入如下命令,后面加上你需要的版本

1
pip install --ignore-installed --upgrade packageURL

packageURL
请访问https://www.tensorflow.org/install/pip#package-location获得所需版本下载链接
如tensorflow-GPU版本安装为(2019/03/18)

1
pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-1.12.0-cp36-cp36m-win_amd64.whl

注意
PIP默认源下载速度感人,请提前修改配置文件,安装目录\pip\pip.ini,修改内容为

1
2
3
4
[global]
index-url = https://pypi.mirrors.ustc.edu.cn/simple/
[install]
trusted-host=pypi.mirrors.ustc.edu.cn

或者在pip命令后加上

1
-i https://pypi.mirrors.ustc.edu.cn/simple/ --trusted-host pypi.mirrors.ustc.edu.cn

警告 请务必使用管理员权限,否则安装过程可能会提示”Cache entry deserialization failed, entry ignored”

1
2
3
import tensorflow as tf
tf.enable_eager_execution()
print(tf.reduce_sum(tf.random_normal([1000, 1000])))

安装完毕之后,在安装了Tensorflow的环境中测试上述代码,若如图所示则未安装成功,若安装失败,请往下翻阅Tensorflow安装常见错误

test
test

GPU版本安装

GPU版本要求为CUDA® 计算能力为 3.5 或更高的 NVIDIA® GPU 卡,请参阅支持CUDA的GPU卡列表。

geforce_confirm
geforce_confirm

Tensorflow GPU版本需要以下软件支持

  • NVIDIA® GPU 驱动程序 - CUDA 9.0 需要 384.x 或更高版本。
  • CUDA® 工具包 - TensorFlow 支持 CUDA 9.0。
  • CUDA 工具包附带的 CUPTI。
  • cuDNN SDK(7.2 及更高版本)

驱动程序请通过GeForce Experience进行更新

CUDA下载网站https://developer.nvidia.com/cuda-toolkit-archive,当前不支持9.1 9.2 10.0,请严格按照要求下载CUDA9.0,下载Base Installer即可

cuda9.0
cuda9.0

解压完成之后会开始安装,选择精简即可,并且之后会自动安装Visual Studio支持

cuda_install
cuda_install

安装完成之后CMD输入命令,若如图所示极为安装成功,无需配置环境变量

1
nvcc --version
nvcc -v
nvcc -v

Cudnn下载地址https://developer.nvidia.com/rdp/cudnn-archive
首先需要你的NVIDIA账户申请开发者然后做一个调查问卷就可以开始下载,请注意版本号对应,选择for CUDA 9.0版本,下载后,解压文件到CUDA目录替换文件即可,默认目录C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0,运行测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
import tensorflow as tf
with tf.device('/cpu:0'):
a = tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')
b = tf.constant([1.0, 2.0, 3.0], shape=[3], name='b')
with tf.device('/gpu:1'):
c = a + b

# 注意:allow_soft_placement=True表明:计算设备可自行选择,如果没有这个参数,会报错。
# 因为不是所有的操作都可以被放在GPU上,如果强行将无法放在GPU上的操作指定到GPU上,将会报错。
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))
# sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
sess.run(tf.global_variables_initializer())
print(sess.run(c))

至此,GPU版本配置也已经完成,测试上述代码,若显示显卡相关信息,则说明安装配置成功

gpu_test
gpu_test

安装常见错误

常见报错导入tensorflow时Failed to load the native TensorFlow runtime
请按以下步骤依次排查

1.是否缺失Visual C ++ Redistributate 2015 x64
下载地址https://www.microsoft.com/en-us/download/details.aspx?id=48145

2.CUDA Cudnn是否匹配、成功安装,请运行以下Python脚本进行检查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""A script for testing that TensorFlow is installed correctly on Windows.
The script will attempt to verify your TensorFlow installation, and print
suggestions for how to fix your installation.
"""

import ctypes
import imp
import sys

def main():
try:
import tensorflow as tf
print("TensorFlow successfully installed.")
if tf.test.is_built_with_cuda():
print("The installed version of TensorFlow includes GPU support.")
else:
print("The installed version of TensorFlow does not include GPU support.")
sys.exit(0)
except ImportError:
print("ERROR: Failed to import the TensorFlow module.")

print("""
WARNING! This script is no longer maintained!
=============================================
Since TensorFlow 1.4, the self-check has been integrated with TensorFlow itself,
and any missing DLLs will be reported when you execute the `import tensorflow`
statement. The error messages printed below refer to TensorFlow 1.3 and earlier,
and are inaccurate for later versions of TensorFlow.""")
candidate_explanation = False

python_version = sys.version_info.major, sys.version_info.minor
print("\n- Python version is %d.%d." % python_version)
if not (python_version == (3, 5) or python_version == (3, 6)):
candidate_explanation = True
print("- The official distribution of TensorFlow for Windows requires "
"Python version 3.5 or 3.6.")

try:
_, pathname, _ = imp.find_module("tensorflow")
print("\n- TensorFlow is installed at: %s" % pathname)
except ImportError:
candidate_explanation = False
print("""
- No module named TensorFlow is installed in this Python environment. You may
install it using the command `pip install tensorflow`.""")

try:
msvcp140 = ctypes.WinDLL("msvcp140.dll")
except OSError:
candidate_explanation = True
print("""
- Could not load 'msvcp140.dll'. TensorFlow requires that this DLL be
installed in a directory that is named in your %PATH% environment
variable. You may install this DLL by downloading Microsoft Visual
C++ 2015 Redistributable Update 3 from this URL:
https://www.microsoft.com/en-us/download/details.aspx?id=53587""")

try:
cudart64_80 = ctypes.WinDLL("cudart64_80.dll")
except OSError:
candidate_explanation = True
print("""
- Could not load 'cudart64_80.dll'. The GPU version of TensorFlow
requires that this DLL be installed in a directory that is named in
your %PATH% environment variable. Download and install CUDA 8.0 from
this URL: https://developer.nvidia.com/cuda-toolkit""")

try:
nvcuda = ctypes.WinDLL("nvcuda.dll")
except OSError:
candidate_explanation = True
print("""
- Could not load 'nvcuda.dll'. The GPU version of TensorFlow requires that
this DLL be installed in a directory that is named in your %PATH%
environment variable. Typically it is installed in 'C:\Windows\System32'.
If it is not present, ensure that you have a CUDA-capable GPU with the
correct driver installed.""")

cudnn5_found = False
try:
cudnn5 = ctypes.WinDLL("cudnn64_5.dll")
cudnn5_found = True
except OSError:
candidate_explanation = True
print("""
- Could not load 'cudnn64_5.dll'. The GPU version of TensorFlow
requires that this DLL be installed in a directory that is named in
your %PATH% environment variable. Note that installing cuDNN is a
separate step from installing CUDA, and it is often found in a
different directory from the CUDA DLLs. You may install the
necessary DLL by downloading cuDNN 5.1 from this URL:
https://developer.nvidia.com/cudnn""")

cudnn6_found = False
try:
cudnn = ctypes.WinDLL("cudnn64_6.dll")
cudnn6_found = True
except OSError:
candidate_explanation = True

if not cudnn5_found or not cudnn6_found:
print()
if not cudnn5_found and not cudnn6_found:
print("- Could not find cuDNN.")
elif not cudnn5_found:
print("- Could not find cuDNN 5.1.")
else:
print("- Could not find cuDNN 6.")
print("""
The GPU version of TensorFlow requires that the correct cuDNN DLL be installed
in a directory that is named in your %PATH% environment variable. Note that
installing cuDNN is a separate step from installing CUDA, and it is often
found in a different directory from the CUDA DLLs. The correct version of
cuDNN depends on your version of TensorFlow:

* TensorFlow 1.2.1 or earlier requires cuDNN 5.1. ('cudnn64_5.dll')
* TensorFlow 1.3 or later requires cuDNN 6. ('cudnn64_6.dll')

You may install the necessary DLL by downloading cuDNN from this URL:
https://developer.nvidia.com/cudnn""")

if not candidate_explanation:
print("""
- All required DLLs appear to be present. Please open an issue on the
TensorFlow GitHub page: https://github.com/tensorflow/tensorflow/issues""")

sys.exit(-1)

if __name__ == "__main__":
main()

3.检查numpy mkl scipy matplotlib是否安装,若安装是否为最新版本

结语

至此,Tensorflow已经安装完毕,请注意本文中的注意与警告,接下来开始深度学习吧!