tensorflow 中Graphs的相关文档

2.1 建立图(Building Graphs)

本节主要介绍建立tensorflow图的相关类或函数

核心图的数据结构(Core graph data structures)

tf.Graph

操作 描述
class tf.Graph tensorflow中的计算以图数据流的方式表示
一个图包含一系列表示计算单元的操作对象
以及在图中流动的数据单元以tensor对象表现
tf.Graph.__init__() 建立一个空图
tf.Graph.as_default() 一个将某图设置为默认图,并返回一个上下文管理器
如果不显式添加一个默认图,系统会自动设置一个全局的默认图。
所设置的默认图,在模块范围内所定义的节点都将默认加入默认图中
tf.Graph.as_graph_def
(from_version=None, add_shapes=False)
返回一个图的序列化的GraphDef表示
序列化的GraphDef可以导入至另一个图中(使用 import_graph_def())
或者使用C++ Session API
tf.Graph.finalize() 完成图的构建,即将其设置为只读模式
tf.Graph.finalized 返回True,如果图被完成
tf.Graph.control_dependencies(control_inputs) 定义一个控制依赖,并返回一个上下文管理器
with g.control_dependencies([a, b, c]):
# `d` 和 `e` 将在 `a`, `b`, 和`c`执行完之后运行.
d = …
e = …
tf.Graph.device(device_name_or_function) 定义运行图所使用的设备,并返回一个上下文管理器
with g.device('/gpu:0'): ...
with g.device('/cpu:0'): ...
tf.Graph.name_scope(name) 为节点创建层次化的名称,并返回一个上下文管理器
tf.Graph.add_to_collection(name, value) 将value以name的名称存储在收集器(collection)中
tf.Graph.get_collection(name, scope=None) 根据name返回一个收集器中所收集的值的列表
tf.Graph.as_graph_element
(obj, allow_tensor=True, allow_operation=True)
返回一个图中与obj相关联的对象,为一个操作节点或者tensor数据
tf.Graph.get_operation_by_name(name) 根据名称返回操作节点
tf.Graph.get_tensor_by_name(name) 根据名称返回tensor数据
tf.Graph.get_operations() 返回图中的操作节点列表
tf.Graph.gradient_override_map(op_type_map) 用于覆盖梯度函数的上下文管理器
#class tf.Graph
#tensorflow运行时需要设置默认的图
g = tf.Graph()
with g.as_default():
  # Define operations and tensors in `g`.
  c = tf.constant(30.0)
  assert c.graph is g

##也可以使用tf.get_default_graph()获得默认图,也可在基础上加入节点或子图
c = tf.constant(4.0)
assert c.graph is tf.get_default_graph()
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
#tf.Graph.as_default
#以下两段代码功能相同
#1、使用Graph.as_default():
g = tf.Graph()
with g.as_default():
  c = tf.constant(5.0)
  assert c.graph is g

#2、构造和设置为默认
with tf.Graph().as_default() as g:
  c = tf.constant(5.0)
  assert c.graph is g
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
#tf.Graph.control_dependencies(control_inputs)
# 错误代码
def my_func(pred, tensor):
  t = tf.matmul(tensor, tensor)
  with tf.control_dependencies([pred]):
    # 乘法操作(op)没有创建在该上下文,所以没有被加入依赖控制
    return t

# 正确代码
def my_func(pred, tensor):
  with tf.control_dependencies([pred]):
    # 乘法操作(op)创建在该上下文,所以被加入依赖控制中
    #执行完pred之后再执行matmul
    return tf.matmul(tensor, tensor)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
# tf.Graph.name_scope(name)
# 一个图中包含有一个名称范围的堆栈,在使用name_scope(...)之后,将压(push)新名称进栈中,
#并在下文中使用该名称
with tf.Graph().as_default() as g:
  c = tf.constant(5.0, name="c")
  assert c.op.name == "c"
  c_1 = tf.constant(6.0, name="c")
  assert c_1.op.name == "c_1"

  # Creates a scope called "nested"
  with g.name_scope("nested") as scope:
    nested_c = tf.constant(10.0, name="c")
    assert nested_c.op.name == "nested/c"

    # Creates a nested scope called "inner".
    with g.name_scope("inner"):
      nested_inner_c = tf.constant(20.0, name="c")
      assert nested_inner_c.op.name == "nested/inner/c"

    # Create a nested scope called "inner_1".
    with g.name_scope("inner"):
      nested_inner_1_c = tf.constant(30.0, name="c")
      assert nested_inner_1_c.op.name == "nested/inner_1/c"

      # Treats `scope` as an absolute name scope, and
      # switches to the "nested/" scope.
      with g.name_scope(scope):
        nested_d = tf.constant(40.0, name="d")
        assert nested_d.op.name == "nested/d"

        with g.name_scope(""):
          e = tf.constant(50.0, name="e")
          assert e.op.name == "e"
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值