1. 图(Graph)的核心数据结构

  • tf.Graph.__init__:建立一个空图;
  • tf.Graph.as_default():一个将某图设置为默认图,并返回一个上下文管理器,常与 with 结构相搭配;

    g = tf.Graph()
    with g.as_default():
      # Define operations and tensors in `g`.
      c = tf.constant(30.0)
      assert c.graph is g
    
    c = tf.constant(4.0)
    assert c.graph is tf.get_default_graph()
  • tf.Graph.finalized:返回True,如果图被完成

    • tf.Graph.finalize():完成图的构建,即将其设置为只读模式
    if not tf.Graph.finalized:
        tf.Graph.finalize()
  • tf.Graph.control_dependencies(control_inputs):定义一个控制依赖,并返回一个上下文管理器,也常与 with 结构搭配;

    with g.control_dependencies([a, b, c]):
        ....
        d = …
        e = …
    
    
    # `d` 和 `e` 将在 `a`, `b`, 和`c`执行完之后运行.
    
    
    def my_func(pred, tensor):
        with tf.control_dependencies([pred]):
            # 乘法操作(op)创建在该上下文,所以被加入依赖控制中
            #执行完pred之后再执行matmul
            return tf.matmul(tensor, tensor)
  • tf.Graph.device(device_name_or_function):定义运行图所使用的设备,并返回一个上下文管理器

    with g.device('/gpu:0'): ...
    with g.device('/cpu:0'): ...
posted on 2017-04-26 12:53  未雨愁眸  阅读(527)  评论(0编辑  收藏  举报