Skip to content

Commit af0b248

Browse files
committed
add content
1 parent 743eb3b commit af0b248

13 files changed

+133
-9
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.vscode
22
build
33
reference
4-
teaching
4+
teaching
5+
workspace.code-workspace
6+
source/_static/code/.idea

make_latex.bat

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
make latex

source/_static/code/basic/1plus1.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import tensorflow as tf
2+
3+
# 定义一个“计算图”
4+
a = tf.constant(1) # 定义一个常量Tensor(张量)
5+
b = tf.constant(1)
6+
c = a + b # 等价于 c = tf.add(a, b),c是张量a和张量b通过Add这一Operation(操作)所形成的新张量
7+
8+
sess = tf.Session() # 实例化一个Session(会话)
9+
c_ = sess.run(c) # 通过Session的run()方法对计算图里的节点(张量)进行实际的计算
10+
print(c_)

source/_static/code/basic/AmatmulB.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import tensorflow as tf
2+
3+
A = tf.ones(shape=[2, 3]) # tf.ones(shape)定义了一个形状为shape的全1矩阵
4+
B = tf.ones(shape=[3, 2])
5+
C = tf.matmul(A, B)
6+
7+
sess = tf.Session()
8+
C_ = sess.run(C)
9+
print(C_)

source/_static/code/basic/aplusb.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import tensorflow as tf
2+
3+
a = tf.placeholder(dtype=tf.int32) # 定义一个占位符Tensor
4+
b = tf.placeholder(dtype=tf.int32)
5+
c = a + b
6+
7+
a_ = input("a = ") # 从终端读入一个整数并放入变量a_
8+
b_ = input("b = ")
9+
10+
sess = tf.Session()
11+
c_ = sess.run(c, feed_dict={a: a_, b: b_}) # feed_dict参数传入为了计算c所需要的张量的值
12+
print("a + b = %d" % c_)

source/_static/code/basic/example_tensorflow_eager_autograd.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import tensorflow as tf
33
import tensorflow.contrib.eager as tfe
4-
tfe.enable_eager_execution()
4+
tf.enable_eager_execution()
55

66
X_raw = np.array([2013, 2014, 2015, 2016, 2017], dtype=np.float32)
77
y_raw = np.array([12000, 14000, 15000, 16500, 17500], dtype=np.float32)
@@ -26,7 +26,7 @@ def loss(X_, y_):
2626
optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-3)
2727
for e in range(num_epoch):
2828
# 前向传播
29-
y_pred = a * X + b
29+
# y_pred = a * X + b
3030

3131
# 反向传播,利用Eager模式下的tfe.implicit_gradients()自动计算梯度
3232
grad = grad_fn(X, y)

source/_static/code/basic/variable.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import tensorflow as tf
2+
3+
a = tf.get_variable(name='a', shape=[])
4+
initializer = tf.assign(a, 0) # tf.assign(x, y)返回一个“将张量y的值赋给变量x”的操作
5+
a_plus_1 = a + 1 # 等价于 a + tf.constant(1)
6+
plus_one_op = tf.assign(a, a_plus_1)
7+
8+
sess = tf.Session()
9+
sess.run(initializer)
10+
for i in range(5):
11+
sess.run(plus_one_op) # 对变量a执行加一操作
12+
a_ = sess.run(a) # 获得变量a的值并存入a_
13+
print(a_)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import tensorflow as tf
2+
3+
a = tf.get_variable(name='a', shape=[], initializer=tf.zeros_initializer) # 指定初始化器为全0初始化
4+
a_plus_1 = a + 1
5+
plus_one_op = tf.assign(a, a_plus_1)
6+
7+
sess = tf.Session()
8+
sess.run(tf.global_variables_initializer()) # 初始化所有变量
9+
for i in range(5):
10+
sess.run(plus_one_op)
11+
a_ = sess.run(a)
12+
print(a_)

