• (一)Scope类的实现


    基本介绍

    Class Scope类对象是一系列tensorflow操作(ops)所携带属性的集合,这些ops具有相似性质,如:前缀名。

    解析

    本文以下列代码段为参考对Class Scope的内部实现细节展开分析。

    1 Scope root = Scope::NewRootScope();
    2 Scope linear = root.NewSubScope("linear");
    3 // W will be named "linear/W"
    4 auto W = Variable(linear.WithOpName("W"),{2, 2}, DT_FLOAT);
    5 // b will be named "linear/b"
    6 auto b = Variable(linear.WithOpName("b"),{2}, DT_FLOAT);
    7 auto x = Const(linear, {...}); // name: "linear/Const"
    8 auto m = MatMul(linear, x, W); // name: "linear/MatMul"
    9 auto r = BiasAdd(linear, m, b); // name: "linear/BiasAdd"

    (1)Scope::NewRootScope():

    1 Scope Scope::NewRootScope() {
    2   Graph* graph = new Graph(OpRegistry::Global());
    3   ShapeRefiner* refiner =
    4       new ShapeRefiner(graph->versions(), graph->op_registry());
    5   return Scope(new Impl(graph, new Status, new Impl::NameMap, refiner,
    6                         /* disable_shape_inference */ false));
    7 }
    ---Scope::NewScope()
    |    |
    |    |---new Graph()
    |    |---new ShapeRefiner()
    |    |---Scope(new Impl())

    通过调用该静态成员函数,函数会构造图结构,同时实例化ShapeRefiner类负责图对象中节点的上下文初始化,然后利用 前述两实例化对象实例化Impl类并将其指针作为参数传递给Scope类的构造函数以初始化std::unique_ptr<Impl> impl_成员变量,最后返回初始化后的Scope类对象。在调用Scope构造函数时,传递的参数是Impl类型的指针,class Impl (scope_internal.h内给出定义)嵌套在class Scope中,主要负责对Scope类对象中的成员变量进行初始化,如graph_、status_、name_map_等。

    (2)Scope::NewSubScope():

    1   /// Return a new scope. Ops created with this scope will have
    2   /// `name/child_scope_name` as the prefix. The actual name will be unique
    3   /// in the current scope. All other properties are inherited from the current
    4   /// scope. If `child_scope_name` is empty, the `/` is elided.
    5   Scope NewSubScope(const string& child_scope_name) const;

    创建新的scope,在其范围内的ops的前缀形如:name/child_scope_name,所有其他的性质从当前的scope继承而来。

  • 相关阅读:
    输出重定向
    echo带颜色输出
    shell学习视频目录
    css盒模型
    jQuery表格模糊搜索
    mysql基础语法3
    mysql基础语法2
    mysql基础语法1
    pyspider框架的使用
    quill富文本框图片上传重写
  • 原文地址:https://www.cnblogs.com/bytedance/p/8183668.html
Copyright © 2020-2023  润新知