提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
- 前言
- 一、TensorFlow结构
- 二、图
- 1.图结构
- 2.TensorBoard
- 三、会话(Session)
- 总结
TensorFlow程序通常被组织成一个构件图阶段和一个执行图阶段。在构建阶段,数据与操作的执行步骤被描述为一个图; 在执行阶段,使用会话执行构建好的图中的操作
术语介绍:
图:TensorFlow将计算表示为指令之间的依赖关系的一种表示方法 会话:TensorFlow跨一个或多个本地或远程设备运行数据流图的机制 张量:TensorFlow中的基本数据对象 节点:提供图当中执行的操作
二、图 1.图结构图包含了一组tf.Operation代表的计算单元对象和tf.Tensor代表的计算单元之间流动的数据
图一般分为默认图和自定义图
查看默认图: (1)通过调用tf.get_default_graph()访问,要将操作添加到默认图形中,直接创建OP即可
(2)op、sess都含有graph属性,默认都在一张图中
def graph():
a = tf.constant(2)
b = tf.constant(3)
c = a + b
print("c=",c)
# 查看默认图
g = tf.get_default_graph()
print("g=", g)
# 查看属性
print("a:", a.graph)
print("b:", b.graph)
# 开启会话
with tf.Session() as sess:
c_val = sess.run(c)
print("c_val :", c_val)
print("sess:", sess.graph)
'''
c= Tensor("add:0", shape=(), dtype=int32)
g=
a:
b:
c_val : 5
sess:
'''
可以通过tf.Graph()自定义创建图 , 如果要在这张图中创建OP,典型用法是使用tf.Graph.as_default()上下文管理器
a = tf.constant(2)
b = tf.constant(3)
c = a + b
# 自定义图
new_g = tf.Graph()
# 定义数据和操作
with new_g.as_default():
a_new = tf.constant(20)
b_new = tf.constant(80)
c_new = a_new + b_new
print("a_new:", a_new.graph)
print("b_new:", b_new.graph)
with tf.Session(graph=new_g) as new_sess:
c_val = new_sess.run(c_new)
print("c:", c_val)
print("new_sess", new_sess.graph)
'''
a_new:
b_new:
c: 100
new_sess
'''
2.TensorBoard
tensorflow用于训练大规模深度神经网络所需的计算,为了更方便tensorflow程序的理解、调试与优化,tensorflow提供了TensorBoard可视化工具
实现程序可视化过程:
(1)实现程序可视化过程
TensorBoard通过读取TensorFlow的事件文件来运行,需要将数据生成一个序列化的Summary protobuf对象
tf.summary.FileWriter(path, graph=sess.graph)
(2)启动TensorBoard
tensorboard --logdir=path
def graph():
a = tf.constant(2)
b = tf.constant(3)
c = a + b
print("c=",c)
# 查看默认图
g = tf.get_default_graph()
print("g=", g)
# 查看属性
print("a:", a.graph)
print("b:", b.graph)
# 开启会话
with tf.Session() as sess:
c_val = sess.run(c)
print("c_val :", c_val)
print("sess:", sess.graph)
# 将图写如本地生成events文件
tf.summary.FileWriter("summary", graph=sess.graph)
运行后终端执行tensorboard --logdir=path,出现如图 打开网址即可
Session允许执行图形或部分图形,为此分配资源并保存中间结果和变量的实际值
tf.Session:用于完整的程序当中 tf.InteractiveSession:用于交互式上下文中的TensorFlow,如shell
由上面知道创建一个图形,如下代码
# 自定义图
new_g = tf.Graph()
# 定义数据和操作
with new_g.as_default():
a_new = tf.constant(20)
b_new = tf.constant(80)
c_new = a_new + b_new
print("a_new:", a_new.graph)
print("b_new:", b_new.graph)
要为该 Graph(图形)创建一个Session(会话),会话还将分配内存来存储变量的当前值,会话掌握资源,用完要回收 - 上下文管理器
建立会话
with tf.Session() as sess:
sess.run(sth)
target:如果将此参数留空(默认设置),会话将仅使用本地计算机中的设备。可以指定grpc://网址,以便指定TensorFlow服务器的地址,这使得会话可以访问该服务器控制的计算机上的所有设备 graph:默认情况下,新的tf.Session将绑定到当前的默认图 config:此参数允许您指定一个tf.ConfigProto以便控制会话的行为。例如,ConfigProto协议用于打印设备使用信息
运行会话 通过使用sess.run(0)来运行operation
run(fetches, feed_dict=None, options=None, run_metadata=None)
fetches:单一的operation,或者列表、元组(其他不属于tensorflow的类型不行) feed_dict:参数运行调用者覆盖图中张量的值,运行时赋值,与tf.placeholder搭配使用,则会检查值的形式是否与占位符兼容
def session():
a = tf.constant(6.8, name='a')
b = tf.constant(7.9, name='b')
c = tf.add(a, b, name='c')
print("a:\n", a)
print("b:\n", b)
print("c:\n", c)
print(".........")
a1 = tf.placeholder(tf.float32)
b1 = tf.placeholder(tf.float32)
c1 = tf.add(a1,b1)
print("a1:\n", a)
print("b1:\n", b)
print("c1:\n", c)
print(".........")
# 查看图
default_g = tf.get_default_graph()
print("default:",default_g)
print("a的属性\n",a.graph)
print("b的属性\n", b.graph)
print(".........")
with tf.compat.v1.Session(config=tf.ConfigProto(allow_soft_placement=True,log_device_placement=True)) as sess:
# placeholder
c_val = sess.run(c1,feed_dict={a1:788.2, b1:82.2})
print("c_val:",c_val)
abc = sess.run([a1,b1,c1])
'''
a:
Tensor("a:0", shape=(), dtype=float32)
b:
Tensor("b:0", shape=(), dtype=float32)
c:
Tensor("c:0", shape=(), dtype=float32)
.........
a1:
Tensor("a:0", shape=(), dtype=float32)
b1:
Tensor("b:0", shape=(), dtype=float32)
c1:
Tensor("c:0", shape=(), dtype=float32)
.........
default:
a的属性
b的属性
c: (AddV2): /job:localhost/replica:0/task:0/device:CPU:0
Add: (AddV2): /job:localhost/replica:0/task:0/device:CPU:0
a: (Const): /job:localhost/replica:0/task:0/device:CPU:0
b: (Const): /job:localhost/replica:0/task:0/device:CPU:0
Placeholder: (Placeholder): /job:localhost/replica:0/task:0/device:CPU:0
Placeholder_1: (Placeholder): /job:localhost/replica:0/task:0/device:CPU:0
c_val: 870.4
'''
虽然在Pycharm报了许多错,但是最终还是能正确运行,
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?