source/_static/code/test/test.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import tensorflow as tf
2+
3+
zero = tf.fill([2, 3], tf.constant(0)) # == tf.zeros([2, 3])
4+
tf.get_default_graph().collections
5+
a = tf.Variable(initial_value=zero) # name="Variable:0"
6+
print(tf.GraphKeys.GLOBAL_VARIABLES) # "variables"
7+
a_initializer = tf.assign(a, a._initial_value)
8+
a_plus_1 = tf.add(a, 1)
9+
tf.Graph
10+
# b = tf.get_variable()
11+
12+
with tf.Session() as sess:
13+
sess.run(a_initializer)
14+
print(sess.run(a_plus_1))

source/basic.rst

+50-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,54 @@ TensorFlow,顾名思义,就是Tensor(张量)进行Flow(流动)的过
1616

1717
在对TensorFlow的具体概念,如张量(Tensor)、数据流图(Dataflow Graph)、变量(Variable)、优化器(Optimizer)等进行具体介绍之前,本手册先举一个具体的例子,以让读者能对TensorFlow的基本运作方式有一个直观的理解。
1818

19+
基础示例:TensorFlow 1+1
20+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
21+
22+
TensorFlow本质上是一个符号式的(基于计算图的)计算框架。这里以计算1+1作为Hello World的示例。
23+
24+
.. literalinclude:: ../source/_static/code/basic/1plus1.py
25+
26+
输出::
27+
28+
2
29+
30+
上面这个程序只能计算1+1,以下程序通过 ``tf.placeholder()`` (占位符张量)和 ``sess.run()`` 的 ``feed_dict=`` 参数展示了如何使用TensorFlow计算任意两个数的和:
31+
32+
.. literalinclude:: ../source/_static/code/basic/aplusb.py
33+
34+
运行程序::
35+
36+
>>> a = 2
37+
>>> b = 3
38+
a + b = 5
39+
40+
变量(Variable)是一种特殊类型的张量,使用 ``tf.get_variable()`` 建立,与编程语言中的变量很相似。使用变量前需要先初始化,变量的值可以在计算图的计算过程中被修改。以下示例如何建立一个变量,将其值初始化为0,并逐次累加1。
41+
42+
.. literalinclude:: ../source/_static/code/basic/variable.py
43+
44+
输出::
45+
46+
1.0
47+
2.0
48+
3.0
49+
4.0
50+
5.0
51+
52+
以下代码和上述代码等价,在声明变量时指定初始化器,并通过 ``tf.global_variables_initializer()`` 一次性初始化所有变量,在实际工程中更常用:
53+
54+
.. literalinclude:: ../source/_static/code/basic/variable_with_initializer.py
55+
56+
矩阵乃至张量运算是科学计算(包括机器学习)的基本操作。以下程序展示如何计算两个矩阵 :math:`\begin{bmatrix} 1 & 1 & 1 \\ 1 & 1 & 1 \end{bmatrix}` 和 :math:`\begin{bmatrix} 1 & 1 \\ 1 & 1 \\ 1 & 1 \end{bmatrix}` 的乘积:
57+
58+
.. literalinclude:: ../source/_static/code/basic/AmatmulB.py
59+
60+
输出::
61+
62+
[[3. 3.]
63+
[3. 3.]]
64+
65+
Placeholder(占位符张量)和Variable(变量张量)也同样可以为向量、矩阵乃至更高维的张量。
66+
1967
基础示例:线性回归
2068
^^^^^^^^^^^^^^^^^^^^^^^^^^^
2169

@@ -46,7 +94,7 @@ NumPy:命令式编程
4694
TensorFlow:符号式编程
4795
----------------------------
4896

