学习目标:
- 第一天:tensorflow框架实用
- 第二天:数据读取、神经网络基础
- 第三天:卷积神经网络、验证码识别
第一天:
- 学习学习介绍
- tensorflow框架的使用
- 1)tensorflow的结构
- 2)tensorflow的各个组件:图、会话、张量、变量
- 3)简单的线性回归案例----将TensorFlow用起来
一、深度学习的介绍
1.1 深度学习与机器学习的区别
学习目标:知道深度学习与机器学习的区别
区别:深度学习没有特征提取
1.1.1 特征提取方面
- 机器学习的特征工程步骤是要靠手动完成的,而且需要大量领域专业知识
- 深度学习通常由多个层组成,它们通常将更简单的模型组合在一起,将数据从一层传递到另一层来构建更复杂的模型。通过训练大量数据自动得到模型,不需要人工特征提取环节
深度学习算法试图从数据中学习高级功能,这是深度学习的一个非常独特的部分。因此,减少了为每个问题开发新特征提取器的任务。适合用在难提取特征的图像、语音、自然语言处理领域
1.1.2 数据量和计算性能要求
机器学习需要的执行时间远少于深度学习,深度学习参数往往很庞大,需要通过大量
数据的多次优化来训练参数
- 第一,深度学习需要大量的训练数据集
- 第二,训练深度神经网络需要大量的算力
可能要花费数天、甚至数周的时间,才能使用数百万张图像的数据集训练出一个深度网络。所以深度学习通常需要强大的GPU服务器来进行计算
1.1.3 算法代表
机器学习:朴素贝叶斯,决策树
深度学习:神经网络
1.2 深度学习的应用场景
图像识别:物体识别、场景识别、车型识别、人脸检测跟踪、人脸关键点定位、人脸身份认证
自然语言处理技术:机器翻译、文本识别、聊天对话
语音技术:语音识别
1.3 深度学习框架介绍
1.3.1 常见深度学习框架对比
1.3.2 tensorflow的特点
官网:https://www.tensorflow.org/
1.3.3 tensorflow的安装
1 CPU版本
2 GPU版本:核芯数量多,更适合处理并行任务
pip install tensorflow==1.8 -i https://pypi.douban.com/simple
二、tensorflow框架介绍
2.1 TF数据流图
学习目标:说明tensorflow的数据流图结构
2.1.1 案例:tensorflow实现一个加法运算
import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 去警告 def tensorflow_demo(): """ tensorflow的基本结构 :return: """ # tensorflow实现加法运算 a_t = tf.constant(2) b_t = tf.constant(3) c_t = a_t + b_t print("c_t:", c_t) # 开启会话 with tf.Session() as sess: c_t_value = sess.run(c_t) print("c_t_value:", c_t_value) return None if __name__ == "__main__": tensorflow_demo()
c_t: Tensor("add:0", shape=(), dtype=int32) c_t_value: 5
2 TensorFlow结构分析
TensorFlow程序通常被组织成一个构件图阶段和一个执行图阶段。
在构建阶段,数据与操作的执行步骤被描述为一个图
在执行阶段,使用会话执行构建好的图中的操作
图:这是TensorFlow将计算表示为指令之间的依赖关系的一种表示法
会话:TensorFlow跨一个或多个本地或远程设备运行数据流图的机制
张量:TensorFlow中的基本数据对象
节点:提供图当中执行的操作
2.2 图与TensorBoard
学习目标:
- 说明图的基本使用
- 应用tf.Graph创建图,tf.get_default_graph获取默认图
- 知道开启TensorBoard过程
- 知道图当中op的名字以及命名空间
2.2.1 什么是图结构
图包含了一组tf.Operation代表的计算单元对象和tf.Tensor代表的计算单元之间流动的数据
2.2.2 图相关操作
1 默认图
通常tensorflow会默认帮我们创建一张图
查看默认图的两种方法:
- 通过调用**tf.get_default_graph()**访问,要将操作添加到默认图形中,直接创建OP即可
- op、sess都含有graph属性,默认都在一张图中
import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 去警告 def graph_demo(): """ 图的演示 :return: """ # tensorflow实现加法运算 a_t = tf.constant(2) b_t = tf.constant(3) c_t = a_t + b_t print("c_t:", c_t) # 查看默认图 # 方法1:调用方法 default_g = tf.get_default_graph() print("default_g:", default_g) # 方法2:查看属性 print("a_t的图属性:", a_t.graph) print("c_t的图属性:", c_t.graph) # 开启会话 with tf.Session() as sess: c_t_value = sess.run(c_t) print("c_t_value:", c_t_value) print("sess的图属性:", sess.graph) return None if __name__ == "__main__": graph_demo()
c_t: Tensor("add:0", shape=(), dtype=int32) default_g: <tensorflow.python.framework.ops.Graph object at 0x000002A49DEA72B0> a_t的图属性: <tensorflow.python.framework.ops.Graph object at 0x000002A49DEA72B0> c_t的图属性: <tensorflow.python.framework.ops.Graph object at 0x000002A49DEA72B0> c_t_value: 5 sess的图属性: <tensorflow.python.framework.ops.Graph object at 0x000002A49DEA72B0>
2 创建图
- 可以通过**tf.Graph()**自定义创建图
- 如果要在这张图中创建OP,典型用法是使用**tf.Graph.as_default()**上下文管理器
import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 去警告 def graph_demo(): """ 图的演示 :return: """ # tensorflow实现加法运算 a_t = tf.constant(2) b_t = tf.constant(3) c_t = a_t + b_t print("c_t:", c_t) # 查看默认图 # 方法1:调用方法 default_g = tf.get_default_graph() print("default_g:", default_g) # 方法2:查看属性 print("a_t的图属性:", a_t.graph) print("c_t的图属性:", c_t.graph) # 开启会话 with tf.Session() as sess: c_t_value = sess.run(c_t) print("c_t_value:", c_t_value) print("sess的图属性:", sess.graph) print("--------------------------------") # 自定义图 new_g = tf.Graph() # 在自己的图中定义数据和操作 with new_g.as_default(): a_new = tf.constant(20) b_new = tf.constant(30) c_new = a_new + b_new print("c_new:", c_new) print("a_new的图属性:", a_new.graph) print("c_new的图属性:", c_new.graph) # 开启new_g的会话 with tf.Session(graph=new_g) as new_sess: # 试图运行自定义图中的数据,操作 c_new_value = new_sess.run(c_new) print("c_new_value:", c_new_value) print("new_sess的图属性:", new_sess.graph) return None if __name__ == "__main__": graph_demo()
c_t: Tensor("add:0", shape=(), dtype=int32) default_g: <tensorflow.python.framework.ops.Graph object at 0x00000152C1C87400> a_t的图属性: <tensorflow.python.framework.ops.Graph object at 0x00000152C1C87400> c_t的图属性: <tensorflow.python.framework.ops.Graph object at 0x00000152C1C87400> c_t_value: 5 sess的图属性: <tensorflow.python.framework.ops.Graph object at 0x00000152C1C87400> -------------------------------- c_new: Tensor("add:0", shape=(), dtype=int32) a_new的图属性: <tensorflow.python.framework.ops.Graph object at 0x00000152E6FFD9B0> c_new的图属性: <tensorflow.python.framework.ops.Graph object at 0x00000152E6FFD9B0> c_new_value: 50 new_sess的图属性: <tensorflow.python.framework.ops.Graph object at 0x00000152E6FFD9B0>
2.2.3 TensorBoard可视化学习
tensorflow可用于训练大规模深度神经网络所需的计算,使用该工具设计的计算往往复杂而深奥。为了更方便tensorflow程序的理解、调试与优化,tensorflow提供了TensorBoard可视化工具
实现程序可视化过程:
1 数据序列化-events文件
TensorBoard通过读取TensorFlow的事件文件来运行,需要将数据生成一个序列化的Summary protobuf对象
tf.summary.FileWriter(path, graph=sess.graph)
2 启动TensorBoard
tensorboard --logdir=path
例:
import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 去警告 def graph_demo(): """ 图的演示 :return: """ # tensorflow实现加法运算 a_t = tf.constant(2) b_t = tf.constant(3) c_t = a_t + b_t print("c_t:", c_t) # 查看默认图 # 方法1:调用方法 default_g = tf.get_default_graph() print("default_g:", default_g) # 方法2:查看属性 print("a_t的图属性:", a_t.graph) print("c_t的图属性:", c_t.graph) # 开启会话 with tf.Session() as sess: c_t_value = sess.run(c_t) print("c_t_value:", c_t_value) print("sess的图属性:", sess.graph) # 1)将图写入本地生成events文件 tf.summary.FileWriter("summary", graph=sess.graph) # tmp文件夹下 print("--------------------------------") # 自定义图 new_g = tf.Graph() # 在自己的图中定义数据和操作 with new_g.as_default(): a_new = tf.constant(20) b_new = tf.constant(30) c_new = a_new + b_new print("c_new:", c_new) print("a_new的图属性:", a_new.graph) print("c_new的图属性:", c_new.graph) # 开启new_g的会话 with tf.Session(graph=new_g) as new_sess: # 试图运行自定义图中的数据,操作 c_new_value = new_sess.run(c_new) print("c_new_value:", c_new_value) print("new_sess的图属性:", new_sess.graph) return None if __name__ == "__main__": graph_demo()
之后,在终端输入:
得到链接,点击网址链接
成功!
2.2.4 OP
即操作对象
1 常见OP
那些是OP
操作函数 | 操作对象 |
tf.constant(Tensor对象) | 输入Tensor对象-Const输出 Tensor对象 |
tf.add(Tensor对象1,Tensor对象2) | 输入(Tensor对象1,Tensor对象2) ,add对象,输出 Tensor对象3 |
- 一个图一个命名空间,互不干扰影响
import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 去警告 def graph_demo(): """ 图的演示 :return: """ # tensorflow实现加法运算 a_t = tf.constant(2, name="a_t") b_t = tf.constant(3, name="b_t") c_t = tf.add(a_t, b_t, name="c_t") print("c_t:", c_t) # 查看默认图 # 方法1:调用方法 default_g = tf.get_default_graph() print("default_g:", default_g) # 方法2:查看属性 print("a_t的图属性:", a_t.graph) print("c_t的图属性:", c_t.graph) # 开启会话 with tf.Session() as sess: c_t_value = sess.run(c_t) print("c_t_value:", c_t_value) print("sess的图属性:", sess.graph) # 1)将图写入本地生成events文件 tf.summary.FileWriter("logs", graph=sess.graph) print("--------------------------------") # 自定义图 new_g = tf.Graph() # 在自己的图中定义数据和操作 with new_g.as_default(): a_new = tf.constant(20, name="a_new") b_new = tf.constant(30, name="b_new") c_new = tf.add(a_new, b_new, name="c_new") print("c_new:", c_new) print("a_new的图属性:", a_new.graph) print("c_new的图属性:", c_new.graph) tf.summary.FileWriter("log2", graph=sess.graph) # 开启new_g的会话 with tf.Session(graph=new_g) as new_sess: # 试图运行自定义图中的数据,操作 c_new_value = new_sess.run(c_new) print("c_new_value:", c_new_value) print("new_sess的图属性:", new_sess.graph) return None if __name__ == "__main__": graph_demo()
2.3 会话
学习目标:
- 应用sess.rn或者eval运行图程序并获取张量值
- 应用feed_dict机制实现运行时填充数据
- 应用placeholder实现创建占位符
2.3.1 会话创建
- tf.Session:用于完整的程序当中
- tf.InteractiveSession:用于交互式上下文中的TensorFlow,例如shell
上下文管理器:
with tf.Session() as sess: sess.run(sth)
- target:如果将此参数留空(默认设置),会话将仅使用本地计算机中的设备。可以指定grpc://网址,以便指定TensorFlow服务器的地址,这使得会话可以访问该服务器控制的计算机上的所有设备
- graph:默认情况下,新的tf.Session将绑定到当前的默认图
- config:此参数允许您指定一个tf.ConfigProto以便控制会话的行为。例如,ConfigProto协议用于打印设备使用信息
# 运行会话并打印设备信息 sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))
2 会话的run()
通过使用sess.run(0)来运行operation
run(fetches, feed_dict=None, options=None, run_metadata=None)
- fetches:单一的operation,或者列表、元组(其他不属于tensorflow的类型不行)
- feed_dict:参数运行调用者覆盖图中张量的值,运行时赋值,与tf.placeholder搭配使用,则会检查值的形式是否与占位符兼容
# 创建图 a = tf.constant(5.0) b = tf.constant(6.0) c = a + b # 创建会话 sess = tf.Session() # 计算C的值 print(sess.run(c)) print(c.eval(session=sess))
3 feed操作
- placeholder提供占位符,run时候通过feed_dict指定参数
import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 去警告 def session_demo(): """ 会话的演示:打印设备信息 :return: """ # tensorflow实现加法运算 a_t = tf.constant(2, name="a_t") b_t = tf.constant(3, name="b_t") c_t = tf.add(a_t, b_t, name="c_t") print("a_t", a_t) print("b_t", b_t) print("c_t:", c_t) print("------------------------------") # 定义占位符 a_ph = tf.placeholder(tf.float32) b_ph = tf.placeholder(tf.float32) c_ph = tf.add(a_ph, b_ph) print("a_ph:", a_ph) print("b_ph:", b_ph) print("c_ph:", c_ph) print("------------------------------") # 查看默认图 # 方法1:调用方法 default_g = tf.get_default_graph() print("default_g:", default_g) # 方法2:查看属性 print("a_t的图属性:", a_t.graph) print("c_t的图属性:", c_t.graph) print("-------------------------------") # 开启会话 with tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)) as sess: # 运行placeholder c_ph_value = sess.run(c_ph, feed_dict={a_ph:3.9, b_ph:4.8}) print('c_ph_value', c_ph_value) print("------------------------------") abc = sess.run([a_t, b_t, c_t]) # 传入列表,返回列表 print("abc:", abc) print("c_t_value;", c_t.eval()) print("sess的图属性:", sess.graph) #tf.summary.FileWriter("logs", graph=sess.graph) # 1)将图写入本地生成events文件 if __name__ == "__main__": session_demo()
a_t Tensor("a_t:0", shape=(), dtype=int32) b_t Tensor("b_t:0", shape=(), dtype=int32) c_t: Tensor("c_t:0", shape=(), dtype=int32) ------------------------------ a_ph: Tensor("Placeholder:0", dtype=float32) b_ph: Tensor("Placeholder_1:0", dtype=float32) c_ph: Tensor("Add:0", dtype=float32) ------------------------------ default_g: <tensorflow.python.framework.ops.Graph object at 0x000001618EF65748> a_t的图属性: <tensorflow.python.framework.ops.Graph object at 0x000001618EF65748> c_t的图属性: <tensorflow.python.framework.ops.Graph object at 0x000001618EF65748> ------------------------------- Device mapping: no known devices. Add: (Add): /job:localhost/replica:0/task:0/device:CPU:0 c_t: (Add): /job:localhost/replica:0/task:0/device:CPU:0 Placeholder_1: (Placeholder): /job:localhost/replica:0/task:0/device:CPU:0 Placeholder: (Placeholder): /job:localhost/replica:0/task:0/device:CPU:0 b_t: (Const): /job:localhost/replica:0/task:0/device:CPU:0 a_t: (Const): /job:localhost/replica:0/task:0/device:CPU:0 c_ph_value 8.700001 ------------------------------ abc: [2, 3, 5] c_t_value; 5 sess的图属性: <tensorflow.python.framework.ops.Graph object at 0x000001618EF65748>