[442]tf.Graph().as_default()

TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理。Tensor(张量)意味着N维数组,Flow(流)意味着基于数据流图的计算,TensorFlow为张量从流图的一端流动到另一端计算过程。TensorFlow是将复杂的数据结构传输至人工智能神经网中进行分析和处理过程的系统。

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
'''
最终结果是没有报错
'''

原生接口文章

【Tensorflow】tf.placeholder函数
【TensorFlow】tf.nn.conv2d是怎样实现卷积的
【TensorFlow】tf.nn.max_pool实现池化操作
【Tensorflow】tf.nn.relu函数
【Tensorflow】tf.reshape 函数
【Tensorflow】tf.nn.dropout函数
【Tensorflow】tf.argmax函数
【Tensorflow】tf.cast 类型转换 函数
【Tensorflow】tf.train.AdamOptimizer函数
【Tensorflow】tf.Graph()函数
【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法
【Tensorflow】tf.dynamic_partition 函数 分拆数组

tf.Graph() 函数非常重要,注意体现在两个方面

  1. 它可以通过tensorboard用图形化界面展示出来流程结构

  2. 它可以整合一段代码为一个整体存在于一个图中

声明情况大体有三种

  1. tensor:通过张量本身直接出graph
# -*- coding: utf-8 -*-  
import tensorflow as tf
 
c = tf.constant(4.0)
 
sess = tf.Session()
sess.run(tf.global_variables_initializer())
c_out = sess.run(c)
print(c_out)
print(c.graph == tf.get_default_graph())
print(c.graph)
print(tf.get_default_graph())

输出

4.0
True
<tensorflow.python.framework.ops.Graph object at 0x7f382f9ef110>
<tensorflow.python.framework.ops.Graph object at 0x7f382f9ef110>

2.通过声明一个默认的,然后定义张量内容,在后面可以调用或保存

# -*- coding: utf-8 -*-  
import tensorflow as tf
 
g = tf.Graph()
with g.as_default():
    c = tf.constant(4.0)
 
sess = tf.Session(graph=g)
c_out = sess.run(c)
print(c_out)
print(g)
print(tf.get_default_graph())

输出

4.0
<tensorflow.python.framework.ops.Graph object at 0x7f65f1cb2fd0>
<tensorflow.python.framework.ops.Graph object at 0x7f65de447c90>

3.通过多个声明,在后面通过变量名来分别调用

# -*- coding: utf-8 -*-  
import tensorflow as tf
 
g1 = tf.Graph()
with g1.as_default():
    c1 = tf.constant(4.0)
 
g2 = tf.Graph()
with g2.as_default():
    c2 = tf.constant(20.0)
 
with tf.Session(graph=g1) as sess1:
    print(sess1.run(c1))
with tf.Session(graph=g2) as sess2:
    print(sess2.run(c2))

输出

4.0
20.0

对graph的操作大体有三种

1.保存

# -*- coding: utf-8 -*-  
import tensorflow as tf
 
g1 = tf.Graph()
with g1.as_default():
    # 需要加上名称,在读取pb文件的时候,是通过name和下标来取得对应的tensor的
    c1 = tf.constant(4.0, name='c1')
 
g2 = tf.Graph()
with g2.as_default():
    c2 = tf.constant(20.0)
 
with tf.Session(graph=g1) as sess1:
    print(sess1.run(c1))
with tf.Session(graph=g2) as sess2:
    print(sess2.run(c2))
 
# g1的图定义,包含pb的path, pb文件名,是否是文本默认False
tf.train.write_graph(g1.as_graph_def(),'.','graph.pb',False)

输出

4.0
20.0

并且在当前文件夹下面生成graph.pb文件

2.从pb文件中调用

# -*- coding: utf-8 -*-  
import tensorflow as tf
from tensorflow.python.platform import gfile
 
#load graph
with gfile.FastGFile("./graph.pb",'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    tf.import_graph_def(graph_def, name='')
 
sess = tf.Session()
c1_tensor = sess.graph.get_tensor_by_name("c1:0")
c1 = sess.run(c1_tensor)
print(c1)

输出

4.0

3.穿插调用

# -*- coding: utf-8 -*-  
import tensorflow as tf
 
g1 = tf.Graph()
with g1.as_default():
    # 声明的变量有名称是一个好的习惯,方便以后使用
    c1 = tf.constant(4.0, name="c1")
 
g2 = tf.Graph()
with g2.as_default():
    c2 = tf.constant(20.0, name="c2")
 
with tf.Session(graph=g2) as sess1:
    # 通过名称和下标来得到相应的值
    c1_list = tf.import_graph_def(g1.as_graph_def(), return_elements = ["c1:0"], name = '')
    print(sess1.run(c1_list[0]+c2))

输出

24.0

当然还有很多比较好的地方,比如graph中自带的名称函数,通过with写的内容丰富的代码块,这里就不讲了

  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周小董

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值