49-
TensorFlow使用 **符号式编程** 来进行数值运算。首先,我们需要将待计算的过程抽象为数据流图,将输入、运算和输出都用符号化的节点来表达。然后,我们将数据不断地送入输入节点,让数据沿着数据流图进行计算和流动,最终到达我们需要的特定输出节点。以下代码展示了如何基于TensorFlow的符号式编程方法完成与前节相同的任务。其中, ``tf.placeholder()`` 即可以视为一种“符号化的输入节点”,而 ``sess.run(output_node, feed_dict={input_node: data})`` 可以视作将数据送入输入节点,沿着数据流图计算并到达输出节点并返回值的过程。
97+
TensorFlow使用 **符号式编程** 来进行数值运算。首先,我们需要将待计算的过程抽象为数据流图,将输入、运算和输出都用符号化的节点来表达。然后,我们将数据不断地送入输入节点,让数据沿着数据流图进行计算和流动,最终到达我们需要的特定输出节点。以下代码展示了如何基于TensorFlow的符号式编程方法完成与前节相同的任务。其中, ``tf.placeholder()`` 即可以视为一种“符号化的输入节点”,使用 ``tf.get_variable()`` 定义模型的参数(Variable类型的张量可以使用 ``tf.assign()`` 进行赋值),而 ``sess.run(output_node, feed_dict={input_node: data})`` 可以视作将数据送入输入节点,沿着数据流图计算并到达输出节点并返回值的过程。
5098

