上一篇:2-TensorFlow简介及环境搭建
下一篇:4-梯度下降解决线性回归【综合小练习】
1 从 Hello World 开始
开发工具看大家心情,笔者使用Pycharm
- 新建 helloworld.py 文件
# -*- coding: utf-8 -*-
import tensorflow as tf
import os
# 忽略一些警告信息
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# 创建一个常量 Operation(操作)
hw = tf.constant("Hello World!I Love TensorFlow !")
# 启动一个 TensorFlow 的 Session(会话)
sess = tf.Session()
# 运行 Graph(计算图)
print(sess.run(hw))
# 关闭Session
sess.close()
# 运行结果
Hello World! I Love tensorFlow !
2 TensorFlow编程模式
一般的编程模式
image.png
2-1 命令式编程
容易理解,命令式语句基本没有优化:C,Java,C++,Python
# 命令式编程
a = 2
b = 3
c = a * b
d = c + 1
print (d)
2-2 符号式编程
- TensorFlow 属于符号式编程
- 涉及较多的嵌入和优化,运行速度有同比提升
之前的公式会用计算流图表示,c和d公用内存
image.png
# 符号式编程
import tensorflow as tf
a = tf.constant(2)
b = tf.constant(3)
c = tf.multiply(a, b)
d = tf.add(c, 1)
with tf.Session() as sess:
print (sess.run(d))
3 TensorFlow 基础结构
计算流图
image.png
3-1 图(Graph) 的基本构成
image.png
3-2 TensorFlow基础模型
image.png
- Graph (图) 的形象比喻
化学实验中,一些节点的加热,提纯等,就相当于数据流图中的 操作(Operation),在连接各节点的导管中流动的化学试剂,就相当于数据流图中的数据,即 张量(Tensor)
image.png
3-3 什么是会话 (Session)
例如打开一个浏览器,就是一个会话
TensorFlow 是C/S架构,客户端是我们自己构建的程序,服务端是C++运行时,通过会话去运行我们的程序,返回结果
image.png
* Session 的作用
1 启动图的会话作用
2 可以只run静态数据流图中的某一部分,就像化学实验流程中,只加热其中一个烧杯等。
image.png
3-4 TensorFlow 程序的流程
1. 定义算法的
计算图(Graph)
结构
2. 使用会话(Session)
执行计算
3-5 什么是 Tensor(张量)
计算流图中,流动的数据就是Tensor
数据流图
数据 流图
张量 流图 (TensorFlow)
张量 流图
张量的维度(秩):Rank / Order
三维数组称之为 张量
58568642-56B6-4008-AA01-2A86B3E61C5C.png
三维表示
- Tensor 的属性
525099F1-77A8-488F-A915-676516404355.png
- 几种常见的 Tensor
FFBEF5E5-FC19-4A39-9163-C4AB9FCD3C68.png
1 Constant (常量)
值不能改变的Tensor,定义在 tf.constant, 属性如下图
image.png
import tensorflow as tf
tensor = tf.constant([1,2,3,4,5,6,7])
print (tensor)
# 创建一个会话并run tensor,才能输出里面的值
print (tf.Session().run(tensor))
# result
<tf.Tensor 'Const:0' shape=(7,) dtype=int32>
[1 2 3 4 5 6 7]
2 Variable (变量)
值可以改变的tensor
image.png
import tensorflow as tf
tensor = tf.Variable([1,2,3,4,5,6,7])
print (tensor)
tensor1 = tf.Variable([1,2,3], dtype=tf.int64)
print (tensor)
# result
<tf.Variable 'Variable:0' shape=() dtype=int32_ref>
<tf.Variable 'Variable_1:0' shape=(3,) dtype=int64_ref>
3 Placeholder (占位符)
先占住一个固定的位置,等着你之后往里面添加值的一种Tensor
image.png
image.png
4 SparseTensor (稀疏张量)
一种 “稀疏” 的Tensor,类似线性代数里的稀疏矩阵的概念
只需要定义非零的元素,其余默认为0
image.png
稀疏矩阵示意图
- Tensor 表示法
76926C83-0323-4221-A293-3FB60FB499D8.png
3-6 再来看一下图 graph
image.png
# -*- coding: utf-8 -*-
import tensorflow as tf
import os
# 忽略一些警告信息
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
const1 = tf.constant([[2, 2]])
const2 = tf.constant([[4],
[4]])
multiple = tf.matmul(const1, const2)
# 尝试print 输出multiple的值
print (multiple)
# 创建了 Session(会话)对象
sess = tf.Session()
result = sess.run(multiple)
print (result)
if const1.graph is tf.get_default_graph():
print ("const1所在的图(Graph)是当前上下文默认的图")
# 结果:
Tensor("MatMul:0", shape=(1, 1), dtype=int32)
[[16]]
const1所在的图(Graph)是当前上下文默认的图
4 可视化利器 TesonBoard
人工智能好比黑盒,接收输入,产生输出, TensorBoard的作用就是打开黑盒,看到里面构建的神经网络,可以帮你更好的构建和调参
image.png
image.png
- 用法
# 在代码中保存TensorFlow日志
tf.summary.FileWrite("日志保存路径", sess.graph)
$ tensorboard --logdir=日志所在路径
符号的含义
image.png
TensorBoard 使用示例
# -*- coding: UTF-8 -*-
import tensorflow as tf
# 用一个线性方程例子
# y = W * x + b
W= tf.Variable(2.0, dtype=tf.float32, name="Weight") # 权重
b = tf.Variable(1.0, dtype=tf.float32, name="Bias") # 偏差
x = tf.placeholder(dtype=tf.float32, name="Input") # 输入
with tf.name_scope("Output"): # 输出的命名空间
y = W * x + b
# 定义保存日志的路径
path = "./log"
# 创建用于初始化所有变量(Variable)的操作
init = tf.global_variables_initializer()
# 创建Session(会话)
with tf.Session() as sess:
sess.run(init) # 初始化变量
writer = tf.summary.FileWriter(path, sess.graph)
result = sess.run(y, {x: 3.0})
print("y = {}".format(result))
运行以上代码,会在当前目录创建log文件夹,并保存日志
在终端使用如下命令,启动tensorboard服务
tensorboard --logdir=./log
在浏览器输入 : localhost:6006
image.png
5 酷炫模拟游乐园 PlayGround
- javascript编写的网页应用
- 通过浏览器就可以训练简单的神经网络
- 训练过程可视化, 高度可定制