tensorflow学习笔记 为什么要写 tf.Graph().as_default()

一.with tf.Graph().as_default()

1.解释一:

首先,去tensorflow官网API上查询 tf.Graph() 会看到如下图所示的内容:

 总体含义是说:

tf.Graph() 表示实例化了一个类,一个用于 tensorflow 计算和表示用的数据流图,通俗来讲就是:在代码中添加的操作(画中的结点)和数据(画中的线条)都是画在纸上的“画”,而图就是呈现这些画的纸,你可以利用很多线程生成很多张图,但是默认图就只有一张。

tf.Graph().as_default() 表示将这个类实例,也就是新生成的图作为整个 tensorflow 运行环境的默认图,如果只有一个主线程不写也没有关系,tensorflow 里面已经存好了一张默认图,可以使用tf.get_default_graph() 来调用(显示这张默认纸),当你有多个线程就可以创造多个tf.Graph(),就是你可以有一个画图本,有很多张图纸,这时候就会有一个默认图的概念了。

import tensorflow as tf
c=tf.constant(4.0)
assert c.graph is tf.get_default_graph() #看看主程序中新建的一个变量是不是在默认图里
g=tf.Graph()
with g.as_default():
    c=tf.constant(30.0)
    assert c.graph is g
'''
最终结果是没有报错
'''

2.解释二:

tensorflow下的Graph中 tf.Operation是一个node,而tf.Tensor是一个edge。

在没有特别说明的情况下,程序中定义的tf.Operation均是添加进入default_graph中。若使用

graph1=tf.Graph()    # 声明tf.Graph()的一个类实例,即获取一个graph
with graph1.as_default():
    #完成graph1这个计算图中的tf.Operation的定义,即将在这个with所调用的上下文管理器中定义的tf.Operation添加进入声明的tf.Graph实例graph1中
    pass

其实这种方式本质上也是将graph设置为default_graph,所以可以认为,所有的tf.Operation均是添加到default_graph进行的,而这个default_graph是可以设置的。

再说说这个with graph1.as_default():

with会启动一个上下文管理器。所谓上下文管理器,就是在程序执行前将上文中当前所需要的资源准备好,并在结束时被系统回收。

with graph1.as_default()的含义是在这个with启动的上下文管理器中将graph1设置为default_graph,在with启动的上下文管理器内部所定义的tf.Operation则会添加进入当前的default_graph中,也就是graph1中。在整个with代码块结束后,default_graph将会重新设置为之前的,属于全局的graph。

从下面的代码可以对以上说法进行验证:

# -*- coding:utf-8 -*-
import tensorflow as tf
 
v1 = tf.Variable([0.0], name='v1')
v2 = tf.constant([1.0], name='v2')
add = tf.add(v1,v2, name='add')
 
graph1 = tf.Graph()
 
with graph1.as_default():
    v4 = tf.Variable([3.0], name='v4') with tf.Session() as sess1: sess1.run(tf.global_variables_initializer()) print sess1.run(add) with tf.Session(graph=tf.get_default_graph()) as sess2: sess2.run(tf.global_variables_initializer()) print sess2.run(v1) g_op = graph1.get_operations() print 'the operations in graph1>>> \n', g_op print '\n' d_op = tf.get_default_graph().get_operations() print 'the operations in default graph>>> \n',d_op

输出结果如下:

[ 1.]
[ 0.]
the operations in graph1>>> 
[<tf.Operation 'v4/initial_value' type=Const>, <tf.Operation 'v4' type=VariableV2>, <tf.Operation 'v4/Assign' type=Assign>, <tf.Operation 'v4/read' type=Identity>]
 
the operations in default graph>>> 
[<tf.Operation 'v1/initial_value' type=Const>, <tf.Operation 'v1' type=VariableV2>, <tf.Operation 'v1/Assign' type=Assign>, <tf.Operation 'v1/read' type=Identity>, <tf.Operation 'v2' type=Const>, <tf.Operation 'add' type=Add>, <tf.Operation 'init' type=NoOp>, <tf.Operation 'init_1' type=NoOp>]
 

3.数据流图

TensorFlow计算,表示为数据流图
TensorFlow 中的所有计算都会被转化为计算图上的节点。
TensorFlow 是一个通过计算图的形式来表述计算的编程系统。
TensorFlow中的每个计算都是计算图的一个节点,而节点之间的边描述了计算之间的依赖关系。
TensorFlow 的计算模型是有向图,采用数据流图 (Data Flow Graphs),其中每个节点代表一些函数或计算,而边代表了数值、矩阵或张量。

数据流图是用于定义计算结构的。在 TensorFlow 中,数据流图本质上是一组链接在一起的函数,每个函数都会将其输出传递给 0 个、1 个或多个位于这个级联链上的其他函数。

4.为什么要写 .as_default()

目的是将这个图设置为默认图,会话设置成默认对话,这样的话在with语句的外面也能使用这个会话执行。

tf.Session()创建一个会话,当上下文管理器退出时会话关闭和资源释放自动完成。
tf.Session().as_default()创建一个默认会话,当上下文管理器退出时会话没有关闭,还可以通过调用会话进行run()和eval()操作

参考博客:

为什么要写 tf.Graph().as_default()

Tensorflow-with tf.Graph().as_default()

tf.Graph().as_default() 详解

猜你喜欢

转载自www.cnblogs.com/yuehouse/p/12029384.html