tensorflow 中Graphs的相关文档

来源:互联网 发布:什么是数据共享 编辑:程序博客网 时间:2024/06/05 12:15

2.1 建立图(Building Graphs)

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

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

tf.Graph

操作描述class tf.Graphtensorflow中的计算以图数据流的方式表示
一个图包含一系列表示计算单元的操作对象
以及在图中流动的数据单元以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 APItf.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 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 买的t恤太小了怎么办? 汽车全包围脚垫翘边怎么办 针织衣服线跑了怎么办 老板跟老板娘吵架我应该怎么办 偷看老板娘洗澡被发现怎么办? 幽浮2人挂了怎么办 蜻蜓fm业务扣费怎么办 退出键退不到主页面怎么办 3d纯英文版怎么办 3d单位设置错了怎么办 翼龙贷款不还款怎么办 lol视角变低了怎么办 王者荣耀体验卡太多怎么办 文件打开后超出电脑屏幕怎么办 2400g玩lol跳fps怎么办 手机吃鸡延迟长怎么办 三星s8发热卡顿怎么办 逆水寒修为不够怎么办 诛仙摆摊金币被扣怎么办 诛仙手游宠物亲密度满了怎么办 诛仙包裹满了怎么办 感觉自己心理有问题 怎么办 刺激战场手机配置低怎么办 国土防线2没反应怎么办 镜之边缘迷路了怎么办? 陌陌直播不清晰怎么办 夏利n5 1.0费油怎么办 gg修改器是病毒怎么办 传送门骑士联机读条慢怎么办 被打成轻伤派出所不抓人怎么办 有人上门找事怎么办算正当防卫吗 win10 电脑账户被停用怎么办 电脑一键还原后黑屏怎么办 win一键还原后黑屏怎么办 打架对方群殴我我怎么办 杀了人没钱赔怎么办 团伙打架被对方所刀捅伤怎么办 过失致人重伤赔偿不起怎么办 被别人打了派出所不管怎么办 先动手的被打伤怎么办 自为伤了人怎么办?