zoukankan      html  css  js  c++  java
  • (二)Graph类的实现

    基本介绍

    位置:tensorflow/core/graph/graph.h

     1 class Edge;
     2 class EdgeSetTest;
     3 class Graph;
     4 class GraphDef;
     5 class Node;
     6 class VersionDef;
     7 class WhileContext;
     8 
     9 class NeighborIter;  // Declared below
    10 class NodeIter;      // Declared below
    11 class NodeProperties;  // Defined in .cc

    此部分主要涉及图结构里的节点、边、图等类的声明与定义。下面主要对class Node, class Edge, class Graph展开分析。

      

    解析

    (一)class Node:

    其成员函数提供了一系列对节点进行操作的接口,如节点的邻节点检索、入/出节点检索、节点属性查询等。需要主要的一些细节是,class Graph是节点类的友元所以可以访问其内部的所有成员变量,而节点的属性由std::shared_ptr<NodeProperties> props_描述。

    不同种类的类节点:

     1   // A set of mutually exclusive classes for different kinds of nodes,
     2   // class_ is initialized in the Node::Initialize routine based on the
     3   // node's type_string().
     4   enum NodeClass {
     5     NC_UNINITIALIZED,
     6     NC_SWITCH,
     7     NC_MERGE,
     8     NC_ENTER,
     9     NC_EXIT,
    10     NC_NEXT_ITERATION,
    11     NC_LOOP_COND,
    12     NC_CONTROL_TRIGGER,
    13     NC_SEND,
    14     NC_HOST_SEND,
    15     NC_RECV,
    16     NC_HOST_RECV,
    17     NC_CONSTANT,
    18     NC_VARIABLE,
    19     NC_IDENTITY,
    20     NC_GET_SESSION_HANDLE,
    21     NC_GET_SESSION_TENSOR,
    22     NC_DELETE_SESSION_TENSOR,
    23     NC_METADATA,
    24     NC_OTHER  // Not a special kind of node
    25   };

    (二)class Edge:

    其程成员函数提供了对边源节点与目标节点访问的借口,同时class EdgeSetTest和class Graph是其友元类,并且当前class Edge用户自己不能实例化。

    除此之外,需要额外补充的是关于类class GraphEdgesIterable,该类实现对图对象中的边进行迭代器遍历,并保证跳过null的项。同时,其内部class const_iterator类的构造揭示了迭代器类的实现细节。

    (三)class Graph:

     图结构包含很多不同的ops,其最主要的成员变量为std::vector<Node*> nodes_std::vector<Edge*> edges_,其成员函数包含了对这些节点与边的添加、复制、删除、更新、查找、校验等操作。

    Graph对象可使用以下两种方式构造,二者构造的图包含单源节点和单汇节点:

     //可容纳registry中的ops
    1
    Graph::Graph(const OpRegistryInterface* ops) 2 : ops_(ops, FunctionDefLibrary()), 3 versions_(new VersionDef), 4 arena_(8 << 10 /* 8kB */) { 5 versions_->set_producer(TF_GRAPH_DEF_VERSION); 6 versions_->set_min_consumer(TF_GRAPH_DEF_VERSION_MIN_CONSUMER); 7 8 // Initialize the name interning table for assigned_device_name. 9 device_names_.push_back(""); 10 DCHECK_EQ(0, InternDeviceName(""));
    11 12 // Source and sink have no endpoints, just control edges. 13 NodeDef def; 14 def.set_name("_SOURCE"); 15 def.set_op("NoOp"); 16 Status status; 17 Node* source = AddNode(def, &status); 18 TF_CHECK_OK(status); 19 CHECK_EQ(source->id(), kSourceId); 20 21 def.set_name("_SINK"); 22 Node* sink = AddNode(def, &status); 23 TF_CHECK_OK(status); 24 CHECK_EQ(sink->id(), kSinkId); 25 26 AddControlEdge(source, sink); 27 } 28
    // 可容纳flib_def中的ops
    29 Graph::Graph(const FunctionLibraryDefinition& flib_def) 30 : Graph(flib_def.default_registry()) { 31 // Need a new-enough consumer to support the functions we add to the graph. 32 if (flib_def.ToProto().function_size() > 0 && 33 versions_->min_consumer() < 12) { 34 versions_->set_min_consumer(12); 35 } 36 Status s = ops_.AddLibrary(flib_def); 37 CHECK(s.ok()) << s.error_message(); 38 }

    当前文件里,也包含节点、邻节点的类迭代器,用于方便对其进行操作。

  • 相关阅读:
    DateUtils
    Java静态绑定与动态绑定
    Mysql中实现递归查询
    架构一、核心概念
    Spring cron 表达式
    MySql点点滴滴(一)之可视化工具介绍
    java中注解的使用与实例
    3、第一个Python程序
    CSS命名
    如何在Notepad++ 中成功地安装Emmet 插件
  • 原文地址:https://www.cnblogs.com/bytedance/p/8184363.html
Copyright © 2011-2022 走看看