5199
.. literalinclude:: ../source/_static/code/basic/example_tensorflow.py
52100
:lines: 9-
@@ -93,7 +141,7 @@ TensorFlow的动态图支持 *
93141
.. [注1] 其实线性回归是有解析解的。这里使用梯度下降方法只是为了展示TensorFlow的运作方式。
94142
.. [注2] 此处的损失函数为均方差 :math:`L(x) = \frac{1}{2} \sum_{i=1}^5 (ax_i + b - y_i)^2`。其关于参数 ``a`` 和 ``b`` 的偏导数为 :math:`\frac{\partial L}{\partial a} = \sum_{i=1}^5 (ax_i + b - y) x_i`,:math:`\frac{\partial L}{\partial b} = \sum_{i=1}^5 (ax_i + b - y)`
95143
96-
变量、常量与占位符
144+
张量(变量、常量与占位符
97145
^^^^^^^^^^^^^^^^^^^^^^^^^^^
98146

99147
会话与计算图

source/installation.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ TensorFlow的具体安装步骤可参考官方网站上的说明(https://tenso
1515
3. (针对GPU版本)安装NVIDIA显卡驱动程序、 `CUDA Toolkit <https://developer.nvidia.com/cuda-downloads>`_ 和 `cuDNN <https://developer.nvidia.com/cudnn>`_ 。值得注意的事项有:
1616

1717
* 建议的顺序是:先安装最新版NVIDIA显卡驱动程序,再安装CUDA(安装时不要选择同时安装驱动),最后安装cuDNN。CUDA附带的显卡驱动程序可能过旧;
18-
* 在Linux系统(如Ubuntu)下安装NVIDIA显卡驱动程序往往不会一帆风顺,注意在安装前禁用系统自带的开源显卡驱动、禁用主板的Secure Boot功能。更详细的指导可以参考 `这篇文章 <https://www.linkedin.com/pulse/installing-nvidia-cuda-80-ubuntu-1604-linux-gpu-new-victor/>`_ ;
19-
* CUDA Toolkit和cuDNN的版本一定要与TensorFlow官方网站安装说明的版本一致,注意官方网站安装说明里要求安装的版本可能并非最新版本。截至本手册撰写时,TensorFlow版本为1.4,其使用的版本为CUDA Toolkit 8.0和cuDNN v6.0,而此时NVIDIA网站上的最新版本为CUDA Toolkit 9.1和cuDNN v7.0
18+
* 在Ubuntu下有一个很简易的驱动安装方法:在系统设置(System Setting)里面选软件与更新(Software & Updates),然后点选Additional Drivers里面的“Using NVIDIA binary driver”选项并点选右下角的“Apply Changes”即可,系统即会自动安装NVIDIA驱动。否则,NVIDIA显卡驱动程序在Linux系统上的安装往往不会一帆风顺,注意在安装前禁用系统自带的开源显卡驱动Nouveau、禁用主板的Secure Boot功能。更详细的指导可以参考 `这篇文章 <https://www.linkedin.com/pulse/installing-nvidia-cuda-80-ubuntu-1604-linux-gpu-new-victor/>`_ ;
19+
* CUDA Toolkit和cuDNN的版本一定要与TensorFlow官方网站安装说明的版本一致,注意官方网站安装说明里要求安装的版本可能并非最新版本;
2020
* cuDNN的安装方式比较特殊,你需要手动将下载的安装包复制到CUDA的安装目录下。
2121

2222
.. [] GPU加速的效果与GPU的性能有关,如果CPU性能较好(例如4核的英特尔酷睿i7)但GPU仅有入门级的性能,其实速度提升不大,大概1-2倍。不过如果GPU性能强大的话(例如,本手册写作时,NVIDIA GeForce GTX 1080或NVIDIA GeForce TITAN系列是市场上性能较强大的显卡型号),十几倍甚至更高的加速效果也是可以达到的。同时,GPU的加速效果与任务本身也有关。入门级的TensorFlow模型往往不需要太高的计算性能,CPU版本的TensorFlow足以胜任,因此可以待到掌握TensorFlow的基本知识后,再决定是否购入更高级的GPU以得到更快的训练速度。

source/ops.rst

+3
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@ TensorFlow运算符
1818
神经网络相关操作
1919
^^^^^^^^^^^^^^^^^^^^^^^^
2020

21+
TensorFlow与Matlab运算对照表
22+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
2124

2225

source/preface.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
前言
22
======
33

4-
目前,市面上已经有不少与TensorFlow相关的中文技术书籍。不过这些书籍大部分都以深度学习为主线,而将TensorFlow作为这些深度学习模型的实现方式。这样固然有体系完整的优点,然而对于已经在机器学习或深度学习领域有所基础,希望侧重于了解TensorFlow本身的读者而言,就显得不够友好。同时,虽然TensorFlow有官方的教学文档(https://tensorflow.google.cn/tutorials),然而在体例上显得逻辑性不足,缺乏一般教学文档从浅入深,层次递进的特性,而更类似于一系列技术文档的罗列。于是,我希望编写一本手册,以尽量简洁的语言展示TensorFlow作为一个计算框架的主要特性,力图能让已经有一定机器学习/深度学习知识及编程能力的读者迅速上手TensorFlow,并在实际编程过程中可以随时查阅并解决实际问题。该手册的主要特征有:
4+
目前,市面上已经有不少与TensorFlow相关的中文技术书籍。不过这些书籍大部分都以深度学习为主线,而将TensorFlow作为这些深度学习模型的实现方式。这样固然有体系完整的优点,然而对于已经对机器学习或深度学习理论有所了解,希望侧重于学习TensorFlow本身的读者而言,就显得不够友好。同时,虽然TensorFlow有官方的教学文档(https://tensorflow.google.cn/tutorials),然而在体例上显得逻辑性不足,缺乏一般教学文档从浅入深,层次递进的特性,而更类似于一系列技术文档的罗列。于是,我希望编写一本手册,以尽量简洁的语言展示TensorFlow作为一个计算框架的主要特性,力图能让已经有一定机器学习/深度学习知识及编程能力的读者迅速上手TensorFlow,并在实际编程过程中可以随时查阅并解决实际问题。该手册的主要特征有:
55

66
* 定位以工具书为主,编排以TensorFlow的各项概念和功能为核心,力求能够让TensorFlow开发者快速查阅。各章相对独立,不一定需要按顺序阅读。同时,正文中不会出现太多关于深度学习和机器学习的理论介绍;
77
* 包含大量的典型示例,让读者可以快速理解并举一反三;
@@ -10,7 +10,7 @@
1010

1111
在整本手册中,带“*”的部分均为选读。
1212

13-
本手册的暂定名称《简单粗暴TensorFlow》是向我的好友兼同学Chris Wu编写的《简单粗暴LaTeX》(https://github.com/wklchris/Note-by-LaTeX)致敬。该手册清晰精炼,是LaTeX领域不可多得的中文资料,也是我在编写这一技术文档时所学习的对象。
13+
本手册的暂定名称《简单粗暴TensorFlow》是向我的好友兼同学Chris Wu编写的《简单粗暴 :math:`\text{\LaTeX}` 》(https://github.com/wklchris/Note-by-LaTeX)致敬。该手册清晰精炼,:math:`\text{\LaTeX}` 领域不可多得的中文资料,也是我在编写这一技术文档时所学习的对象。
1414

1515
|
1616

0 commit comments

Comments
 (0)