graph_graphics - CSDN
精华内容
参与话题
  • Graph(1)--图的基本概念

    千次阅读 2017-06-25 21:33:45
    图是由顶点(vertex)集合及顶点间的关系组成的一种数据结构。 本文主要介绍图的定义以及图的相关术语。

    1.图的定义

    图是由顶点(vertex)集合及顶点间的关系组成的一种数据结构。Graph=(V,E)其中,顶点集合 V={x|x} 是有穷非空集合;E={(x,y)|x,yV} 是顶点间关系的有穷集合,也叫边(edge)集合。Path(x,y)表示从顶点x到y的一条单向通路,他是有方向的。

    2.图的相关概念

    有向图(Directed Graph):一般用<u,v>表示
    无向图(Undirected Graph):一般用(u,v)表示
    完全图(Complete Graph):在n个顶点组成的无向图中,若有n(n1)2条边,则称为无向完全图。在n个顶点组成的有向图中,若有n(n1)条边,则称为有向完全图。完全图中的边数达到最大。
    这里写图片描述
    权(Weight):在某些图中,边具有与之相关的数量(比如一个顶点到另一个顶点的距离、花费的代价、所需的时间、次数等)。这种带权图也叫做网络(Network)
    邻接顶点(Adjacent vertex):如果(u,v)是E(G)中的一条边,则u和v互为邻接顶点,且边(u,v)依附于顶点u和v,顶点u和v依附于边(u,v)。如果<u,v>是E(G)中的一条有向边,则称顶点u邻接到顶点v,顶点v邻接自顶点u,边<u,v>与顶点u和v相关联。
    子图(Subgraph):Subgraph Let G = (V, E) be a graph with vertex set V and edge set E. A subgraph of G is a graph G’ = (V’, E’) where
    1. V’ is a subset of V
    2. E’ consists of edges (v, w) in E such that both v and w are in V’
    这里写图片描述
    度(Degree):与顶点v关联的边数,称为v的度,记作deg(v)。有向图中,顶点的度是入度和出度之和。顶点v的入度是指以v为终点的有向边的条数,记作indeg(v),顶点v的出度是指以v为始点的有向边的条数,记作outdeg(v)。顶点v的度为deg(v)=indeg(v)+outdeg(v)。一般地,若图中有n个顶点,e条边,那么:e=12{ni=1deg(vi)}
    路径(Path)&路径长度(Path length):路径可以用顶点序列表示,在某些算法中,也可用一系列边来表示。对与不带权图,路径长度指的是此路径上边的条数,对于带权图,路径长度指的是此路径上各条边的权值之和。
    简单路径&回路(Cycle):路径上各顶点互不重复,这样的路径为简单路径。路径上第一个顶点与最后一个顶点重合,这样的路径为回路。
    这里写图片描述
    连通图(Connected graph)&连通分量(Connected component ):无向图中,若从v1v2有路径,则称v1v2是连通的。如果图中任一对顶点都是连通的,则称此图是连通图。非连通图的极大连通子图叫做连通分量。
    强连通图(Strongly connected graph)&强连通分量(Strongly connected component ):有向图中,若在每一对vivj之间存在一条从vivj的路径,也存在一条从vjvi的路径,则称此图是强连通图。非强连通图的极大强连通子图叫做强连通分量。
    生成树(Spanning tree):一个无向连通图的生成树是它的极小连通子图,若图中有n个顶点,则其生成树由n-1条边构成。若是有向图,则可能得到它的若干有向树组成的森林。
    最小生成树(Minimum spanning tree):一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边。

    *包含图的所有顶点,n-1条边
    *没有回路
    *边的权重和最小
    展开全文
  • 构建图 Graph

    千次阅读 2019-02-17 16:47:30
    核心图数据结构 class tf.Graph 数据流图 class tf.Operation class tf.Tensor 张量类型 class tf.DType tf.as_dtype(type_value) 实用功能 tf.device(DEV) tf.name_scope(name) tf.control_dependencies...

    构建图

    核心图数据结构 class tf.Graph 数据流图
    class tf.Operation class tf.Tensor 张量类型 class tf.DType tf.as_dtype(type_value) 实用功能 tf.device(DEV) tf.name_scope(name) tf.control_dependencies(control_inputs) tf.convert_to_tensor(value,dtype = None,name = None) tf.get_default_graph() tf.import_graph_def(graph_def,input_map = None,return_elements = None,name = None,op_dict = None) 图集合 tf.add_to_collection(name, value) tf.get_collection(key,scope = None) class tf.GraphKeys 定义新的业务 class tf.RegisterGradient tf.NoGradient(op_type) class tf.RegisterShape class tf.TensorShape class tf.Dimension tf.op_scope(values,name,default_name) tf.get_seed(op_seed) 用于构建TensorFlow图的类和函数。

    核心图数据结构

    用于构建TensorFlow图的类和函数。

    核心图数据结构

    class tf.Graph

    TensorFlow计算,表示为dataflow graph。

    图表包含一组操作对象,表示计算单位; 和Tensor对象一样,表示在操作之间流动的数据单位。

    始终默认注册为Graph,并可通过调用tf.get_default_graph()来访问。 要将operation添加到默认Graph, 只需调用其中一个函数定义新的operation:

    In [1]:

    import tensorflow as tf
    c = tf.constant(4.0)
    assert c.graph is tf.get_default_graph()
    

    In [3]:

    c.graph
    

    Out[3]:

    <tensorflow.python.framework.ops.Graph at 0x2589729c8d0>

    另一种典型的用法涉及Graph.as_default()上下文管理器,它在上下文的生存期内覆盖当前的默认图形:

    In [4]:

    g = tf.Graph()
    with g.as_default():
       # Define operations and tensors in `g`.
        c = tf.constant(30.0)
        assert c.graph is g
    

    重要注意:对于图的构造,这个类不是线程安全的。所有操作都应该从单个线程创建,或者必须提供外部同步。除非另有规定,否则所有方法都不是线程安全的.

    tf.Graph.init()

    Creates a new, empty Graph.

    tf.Graph.as_default()

    返回使此Graph成为默认图形的上下文管理器。

    如果要在同一进程中创建多个图,则应使用此方法。 为方便起见,提供了一个全局默认图,如果您没有明确创建新图,则所有操作都将添加到此图中。 使用此方法with关键字指定应将在块范围内创建的操作添加到此图中。

    默认图是当前线程的属性。 如果您创建一个新线程,并希望在该线程中使用默认图形,则必须在该线程的函数中显式添加 with g.as_default():.

    以下代码示例是等效的:

    In [5]:

    # 1. Using Graph.as_default():
    g = tf.Graph()
    with g.as_default():
      c = tf.constant(5.0)
      assert c.graph is g
    
    # 2. Constructing and making default:
    with tf.Graph().as_default() as g:
      c = tf.constant(5.0)
      assert c.graph is g
    # Returns:
    # A context manager for using this graph as the default graph.
    

    tf.Graph.as_graph_def(from_version=None)

    Returns a serialized GraphDef representation of this graph.

    The serialized GraphDef can be imported into another Graph (using import_graph_def()) or used with the C++ Session API.

    This method is thread-safe.

    Args: from_version: Optional. If this is set, returns a GraphDef containing only the nodes that were added to this graph since its version property had the given value. Returns: A GraphDef protocol buffer.

    tf.Graph.finalize()

    最后确定这个图表,使它成为只读的。 在调用g.finalize()之后,不能向g添加任何新的操作。此方法用于确保图形在多个线程之间共享时不会添加任何操作,例如在使用QueueRunner时。

    tf.Graph.control_dependencies(control_inputs)

    Returns a context manager that specifies control dependencies.

    Use with the with keyword to specify that all operations constructed within the context should have control dependencies on control_inputs. For example:

    d and e will only run after ab, and c have executed.

    with g.control_dependencies([a, b, c]):

    d = ...
    e = ...

    Multiple calls to control_dependencies() can be nested, and in that case a new Operation will have control dependencies on the union of control_inputs from all active contexts.

    with g.control_dependencies([a, b]):

    Ops declared here run after a and b.

    with g.control_dependencies([c, d]):

    # Ops declared here run after `a`, `b`, `c`, and `d`.
    

    N.B. The control dependencies context applies only to ops that are constructed within the context. Merely using an op or tensor in the context does not add a control dependency. The following example illustrates this point: 注: 控制依赖关系上下文仅适用于在上下文中构造的操作。 仅在上下文中使用op或tensor不会添加控件依赖项。 以下示例说明了这一点:

    In [ ]:

    # WRONG
    def my_func(pred, tensor):
      t = tf.matmul(tensor, tensor)
      with tf.control_dependencies([pred]):
        # The matmul op is created outside the context, so no control
        # dependency will be added.
        return t
    
    # RIGHT
    def my_func(pred, tensor):
      with tf.control_dependencies([pred]):
        # The matmul op is created in the context, so a control dependency
        # will be added.
        return tf.matmul(tensor, tensor)
    

    Args: control_inputs: A list of Operation or Tensor objects, which must be executed or computed before running the operations defined in the context. Returns: A context manager that specifies control dependencies for all operations constructed within the context.

    Raises: TypeError: If control_inputs is not a list of Operation or Tensor objects.

    -

    tf.Graph.device(device_name_or_function)

    Returns a context manager that specifies the default device to use.

    device_name_or_function参数可以是设备名称字符串,设备函数或None:

    如果它是设备名称字符串,则在此上下文中构造的所有操作都将分配给具有该名称的设备。 如果它是一个函数,它将被视为从操作对象到设备名称字符串的函数,并在每次创建新操作时调用。 操作将以返回的名称分配给设备。 如果为None,则清除默认设备。 For example: with g.device('/gpu:0'):

    All operations constructed in this context will be placed

    on GPU 0.

    with g.device(None):

    # All operations constructed in this context will have no
    # assigned device.
    
    

    Defines a function from Operation to device string.

    def matmul_on_gpu(n): if n.type == "MatMul": return "/gpu:0" else: return "/cpu:0"

    with g.device(matmul_on_gpu):

    All operations of type "MatMul" constructed in this context

    will be placed on GPU 0; all other operations will be placed

    on CPU 0.

    In [ ]:

    Args:
    device_name_or_function: The device name or function to use in the context.
    Returns:
    A context manager that specifies the default device to use for newly created ops.
    
    tf.Graph.name_scope(name)
    Returns a context manager that creates hierarchical names for operations.
    
    A graph maintains a stack of name scopes. A with name_scope(...): statement pushes a new name onto the stack for the lifetime of the context.
    
    The name argument will be interpreted as follows:
    
    A string (not ending with '/') will create a new name scope, in which name is appended to the prefix of all operations created in the context. If name has been used before, it will be made unique by calling self.unique_name(name).
    A scope previously captured from a with g.name_scope(...) as scope: statement will be treated as an "absolute" name scope, which makes it possible to re-enter existing scopes.
    A value of None or the empty string will reset the current name scope to the top-level (empty) name scope.
    For example:
    
    with tf.Graph().as_default() as g:
      c = tf.constant(5.0, name="c")
      assert c_1.name == "c"
      c_1 = tf.constant(6.0, name="c")
      assert c_1.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.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.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.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.name == "nested/d"
    
            with g.name_scope(""):
              e = tf.constant(50.0, name="e")
              assert e.name == "e"
    The name of the scope itself can be captured by with g.name_scope(...) as scope:, which stores the name of the scope in the variable scope. This value can be used to name an operation that represents the overall result of executing the ops in a scope. For example:
    
    inputs = tf.constant(...)
    with g.name_scope('my_layer') as scope:
      weights = tf.Variable(..., name="weights")
      biases = tf.Variable(..., name="biases")
      affine = tf.matmul(inputs, weights) + biases
      output = tf.nn.relu(affine, name=scope)
    Args:
    name: A name for the scope.
    Returns:
    A context manager that installs name as a new name scope.
    
    A Graph instance supports an arbitrary number of "collections" that are identified by name. For convenience when building a large graph, collections can store groups of related objects: for example, the tf.Variable uses a collection (named tf.GraphKeys.VARIABLES) for all variables that are created during the construction of a graph. The caller may define additional collections by specifying a new name.
    
    tf.Graph.add_to_collection(name, value)
    Stores value in the collection with the given name.
    
    Args:
    name: The key for the collection. For example, the GraphKeys class contains many standard names for collections.
    value: The value to add to the collection.
    
    -
    tf.Graph.get_collection(name, scope=None)
    Returns a list of values in the collection with the given name.
    
    Args:
    key: The key for the collection. For example, the GraphKeys class contains many standard names for collections.
    scope: (Optional.) If supplied, the resulting list is filtered to include only items whose name begins with this string.
    Returns:
    The list of values in the collection with the given name, or an empty list if no value has been added to that collection. The list contains the values in the order under which they were collected.
    
    tf.Graph.as_graph_element(obj, allow_tensor=True, allow_operation=True)
    Returns the object referred to by obj, as an Operation or Tensor.
    
    This function validates that obj represents an element of this graph, and gives an informative error message if it is not.
    
    This function is the canonical way to get/validate an object of one of the allowed types from an external argument reference in the Session API.
    
    This method may be called concurrently from multiple threads.
    
    Args:
    obj: A Tensor, an Operation, or the name of a tensor or operation. Can also be any object with an _as_graph_element() method that returns a value of one of these types.
    allow_tensor: If true, obj may refer to a Tensor.
    allow_operation: If true, obj may refer to an Operation.
    Returns:
    The Tensor or Operation in the Graph corresponding to obj.
    
    Raises:
    TypeError: If obj is not a type we support attempting to convert to types.
    ValueError: If obj is of an appropriate type but invalid. For example, an invalid string.
    KeyError: If obj is not an object in the graph.
    
    -
    tf.Graph.get_operation_by_name(name)
    Returns the Operation with the given name.
    
    This method may be called concurrently from multiple threads.
    
    Args:
    name: The name of the Operation to return.
    Returns:
    The Operation with the given name.
    
    Raises:
    TypeError: If name is not a string.
    KeyError: If name does not correspond to an operation in this graph.
    
    -
    tf.Graph.get_tensor_by_name(name)
    Returns the Tensor with the given name.
    
    This method may be called concurrently from multiple threads.
    
    Args:
    name: The name of the Tensor to return.
    Returns:
    The Tensor with the given name.
    
    Raises:
    TypeError: If name is not a string.
    KeyError: If name does not correspond to a tensor in this graph.
    
    -
    tf.Graph.get_operations()
    Return the list of operations in the graph.
    
    You can modify the operations in place, but modifications to the list such as inserts/delete have no effect on the list of operations known to the graph.
    
    This method may be called concurrently from multiple threads.
    
    Returns:
    A list of Operations.
    
    tf.Graph.get_default_device()
    Returns the default device.
    
    Returns:
    A string.
    
    tf.Graph.seed
    tf.Graph.unique_name(name)
    Return a unique Operation name for "name".
    
    Note: You rarely need to call unique_name() directly. Most of the time you just need to create "with g.name_scope()" blocks to generate structured names.
    
    unique_name is used to generate structured names, separated by "/", to help identify Operations when debugging a Graph. Operation names are displayed in error messages reported by the TensorFlow runtime, and in various visualization tools such as TensorBoard.
    
    Args:
    name: The name for an Operation.
    Returns:
    A string to be passed to create_op() that will be used to name the operation being created.
    
    tf.Graph.version
    Returns a version number that increases as ops are added to the graph.
    
    tf.Graph.create_op(op_type, inputs, dtypes, input_types=None, name=None, attrs=None, op_def=None, compute_shapes=True)
    Creates an Operation in this graph.
    
    This is a low-level interface for creating an Operation. Most programs will not call this method directly, and instead use the Python op constructors, such as tf.constant(), which add ops to the default graph.
    
    Args:
    op_type: The Operation type to create. This corresponds to the OpDef.name field for the proto that defines the operation.
    inputs: A list of Tensor objects that will be inputs to the Operation.
    dtypes: A list of DType objects that will be the types of the tensors that the operation produces.
    input_types: (Optional.) A list of DTypes that will be the types of the tensors that the operation consumes. By default, uses the base DType of each input in inputs. Operations that expect reference-typed inputs must specify input_types explicitly.
    name: (Optional.) A string name for the operation. If not specified, a name is generated based on op_type.
    attrs: (Optional.) A list of AttrValue protos for the attr field of the NodeDef proto that will represent the operation.
    op_def: (Optional.) The OpDef proto that describes the op_type that the operation will have.
    compute_shapes: (Optional.) If True, shape inference will be performed to compute the shapes of the outputs.
    Raises:
    TypeError: if any of the inputs is not a Tensor.
    Returns:
    An Operation object.
    
    tf.Graph.gradient_override_map(op_type_map)
    EXPERIMENTAL: A context manager for overriding gradient functions.
    
    This context manager can be used to override the gradient function that will be used for ops within the scope of the context.
    
    For example:
    
    @tf.RegisterGradient("CustomSquare")
    def _custom_square_grad(op, inputs):
      # ...
    
    with tf.Graph().as_default() as g:
      c = tf.constant(5.0)
      s_1 = tf.square(c)  # Uses the default gradient for tf.square.
      with g.gradient_override_map({"Square": "CustomSquare"}):
        s_2 = tf.square(s_2)  # Uses _custom_square_grad to compute the
                              # gradient of s_2.
    Args:
    op_type_map: A dictionary mapping op type strings to alternative op type strings.
    Returns:
    A context manager that sets the alternative op type to be used for one or more ops created in that context.
    
    Raises:
    TypeError: If op_type_map is not a dictionary mapping strings to strings.
    
    -
    class tf.Operation
    Represents a graph node that performs computation on tensors.
    
    An Operation is a node in a TensorFlow Graph that takes zero or more Tensor objects as input, and produces zero or more Tensor objects as output. Objects of type Operation are created by calling a Python op constructor (such as tf.matmul()) or Graph.create_op().
    
    For example c = tf.matmul(a, b) creates an Operation of type "MatMul" that takes tensors a and b as input, and produces c as output.
    
    After the graph has been launched in a session, an Operation can be executed by passing it to Session.run(). op.run() is a shortcut for calling tf.get_default_session().run(op).
    
    tf.Operation.name
    The full name of this operation.
    
    tf.Operation.type
    The type of the op (e.g. "MatMul").
    
    tf.Operation.inputs
    The list of Tensor objects representing the data inputs of this op.
    
    tf.Operation.control_inputs
    The Operation objects on which this op has a control dependency.
    
    Before this op is executed, TensorFlow will ensure that the operations in self.control_inputs have finished executing. This mechanism can be used to run ops sequentially for performance reasons, or to ensure that the side effects of an op are observed in the correct order.
    
    Returns:
    A list of Operation objects.
    
    tf.Operation.outputs
    The list of Tensor objects representing the outputs of this op.
    
    tf.Operation.device
    The name of the device to which this op has been assigned, if any.
    
    Returns:
    The string name of the device to which this op has been assigned, or None if it has not been assigned to a device.
    
    tf.Operation.graph
    The Graph that contains this operation.
    
    tf.Operation.run(feed_dict=None, session=None)
    Runs this operation in a Session.
    
    Calling this method will execute all preceding operations that produce the inputs needed for this operation.
    
    N.B. Before invoking Operation.run(), its graph must have been launched in a session, and either a default session must be available, or session must be specified explicitly.
    
    Args:
    feed_dict: A dictionary that maps Tensor objects to feed values. See Session.run() for a description of the valid feed values.
    session: (Optional.) The Session to be used to run to this operation. If none, the default session will be used.
    
    -
    tf.Operation.get_attr(name)
    Returns the value of the attr of this op with the given name.
    
    Args:
    name: The name of the attr to fetch.
    Returns:
    The value of the attr, as a Python object.
    
    Raises:
    ValueError: If this op does not have an attr with the given name.
    

    In [ ]:

    tf.Operation.__init__(node_def, g, inputs=None, output_types=None, control_inputs=None, input_types=None, original_op=None, op_def=None)
    Creates an Operation.
    
    NOTE: This constructor validates the name of the Operation (passed as "node_def.name"). Valid Operation names match the following regular expression:
    
    [A-Za-z0-9.][A-Za-z0-9_.-/]*
    
    Args:
    node_def: graph_pb2.NodeDef. NodeDef for the Operation. Used for attributes of graph_pb2.NodeDef, typically "name", "op", and "device". The "input" attribute is irrelevant here as it will be computed when generating the model.
    g: Graph. The parent graph.
    inputs: list of Tensor objects. The inputs to this Operation.
    output_types: list of types_pb2.DataType. List of the types of the Tensors computed by this operation. The length of this list indicates the number of output endpoints of the Operation.
    control_inputs: list of operations or tensors from which to have a control dependency.
    input_types: List of types_pb2.DataType representing the types of the Tensors accepted by the Operation. By default uses [x.dtype.base_dtype for x in inputs]. Operations that expect reference-typed inputs must specify these explicitly.
    original_op: Optional. Used to associate the new Operation with an existing Operation (for example, a replica with the op that was replicated).
    op_def: Optional. The op_def_pb2.OpDef proto that describes the op type that this Operation represents.
    Raises:
    TypeError: if control inputs are not Operations or Tensors, or if node_def is not a NodeDef, or if g is not a Graph, or if inputs are not Tensors, or if inputs and input_types are incompatible.
    ValueError: if the node_def name is not valid.
    
    -
    tf.Operation.node_def
    Returns a serialized NodeDef representation of this operation.
    
    Returns:
    A NodeDef protocol buffer.
    
    tf.Operation.op_def
    Returns the OpDef proto that represents the type of this op.
    
    Returns:
    An OpDef protocol buffer.
    
    tf.Operation.values()
    DEPRECATED: Use outputs.
    

    class tf.Tensor

    表示操作生成的值。

    Tensor是一个操作输出之一的符号句柄。 它不保存该操作的输出值,而是提供在TensorFlow会话中计算这些值的方法。

    这个class 有两个主要目的:

    1.Tensor可以作为输入传递给另一个Operation。 这将在操作之间建立数据流连接,这使TensorFlow能够执行表示大型多步计算的整个Graph。

    2.在会话中启动图形后,可以通过将其传递给Session.run()来计算Tensor的值。 t.eval()是调用tf.get_default_session().run(t)。 在下面的示例中,c,d和e是符号Tensor对象,而result是一个存储具体值的numpy数组:

    In [7]:

    # Build a dataflow graph.
    c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
    d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
    e = tf.matmul(c, d)
    
    # Construct a `Session` to execut the graph.
    sess = tf.Session()
    
    # Execute the graph and store the value that `e` represents in `result`.
    result = sess.run(e)
    result
    

    Out[7]:

    array([[1., 3.],
           [3., 7.]], dtype=float32)

    In [ ]:

    tf.Tensor.dtype
    The DType of elements in this tensor.
    
    tf.Tensor.name
    The string name of this tensor.
    
    tf.Tensor.value_index
    The index of this tensor in the outputs of its Operation.
    
    tf.Tensor.graph
    The Graph that contains this tensor.
    
    tf.Tensor.op
    The Operation that produces this tensor as an output.
    
    tf.Tensor.consumers()
    Returns a list of Operations that consume this tensor.
    
    Returns:
    A list of Operations.
    
    tf.Tensor.eval(feed_dict=None, session=None)
    Evaluates this tensor in a Session.
    
    Calling this method will execute all preceding operations that produce the inputs needed for the operation that produces this tensor.
    
    N.B. Before invoking Tensor.eval(), its graph must have been launched in a session, and either a default session must be available, or session must be specified explicitly.
    
    Args:
    feed_dict: A dictionary that maps Tensor objects to feed values. See Session.run() for a description of the valid feed values.
    session: (Optional.) The Session to be used to evaluate this tensor. If none, the default session will be used.
    Returns:
    A numpy array corresponding to the value of this tensor.
    
    tf.Tensor.get_shape()
    Returns the TensorShape that represents the shape of this tensor.
    
    The shape is computed using shape inference functions that are registered for each Operation type using tf.RegisterShape. See TensorShape for more details of what a shape represents.
    
    The inferred shape of a tensor is used to provide shape information without having to launch the graph in a session. This can be used for debugging, and providing early error messages. For example:
    

    In [12]:

    c = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
    
    print( c.get_shape() )
    d = tf.constant([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0], [0.0, 1.0]])
    
    print (d.get_shape())
    # Raises a ValueError, because `c` and `d` do not have compatible
    # inner dimensions.
    e = tf.matmul(c, d)
    
    f = tf.matmul(c, d, transpose_a=True, transpose_b=True)
    
    print (e.get_shape())
    print (f.get_shape())
    
    (2, 3)
    (4, 2)
    
    ---------------------------------------------------------------------------
    InvalidArgumentError                      Traceback (most recent call last)
    ~\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
       1627   try:
    -> 1628     c_op = c_api.TF_FinishOperation(op_desc)
       1629   except errors.InvalidArgumentError as e:
    
    InvalidArgumentError: Dimensions must be equal, but are 3 and 4 for 'MatMul_1' (op: 'MatMul') with input shapes: [2,3], [4,2].
    
    During handling of the above exception, another exception occurred:
    
    ValueError                                Traceback (most recent call last)
    <ipython-input-12-55d23e300d66> in <module>()
          7 # Raises a ValueError, because `c` and `d` do not have compatible
          8 # inner dimensions.
    ----> 9 e = tf.matmul(c, d)
         10 
         11 f = tf.matmul(c, d, transpose_a=True, transpose_b=True)
    
    ~\Anaconda3\lib\site-packages\tensorflow\python\ops\math_ops.py in matmul(a, b, transpose_a, transpose_b, adjoint_a, adjoint_b, a_is_sparse, b_is_sparse, name)
       2055     else:
       2056       return gen_math_ops.mat_mul(
    -> 2057           a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
       2058 
       2059 
    
    ~\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_math_ops.py in mat_mul(a, b, transpose_a, transpose_b, name)
       4854     _, _, _op = _op_def_lib._apply_op_helper(
       4855         "MatMul", a=a, b=b, transpose_a=transpose_a, transpose_b=transpose_b,
    -> 4856         name=name)
       4857     _result = _op.outputs[:]
       4858     _inputs_flat = _op.inputs
    
    ~\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
        785         op = g.create_op(op_type_name, inputs, output_types, name=scope,
        786                          input_types=input_types, attrs=attr_protos,
    --> 787                          op_def=op_def)
        788       return output_structure, op_def.is_stateful, op
        789 
    
    ~\Anaconda3\lib\site-packages\tensorflow\python\util\deprecation.py in new_func(*args, **kwargs)
        486                 'in a future version' if date is None else ('after %s' % date),
        487                 instructions)
    --> 488       return func(*args, **kwargs)
        489     return tf_decorator.make_decorator(func, new_func, 'deprecated',
        490                                        _add_deprecated_arg_notice_to_docstring(
    
    ~\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in create_op(***failed resolving arguments***)
       3272           input_types=input_types,
       3273           original_op=self._default_original_op,
    -> 3274           op_def=op_def)
       3275       self._create_op_helper(ret, compute_device=compute_device)
       3276     return ret
    
    ~\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in __init__(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
       1790           op_def, inputs, node_def.attr)
       1791       self._c_op = _create_c_op(self._graph, node_def, grouped_inputs,
    -> 1792                                 control_input_ops)
       1793 
       1794     # Initialize self._outputs.
    
    ~\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
       1629   except errors.InvalidArgumentError as e:
       1630     # Convert to ValueError for backwards compatibility.
    -> 1631     raise ValueError(str(e))
       1632 
       1633   return c_op
    
    ValueError: Dimensions must be equal, but are 3 and 4 for 'MatMul_1' (op: 'MatMul') with input shapes: [2,3], [4,2].

    在某些情况下,推断的形状可能有未知的尺寸。如果调用方有关于这些维度值的其他信息,则可以使用Tensor.set form()来增强推断的形状。返回:表示此张量形状的TensorShape。

    tf.Tensor.set_shape(shape) Updates the shape of this tensor.

    This method can be called multiple times, and will merge the given shape with the current shape of this tensor. It can be used to provide additional information about the shape of this tensor that cannot be inferred from the graph alone. For example, this can be used to provide additional information about the shapes of images:

    _, image_data = tf.TFRecordReader(...).read(...) image = tf.image.decode_png(image_data, channels=3)

    The height and width dimensions of image are data dependent, and

    cannot be computed without executing the op.

    print image.get_shape() ==> TensorShape([Dimension(None), Dimension(None), Dimension(3)])

    We know that each image in this dataset is 28 x 28 pixels.

    image.set_shape([28, 28, 3]) print image.get_shape() ==> TensorShape([Dimension(28), Dimension(28), Dimension(3)]) Args: shape: A TensorShape representing the shape of this tensor. Raises: ValueError: If shape is not compatible with the current shape of this tensor. Other Methods tf.Tensor.init(op, value_index, dtype) Creates a new Tensor.

    Args: op: An Operation. Operation that computes this tensor. value_index: An int. Index of the operation's endpoint that produces this tensor. dtype: A types.DType. Type of data stored in this tensor. Raises: TypeError: If the op is not an Operation.

    - tf.Tensor.device The name of the device on which this tensor will be produced, or None.

    Tensor types

    class tf.DType Represents the type of the elements in a Tensor.

    The following DType objects are defined:

    tf.float32: 32-bit single-precision floating-point. tf.float64: 64-bit double-precision floating-point. tf.bfloat16: 16-bit truncated floating-point. tf.complex64: 64-bit single-precision complex.

    tf.int8: 8-bit signed integer. tf.uint8: 8-bit unsigned integer. tf.int32: 32-bit signed integer. tf.int64: 64-bit signed integer.

    tf.bool: Boolean.

    tf.string: String.

    tf.qint8: Quantized 8-bit signed integer. tf.quint8: Quantized 8-bit unsigned integer. tf.qint32: Quantized 32-bit signed integer. In addition, variants of these types with the _ref suffix are defined for reference-typed tensors.

    The tf.as_dtype() function converts numpy types and string type names to a DType object.

    tf.DType.is_compatible_with(other) Returns True if the other DType will be converted to this DType.

    The conversion rules are as follows:

    DType(T) .is_compatible_with(DType(T)) == True DType(T) .is_compatible_with(DType(T).as_ref) == True DType(T).as_ref.is_compatible_with(DType(T)) == False DType(T).as_ref.is_compatible_with(DType(T).as_ref) == True Args: other: A DType (or object that may be converted to a DType). Returns: True if a Tensor of the other DType will be implicitly converted to this DType.

    tf.DType.name Returns the string name for this DType.

    tf.DType.base_dtype Returns a non-reference DType based on this DType.

    tf.DType.is_ref_dtype Returns True if this DType represents a reference type.

    tf.DType.as_ref Returns a reference DType based on this DType.

    tf.DType.is_integer Returns whether this is a (non-quantized) integer type.

    tf.DType.is_quantized Returns whether this is a quantized data type.

    tf.DType.as_numpy_dtype Returns a numpy.dtype based on this DType.

    tf.DType.as_datatype_enum Returns a types_pb2.DataType enum value based on this DType.

    Other Methods

    In [ ]:

    tf.DType.__init__(type_enum)
    Creates a new DataType.
    
    NOTE(mrry): In normal circumstances, you should not need to construct a DataType object directly. Instead, use the types.as_dtype() function.
    
    Args:
    type_enum: A types_pb2.DataType enum value.
    Raises:
    TypeError: If type_enum is not a value types_pb2.DataType.
    
    -
    tf.DType.max
    Returns the maximum representable value in this data type.
    
    Raises:
    TypeError: if this is a non-numeric, unordered, or quantized type.
    
    -
    tf.DType.min
    Returns the minimum representable value in this data type.
    
    Raises:
    TypeError: if this is a non-numeric, unordered, or quantized type.
    
    -
    tf.as_dtype(type_value)
    Converts the given type_value to a DType.
    
    Args:
    type_value: A value that can be converted to a tf.DType object. This may currently be a tf.DType object, a DataType enum, a string type name, or a numpy.dtype.
    Returns:
    A DType corresponding to type_value.
    
    Raises:
    TypeError: If type_value cannot be converted to a DType.
    Utility functions
    tf.device(dev)
    Wrapper for Graph.device() using the default graph.
    
    See Graph.name_scope() for more details.
    
    Args:
    device_name_or_function: The device name or function to use in the context.
    Returns:
    A context manager that specifies the default device to use for newly created ops.
    
    tf.name_scope(name)
    Wrapper for Graph.name_scope() using the default graph.
    
    See Graph.name_scope() for more details.
    
    Args:
    name: A name for the scope.
    Returns:
    A context manager that installs name as a new name scope in the default graph.
    
    tf.control_dependencies(control_inputs)
    Wrapper for Graph.control_dependencies() using the default graph.
    
    See Graph.control_dependencies() for more details.
    
    Args:
    control_inputs: A list of Operation or Tensor objects, which must be executed or computed before running the operations defined in the context.
    Returns:
    A context manager that specifies control dependencies for all operations constructed within the context.
    
    tf.convert_to_tensor(value, dtype=None, name=None)
    Converts the given value to a Tensor.
    
    This function converts Python objects of various types to Tensor objects. It accepts Tensor objects, numpy arrays, Python lists, and Python scalars. For example:
    

    In [20]:

    import numpy as np
    array = np.random.rand(32,100,100)
    print(array.shape)
    def my_func(arg):
        arg = tf.convert_to_tensor(arg,dtype=tf.float32)
        return tf.matmul(arg,arg) +arg
    
    value_1 = my_func(tf.constant([[1.0,2.0],[3.0,4.0]]))
    value_2 = my_func([[1.0, 2.0], [3.0, 4.0]])
    value_3 = my_func(np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32))
    print(value_1)
    print(value_2)
    print(value_3)
    res =sess.run(value_1)
    print(res)
    
    (32, 100, 100)
    Tensor("add_3:0", shape=(2, 2), dtype=float32)
    Tensor("add_4:0", shape=(2, 2), dtype=float32)
    Tensor("add_5:0", shape=(2, 2), dtype=float32)
    [[ 8. 12.]
     [18. 26.]]
    

    In [ ]:

    This function can be useful when composing a new operation in Python (such as my_func in the example above). All standard Python op constructors apply this function to each of their Tensor-valued inputs, which allows those ops to accept numpy arrays, Python lists, and scalars in addition to Tensor objects.
    
    Args:
    value: An object whose type has a registered Tensor conversion function.
    dtype: Optional element type for the returned tensor. If missing, the type is inferred from the type of value.
    name: Optional name to use if a new Tensor is created.
    Returns:
    A Tensor based on value.
    
    Raises:
    TypeError: If no conversion function is registered for value.
    RuntimeError: If a registered conversion function returns an invalid value.
    
    -
    tf.get_default_graph()
    Returns the default graph for the current thread.
    
    The returned graph will be the innermost graph on which a Graph.as_default() context has been entered, or a global default graph if none has been explicitly created.
    
    N.B. The default graph is a property of the current thread. If you create a new thread, and wish to use the default graph in that thread, you must explicitly add a with g.as_default(): in that thread's function.
    
    Returns:
    The default Graph being used in the current thread.
    
    tf.import_graph_def(graph_def, input_map=None, return_elements=None, name=None, op_dict=None)
    Imports the TensorFlow graph in graph_def into the Python Graph.
    
    This function provides a way to import a serialized TensorFlow GraphDef protocol buffer, and extract individual objects in the GraphDef as Tensor and Operation objects. See Graph.as_graph_def() for a way to create a GraphDef proto.
    
    Args:
    graph_def: A GraphDef proto containing operations to be imported into the default graph.
    input_map: A dictionary mapping input names (as strings) in graph_def to Tensor objects. The values of the named input tensors in the imported graph will be re-mapped to the respective Tensor values.
    return_elements: A list of strings containing operation names in graph_def that will be returned as Operation objects; and/or tensor names in graph_def that will be returned as Tensor objects.
    name: (Optional.) A prefix that will be prepended to the names in graph_def. Defaults to "import".
    op_dict: (Optional.) A dictionary mapping op type names to OpDef protos. Must contain an OpDef proto for each op type named in graph_def. If omitted, uses the OpDef protos registered in the global registry.
    Returns:
    A list of Operation and/or Tensor objects from the imported graph, corresponding to the names in `return_elements'.
    
    Raises:
    TypeError: If graph_def is not a GraphDef proto, input_map' is not a dictionary mapping strings toTensorobjects, orreturn_elements` is not a list of strings.
    ValueError: If input_map, or return_elements contains names that do not appear in graph_def, or graph_def is not well-formed (e.g. it refers to an unknown tensor).
    Graph collections
    tf.add_to_collection(name, value)
    Wrapper for Graph.add_to_collection() using the default graph.
    
    See Graph.add_to_collection() for more details.
    
    Args:
    name: The key for the collection. For example, the GraphKeys class contains many standard names for collections.
    value: The value to add to the collection.
    
    -
    tf.get_collection(key, scope=None)
    Wrapper for Graph.get_collection() using the default graph.
    
    See Graph.get_collection() for more details.
    
    Args:
    key: The key for the collection. For example, the GraphKeys class contains many standard names for collections.
    scope: (Optional.) If supplied, the resulting list is filtered to include only items whose name begins with this string.
    Returns:
    The list of values in the collection with the given name, or an empty list if no value has been added to that collection. The list contains the values in the order under which they were collected.
    
    class tf.GraphKeys
    Standard names to use for graph collections.
    
    The standard library uses various well-known names to collect and retrieve values associated with a graph. For example, the tf.Optimizer subclasses default to optimizing the variables collected under tf.GraphKeys.TRAINABLE_VARIABLES if none is specified, but it is also possible to pass an explicit list of variables.
    
    The following standard keys are defined:
    
    VARIABLES: the Variable objects that comprise a model, and must be saved and restored together. See tf.all_variables() for more details.
    TRAINABLE_VARIABLES: the subset of Variable objects that will be trained by an optimizer. See tf.trainable_variables() for more details.
    SUMMARIES: the summary Tensor objects that have been created in the graph. See tf.merge_all_summaries() for more details.
    QUEUE_RUNNERS: the QueueRunner objects that are used to produce input for a computation. See tf.start_queue_runners() for more details.
    Defining new operations
    class tf.RegisterGradient
    A decorator for registering the gradient function for an op type.
    
    This decorator is only used when defining a new op type. For an op with m inputs and n inputs, the gradient function is a function that takes the original Operation and n Tensor objects (representing the gradients with respect to each output of the op), and returns m Tensor objects (representing the partial gradients with respect to each input of the op).
    
    For example, assuming that operations of type "Sub" take two inputs x and y, and return a single output x - y, the following gradient function would be registered:
    
    @tf.RegisterGradient("Sub")
    def _sub_grad(unused_op, grad):
      return grad, tf.Neg(grad)
    The decorator argument op_type is the string type of an operation. This corresponds to the OpDef.name field for the proto that defines the operation.
    
    tf.RegisterGradient.__init__(op_type)
    Creates a new decorator with op_type as the Operation type.
    
    Args:
    op_type: The string type of an operation. This corresponds to the OpDef.name field for the proto that defines the operation.
    
    -
    tf.NoGradient(op_type)
    Specifies that ops of type op_type do not have a defined gradient.
    
    This function is only used when defining a new op type. It may be used for ops such as tf.size() that are not differentiable. For example:
    
    tf.NoGradient("Size")
    Args:
    op_type: The string type of an operation. This corresponds to the OpDef.name field for the proto that defines the operation.
    Raises:
    TypeError: If op_type is not a string.
    
    -
    class tf.RegisterShape
    A decorator for registering the shape function for an op type.
    
    This decorator is only used when defining a new op type. A shape function is a function from an Operation object to a list of TensorShape objects, with one TensorShape for each output of the operation.
    
    For example, assuming that operations of type "Sub" take two inputs x and y, and return a single output x - y, all with the same shape, the following shape function would be registered:
    
    @tf.RegisterShape("Sub")
    def _sub_shape(op):
      return [op.inputs[0].get_shape().merge_with(op.inputs[1].get_shape())]
    The decorator argument op_type is the string type of an operation. This corresponds to the OpDef.name field for the proto that defines the operation.
    
    tf.RegisterShape.__init__(op_type)
    Saves the "op_type" as the Operation type.
    
    class tf.TensorShape
    Represents the shape of a Tensor.
    
    A TensorShape represents a possibly-partial shape specification for a Tensor. It may be one of the following:
    
    Fully-known shape: has a known number of dimensions and a known size for each dimension.
    Partially-known shape: has a known number of dimensions, and an unknown size for one or more dimension.
    Unknown shape: has an unknown number of dimensions, and an unknown size in all dimensions.
    If a tensor is produced by an operation of type "Foo", its shape may be inferred if there is a registered shape function for "Foo". See tf.RegisterShape() for details of shape functions and how to register them. Alternatively, the shape may be set explicitly using Tensor.set_shape().
    
    tf.TensorShape.merge_with(other)
    Returns a TensorShape combining the information in self and other.
    
    The dimensions in self and other are merged elementwise, according to the rules defined for Dimension.merge_with().
    
    Args:
    other: Another TensorShape.
    Returns:
    A TensorShape containing the combined information of self and other.
    
    Raises:
    ValueError: If self and other are not compatible.
    
    -
    tf.TensorShape.concatenate(other)
    Returns the concatenation of the dimension in self and other.
    
    N.B. If either self or other is completely unknown, concatenation will discard information about the other shape. In future, we might support concatenation that preserves this information for use with slicing.
    
    Args:
    other: Another TensorShape.
    Returns:
    A TensorShape whose dimensions are the concatenation of the dimensions in self and other.
    
    tf.TensorShape.ndims
    Returns the rank of this shape, or None if it is unspecified.
    
    tf.TensorShape.dims
    Returns a list of Dimensions, or None if the shape is unspecified.
    
    tf.TensorShape.as_list()
    Returns a list of integers or None for each dimension.
    
    tf.TensorShape.is_compatible_with(other)
    Returns True iff self is compatible with other.
    
    Two possibly-partially-defined shapes are compatible if there exists a fully-defined shape that both shapes can represent. Thus, compatibility allows the shape inference code to reason about partially-defined shapes. For example:
    
    TensorShape(None) is compatible with all shapes.
    
    TensorShape([None, None]) is compatible with all two-dimensional shapes, such as TensorShape([32, 784]), and also TensorShape(None). It is not compatible with, for example, TensorShape([None]) or TensorShape([None, None, None]).
    
    TensorShape([32, None]) is compatible with all two-dimensional shapes with size 32 in the 0th dimension, and also TensorShape([None, None]) and TensorShape(None). It is not compatible with, for example, TensorShape([32]), TensorShape([32, None, 1]) or TensorShape([64, None]).
    
    TensorShape([32, 784]) is compatible with itself, and also TensorShape([32, None]), TensorShape([None, 784]), TensorShape([None, None]) and TensorShape(None). It is not compatible with, for example, TensorShape([32, 1, 784]) or TensorShape([None]).
    The compatibility relation is reflexive and symmetric, but not transitive. For example, TensorShape([32, 784]) is compatible with TensorShape(None), and TensorShape(None) is compatible with TensorShape([4, 4]), but TensorShape([32, 784]) is not compatible with TensorShape([4, 4]).
    
    Args:
    other: Another TensorShape.
    Returns:
    True iff self is compatible with other.
    
    tf.TensorShape.is_fully_defined()
    Returns True iff self is fully defined in every dimension.
    
    tf.TensorShape.with_rank(rank)
    Returns a shape based on self with the given rank.
    
    This method promotes a completely unknown shape to one with a known rank.
    
    Args:
    rank: An integer.
    Returns:
    A shape that is at least as specific as self with the given rank.
    
    Raises:
    ValueError: If self does not represent a shape with the given rank.
    
    -
    tf.TensorShape.with_rank_at_least(rank)
    Returns a shape based on self with at least the given rank.
    
    Args:
    rank: An integer.
    Returns:
    A shape that is at least as specific as self with at least the given rank.
    
    Raises:
    ValueError: If self does not represent a shape with at least the given rank.
    
    -
    tf.TensorShape.with_rank_at_most(rank)
    Returns a shape based on self with at most the given rank.
    
    Args:
    rank: An integer.
    Returns:
    A shape that is at least as specific as self with at most the given rank.
    
    Raises:
    ValueError: If self does not represent a shape with at most the given rank.
    
    -
    tf.TensorShape.assert_has_rank(rank)
    Raises an exception if self is not compatible with the given rank.
    
    Args:
    rank: An integer.
    Raises:
    ValueError: If self does not represent a shape with the given rank.
    
    -
    tf.TensorShape.assert_same_rank(other)
    Raises an exception if self and other do not have compatible ranks.
    
    Args:
    other: Another TensorShape.
    Raises:
    ValueError: If self and other do not represent shapes with the same rank.
    
    -
    tf.TensorShape.assert_is_compatible_with(other)
    Raises exception if self and other do not represent the same shape.
    
    This method can be used to assert that there exists a shape that both self and other represent.
    
    Args:
    other: Another TensorShape.
    Raises:
    ValueError: If self and other do not represent the same shape.
    
    -
    tf.TensorShape.assert_is_fully_defined()
    Raises an exception if self is not fully defined in every dimension.
    
    Raises:
    ValueError: If self does not have a known value for every dimension.
    Other Methods
    tf.TensorShape.__init__(dims)
    Creates a new TensorShape with the given dimensions.
    
    Args:
    dims: A list of Dimensions, or None if the shape is unspecified.
    DEPRECATED: A single integer is treated as a singleton list.
    
    -
    tf.TensorShape.as_dimension_list()
    DEPRECATED: use as_list().
    
    tf.TensorShape.num_elements()
    Returns the total number of elements, or none for incomplete shapes.
    
    class tf.Dimension
    Represents the value of one dimension in a TensorShape.
    
    tf.Dimension.__init__(value)
    Creates a new Dimension with the given value.
    
    tf.Dimension.assert_is_compatible_with(other)
    Raises an exception if other is not compatible with this Dimension.
    
    Args:
    other: Another Dimension.
    Raises:
    ValueError: If self and other are not compatible (see is_compatible_with).
    
    -
    tf.Dimension.is_compatible_with(other)
    Returns true if other is compatible with this Dimension.
    
    Two known Dimensions are compatible if they have the same value. An unknown Dimension is compatible with all other Dimensions.
    
    Args:
    other: Another Dimension.
    Returns:
    True if this Dimension and other are compatible.
    
    tf.Dimension.merge_with(other)
    Returns a Dimension that combines the information in self and other.
    
    Dimensions are combined as follows:
    
    Dimension(n) .merge_with(Dimension(n)) == Dimension(n) Dimension(n) .merge_with(Dimension(None)) == Dimension(n) Dimension(None).merge_with(Dimension(n)) == Dimension(n) Dimension(None).merge_with(Dimension(None)) == Dimension(None) Dimension(n) .merge_with(Dimension(m)) raises ValueError for n != m
    
    Args:
    other: Another Dimension.
    Returns:
    A Dimension containing the combined information of self and other.
    
    Raises:
    ValueError: If self and other are not compatible (see is_compatible_with).
    
    -
    tf.Dimension.value
    The value of this dimension, or None if it is unknown.
    
    tf.op_scope(values, name, default_name)
    Returns a context manager for use when defining a Python op.
    
    This context manager validates that the given values are from the same graph, ensures that that graph is the default graph, and pushes a name scope.
    
    For example, to define a new Python op called my_op:
    
    def my_op(a, b, c, name=None):
      with tf.op_scope([a, b, c], name, "MyOp") as scope:
        a = tf.convert_to_tensor(a, name="a")
        b = tf.convert_to_tensor(b, name="b")
        c = tf.convert_to_tensor(c, name="c")
        # Define some computation that uses `a`, `b`, and `c`.
        return foo_op(..., name=scope)
    Args:
    values: The list of Tensor arguments that are passed to the op function.
    name: The name argument that is passed to the op function.
    default_name: The default name to use if the name argument is None.
    Returns:
    A context manager for use in defining a Python op.
    
    tf.get_seed(op_seed)
    Returns the local seeds an operation should use given an op-specific seed.
    
    Given operation-specific seed, op_seed, this helper function returns two seeds derived from graph-level and op-level seeds. Many random operations internally use the two seeds to allow user to change the seed globally for a graph, or for only specific operations.
    
    For details on how the graph-level seed interacts with op seeds, see set_random_seed.
    
    Args:
    op_seed: integer.
    Returns:
    A tuple of two integers that should be used for the local seed of this operation.
    展开全文
  • 数据结构图类graph()

    千次阅读 2018-12-26 23:10:26
    前言:利用图类数据结构,可以运用到GPS,google地图等需要计算最短路径的算fa中。 1,一个图G = (V, E)由以下元素组成。  V:一组顶点  E:一组边,连接V中的顶点  2,图的表示:邻接矩阵,邻接表,关联...

    前言:利用图类数据结构,可以运用到GPS,google地图等需要计算最短路径的算fa中。

    1,一个图G = (V, E)由以下元素组成。

     V:一组顶点 

    E:一组边,连接V中的顶点 

    2,图的表示:邻接矩阵,邻接表,关联矩阵。

    邻接矩阵: array[A][B]==1,两点相邻为1;

    邻接表:列出所有的点,和每个点相邻的写在这个点旁边;

     

    关联矩阵:行表示顶点,列表示边。

    3,创建图类:使用数组(点)和字典(邻接表【顶点(键),邻接点(值)】)

    function Graph(){
    var   vertices=[ ];

    var adjList=new dictionary();

    }

    添加顶点和带键邻接表:

    this.addVertax=function(v){
    vertices.push(v);

    adjList.set(v,  [  ]);

    }

    添加邻接表值:

    this.addEdge=function(v,w){
    adjList.get(v).push(w);

    adjList.get(w).push(v);

    }

    将图表化作可视的字符串

    this.tostring=function(){
    var s=' ';

    for(var i=0;i<vertices.length;i++){
    s+=vertices[i]+'->';

    var neighbors=adjList.get(vertices[i]);

    for(var j=0;j<neighbors.length;j++){

    s+=neighbors[i]+' ';

    }

    s+='\n';

    }return s;}

    总体合并代码:

    var graph=function(key,val,v,b){

    var dictionary=function(key,val){
      var node={ };
      this.putin=function(key,val){
      node[key]=val;
      }
      this.getout=function(key){
        return node[key];
    }
    }

    var  point=[ ];
    var  diction=new dictionary();

    this.setpoint=function(v){
    point.push(v);          
    diction.putin(v,[]);
    }

    this.setedge=function(v,b){
      diction.getout(v).push(b);  //获取字典对应的点键值,设置此点的边
      diction.getout(b).push(v);
    }

    this.tostring=function(){
    var x1=point.length;
    for(var i=0;i<x1;i++){
       console.log(point[i]+"->");
       for(var j=0;j<diction.getout(point[i]).length;j++)
       console.log(diction.getout(point[i])[j]);
       
    }
    }

    }  //graph;

    var  m=new graph();
    m.setpoint('A');
    m.setpoint('B');
    m.setedge('A','B');
    m.setpoint('C');
    m.setedge('A','C');
    m.tostring();
     

    4,  图的遍历:广度优先搜索,深度优先搜索;

    遍历的中心思想:图的顶点已经被我们存储到私有的属性vertices[ ]数组中了;初始化,将每个点设置成同一种颜色,然后指定一个顶点开始访问,为了避免重复访问,根据点的颜色判断是否继续访问这个点;

    被访问:已经将此顶点加入数组,没有将其邻接顶点加入字典;

    被探索过:已经将此顶点加入数组,并且将其邻接顶点加入了字典;

    用三种颜色表示它们的状态:

    白色:表示该顶点还没有被访问。 

    灰色:表示该顶点被访问过,但并未被探索过。

    黑色:表示该顶点被访问过且被完全探索过。

    广度优先搜索:将点存储到队列中

     

    -------------------------------------------------广度优先搜索-----------------------------------------------------

    广度优先搜索:

    创建一个颜色数组,顶点名作为color[ A ]参数,因为之前将图存储于字典中,遍历字典将所有的顶点,color[ ]都设置为白色。

    ------------------------------------------------------------------------------------------------------------------------ 

    申请一个队列,将一个顶点压入队列,赋予变量u值为队列的第一个顶点且将这个顶点弹出队列,把颜色搞成灰色,从字典遍历这个顶点的邻接点,若邻接点中有白色的,则将这个点搞成灰色的,并将这个点压入队列中,遍历完毕将第一个顶点改成黑色。继续将队列的第一个点弹出栈,赋给变量u......

    var initializeColor=function(){
    var color=[ ];

    for(var i=0;i<vertices.length;i++){

    color[vertices[i ]]='white';

    }

    return color;}

    -------------------------------------------------------------------------------------------------------

    this.bfs=function(v, callback){
    var  color=initializeColor();

    var queue=new queue();            //申请队列(先进先出)

    queue.enqueue(v);                 //压入第一个点v

    while(!queue.isEmpty()){
    var u=queue.dequeue();          //弹出队列中第一个点

    var neighbors=adjList.get(u); 

    for(var i=0;i<neightbors.length;i++){
    var w=neightbors[i];

    if(color[w]=="white"){          //如果是白色
    color[w]=="grey";

    queue.enqueue(w);         //将邻接点全部压入队列

    }

    color[u]="black";

    }

    if(callback){

    callback(v);

    }  }

    };

    callback函数可以自己定义功能:

    function printNode(value){

    console.log('Visited vertex: ' + value);

    graph.bfs(myVertices[0], printNode);  //测试

    -----------------------------------------------------------------------------------------------------

    广度优先搜索的使用:使用BFS寻找最短路径 (给定一个图G和源顶点v,找出对每个顶点u,u和v之间最短路径的距离)

    this.BFS=function (v){

    var color=initializeColor();

    var queue=new queue;

    queue.enqueue(v);

    var d=[ ];  //定义用于盛放距离的数组

    var pred=[ ];   //定义用于盛放每个字母前一个字母发数组

    for(var i=0;i<vertices.length;i++){

    d[vertices[i]]=0;     //初始化都为0

    pred[vertices[i]]=null;

    }

    while(!queue.isEmpty()){

    var u=queue.dequeue();

    color[u]="gray";

    var neightbors=adjList.get(u);

    for(var i=0;i<neightbors.length;i++){

    var w=neightbors[i];

    if(color[w]=="white"){
    color[w]="grey";

    queue.enqueue(w);

    d[w]=d[u]+1;   //每增加一个点距离加1;

    pred[w]=u;

    }

    color[u]="black";

    }}

    return {          //返回数组

    distance: d,

    predecessors:pred

    };

    }

    调用一下函数:

    var myVertices = ['A','B','C','D','E','F','G','H','I'];

    var shortestPathA = graph.BFS(myVertices[0]);

    console.log(shortestPathA); 

    理论上将输出:

    distances: [A: 0, B: 1, C: 1, D: 1, E: 2, F: 2, G: 2, H: 2 , I: 3],

    predecessors: [A: null, B: "A", C: "A", D: "A", E: "B", F: "B", G:"C", H: "D", I: "E"]   

    我还想知道顶点A到其他点的最短路径(并且让它打印出来):

    将预定义的数组里的点用for循环遍历,每个点被遍历的时候,都要申请一个栈,将这个点的前一个点按顺序放到栈里,(前面一个函数已经打印出了predecessors[ ]数组,盛放的就是每个点的前一个点的啥名字),放完了就让它们弹出pop栈,因为栈是先进后出,所以第一个被放入栈的终点将会最后一个弹出,在弹出的时候,用字符串拼接起来就行了。

    var  forvertex=myVertices[0];

    for(var i=1;i<myVertices.length;i++){

    var path=new stack();    

    for(var v=myVertices[i];v!=myVertices[0];v=shortestPathA.predecessors[v]){   //遍历到点D时,不停的寻找点D的前一个点

    path.push(v)            // 将这些点按顺序压入栈

    }

    var s=forvertex;

    while(!path.isEmpty()){
    s+="-" +path.pop();    //将这些点按顺序弹出栈,构成路径;

    }

    console.log(s);

    }

     

     

     

      

     

     

     

     


     

     

     

     

     

     

     

     

     

     

    展开全文
  • 图网络模型原理详解(Graph Network)

    万次阅读 多人点赞 2019-01-25 17:34:08
    《Relational inductive biases, deep learning, and graph networks》 这篇论文包含了一部分新研究、一部分回顾和部分统一结论,这篇文章涉及到的很多知识面,涉及到联结主义、行为主义、符号主义,其本身的模型并...

    《Relational inductive biases, deep learning, and graph networks》

    这篇论文包含了一部分新研究、一部分回顾和部分统一结论,这篇文章涉及到的很多知识面,涉及到联结主义、行为主义、符号主义,其本身的模型并不难,但是要理解其提出来的背景及相关知识联系,和其研究的深层含义就需要慢慢品读其中的参考文献了。

    这篇论文是DeepMind联合谷歌大脑、MIT等机构27位作者发表重磅论文,提出“图网络”(Graph network),将端到端学习与归纳推理相结合,有望解决深度学习无法进行关系推理的问题。

    作者认为组合泛化是人工智能实现与人类相似能力的首要任务,而结构化表示和计算是实现这一目标的关键,实现这个目标的关键是结构化的表示数据和计算(structured representation)。本文讨论图网络如何支持关系推理和组合泛化,为更复杂的、可解释的和灵活的推理模式奠定基础。

    人类智能的一个关键特征是“无限使用有限方法”的能力,也就是说我们一旦掌握了一种方法,我们可以无限次将这种方法应用到对应的场景中解决对应的问题,比如:我们学习说话并不是将世界上所有的句子都学一遍,我们学习的是单个的单词还有语法,然后我们可以将单词经过特定的语法组织,组成很多很多句子。这反映了组合归纳的原则,即从已知的构建块构造新的推论、预测和行为。人类的组合概括能力主要取决于我们表达结构和推理关系的认知机制。我们将复杂系统表示为实体及其相互作用的组合,比如判断一堆不规则的石头堆砌起来是否稳定。人类的认知可以通过层层抽象来去掉事物间细节上的差异,保留下最一般的共同点;所以我们可以通过组合已有的技能和经验来解决新的问题(例如去一个新地方可以将“坐飞机”、"去圣迭戈"、“吃饭”、“在一家印度餐馆”这几个已有的概念组合起来得以实现。);人类可以将两个关系结构(指的就是graph这种表示物体和物体间关系的数据)放在一起进行比较,然后根据对其中一个的知识,来对另一个进行推断。

    当前的深度学习方法都强调end-to-end,端到端的学习方式,而当数据量和计算资源没有现在这么丰富的时候,人们都是采取的hand-engineering的方式,手动设计各种模型。作者认为在这两种模型之间的选择不应该走极端,只要一个而不要另一个,应该是end-to-end和hand-engineering相结合来设计模型。

    Relational reasoning

    这里我们将 "结构" 定义为一堆已知的构件快的组合,“结构化表示”主要关注组合(即元素的排列),“结构化计算” 作用对象为元素及元素组成的总体,"关系推理" 涉及操纵实体和关系的结构化表示,并使用规则去组合实体和关系。概念如下所示:

    实体是具有属性的元素,例如具有大小和质量的物理对象。

    关系是实体之间的属性。这里我们关注实体之间的配对关系。

    规则是一种函数,它将实体和关系映射到其他实体和关系,这里我们采用一个或两个参数的规则,并返回一元属性值。
     

    Relational inductive biases

    首先需要解释一下什么是归纳偏置(Inductive biases)

    inductive bias,这个概念顾名思义,就是在归纳推理(induct)的时候的偏向(bias),也就是在模型学习的时候,参数有倾向性的调节成某种状态,模型有倾向性的学习成某一类样子。

    学习是通过观察和与世界互动来理解有用知识的过程,这就涉及到从一个有很多解决方案的空间中寻找一个最优的能很好对数据做出解释的解决方案,当我们采用了某个解决方案我们的回报也是最大的。可能在很多场景下,有很多解决方案的效果是相同的,但是 Inductive Bias 会让学习算法不依赖观测数据优先考虑一个最最最优的方法,因为他有偏向啊。在贝叶斯模型中,归纳偏置表现为先验分布模型的选择和参数的选择,在另外一些场景中,偏置项可能表现为防止过拟合的正则化项,或者这个偏置项的存在纯粹就是因为模型本身的结构就是这样要求的。归纳偏置通常牺牲灵活性以提高样本的复杂性。理想情况下,归纳偏置既可以改善对解决方案的搜索,又不会显着降低性能,并有助于找到以理想方式推广的解决方案;然而,不合适的归纳偏置项会引入太强的约束可能导致最终的效果并不是最优的。

    在深度学习中,实体和关系通常表示为分布式表示,规则可以当做是神经网络函数逼近器; 然而,实体,关系和规则的确切形式因架构而异。也就是说不同的网络架构对应到的实体和关系是不同的,CNN对应着自己的实体和关系,RNN同样也对应着他自己的实体和关系,如下图所示:

    每种不同的网络模型对应的实体、关系、偏置项都不同,例如RNN中实体代表着时间步,关系代表着序列

    标准深度学习模型中的关系归纳偏置

    这里需要说relational inductive biases有别于inductive biases,什么区别我们来看模型

    全连接层:

    输入的是一个向量,输出的是每个向量元素与权重向量点乘之后加上偏置项,经过一个激活函数,例如(ReLU)。在这个网络中,实体是网络中的单元,关系是各个单元连接的权重参数(层i中的所有单元连接到层j中的所有单元),并且规则是由权重和偏差组成的一系列函数来表示。整个模型中没有共享参数,信息之间也没有隔离,每一个单元都可以参与决定任何一个输出单元,和输出是没有关系的。因此,全连接层的relational inductive biases是很弱的,也就是模型并没有在归纳推断的时候偏向于哪个relation,而是一视同仁,每个单元都连接到后一层的单元节点,自由度非常大。

    卷积层:

    CNN中的实体还是单元,或者说是网格元素(例如image中的pixel),relation更加稀疏。相比于全连接网络,这里加入的relational inductive biases是局域性和平移不变性,局域性指的就是卷积核是大小固定的,就那么大,只抓取那么大的区域的信息,远处的信息不考虑;平移不变性指的是rule的复用,即卷积核到哪里都是一样的,同一个卷积核在各个地方进行卷积。到这我们可以看到,rule在深度学习的模型中的含义是如何将某一层的网络信息传递到下一层,而且是具体到权重的值是多少,后文还提到一句话“learning algorithm which find rules for computing their interactions”,也就是说,算法指的是计算框架,例如卷积操作之类的,而rule是学习到的具体的以什么权重进行卷积操作。

    循环神经网络:

    RNN中的entity是每一步的输入和hidden state;relation是每一步的hidden state对前一步hidden state和当前输入的依赖。这里的rule是用hidden state和输入更新hidden state的方式。rule是在每一步都复用的,也就是说这里存在一个relational inductive biases是temporal invariance,时间不变性。

    Computations over sets and graphs

    文章提出set(集合)的概念,集合中的元素是没有顺序的,但是集合中元素的一些特征可以用来指定顺序,比如质量体积等等。因此,用来处理set或graph的深度学习模型应该是在不同的排序方式下都能有同样的结果,也就是对排序不变(invariance to ordering)。

    作者强调处理set模型的 relational inductive biases 并不是因为有什么relation的存在,而恰恰是由于缺乏顺序,比如求太阳系的中心位置,就是把各个行星的位置进行求平均,每一个行星都是对称的。如果使用多层感知机(MLP)来处理这个问题,那么就可能需要大量的训练才能训练出一个求平均的函数,因为多层感知机对每一个输入都是按照单独的方式处理,而不是一视同仁的处理所有输入。但是有的时候需要考虑排序,例如计算solar system里每一个planet的运动,要考虑planet两两之间的相互作用。以上这两个例子其实是两个极端,一个是planet之间两两没关系,另一个是两两都有关系,而现实世界的很多模型是处在这两个极端之间的(如下图),即并不是每两个之间都没有联系,但也并不是两两之间都有联系,这也其实就是graph模型,每一个物体只与一部分其它物体进行相互作用。

    (a)一个分子结构,其中每个原子表示为对应关系的节点和边(Duvenaud 等,2015)。

    (b)一个质量弹簧系统,其中绳索由在图中表示为节点的质量序列定义(Battaglia 等,2016;Chang 等,2017)。

    (c)一个 n 主体系统,其中主体是节点,底层图是完全连接的(Battaglia 等,2016 年;Chang 等,2017)。

    (d)一个精密的主体系统,其中球和壁是节点,底层图形定义球之间以及球和壁之间的相互作用

    (e)一个句子,其中单词对应于树中的叶子,其他节点和边可以由解析器提供。或者,可以使用完全连接的图

    (f)一张图像,可以分解成与完全连接图像中的节点相对应的图像块(Santoro 等,2017;Wang 等,2018)
     

    Graph networks

    作者在自己的模型中没有使用神经元这个词,意在强调这个模型不一定需要通过neural network实现,也可以使用别的函数。graph network的主要计算单元是GN block,GN block是一个输入输出都是graph的graph-to-graph module。在graph中,前面说过的entity就被表示成节点(node),而relation被表示成边(edge),系统层面的特征用global attribute表示。

    节点表示为 v_i,边表示为 e_k,全局属性表示为 u。我们还使用 s_k 和 r_k 分别表示边 k 发送节点和接收节点的索引。

    • 有向边:单向边,从「发送」节点到「接收」节点。
    • 属性:可以编码为向量、集合甚至其他图形的属性。
    • 属性化:边和顶点具有与其关联的属性。
    • 全局属性:图级属性。
    • 多图:顶点之间可以有多个边,包括自边(self-edge)

    graph用一个3元组(3-tuple)表示 G = (u; V; E) (u v e 详见上图)u代表的是一个全局信息(global attribute),是整个graph的一个特征;V代表graph的节点的集合,E代表边的集合代表边和节点的数量,是第k个边的特征(attribute),分别是receiver node和sender node的编号

    计算单元:GN block 包含三个更新函数和三个聚合函数

    左边φ为更新函数,右边ρ为聚合函数 

    是作用于edge的,更新第k个edge的时候,输入参数为  [  当前的特征、连接在这个edge两端的节点的特征,以及全局的特征u  ] 。是对node使用的,输入的是全局的特征u、当前节点的特征、以及, 这个是由公式第一行右边的aggregation function来得到的。是用来更新全局信息的,输入也无需赘述。

    右边的三个函数,每一个函数都有一个上标,这个上标指的是由什么的信息生成什么的信息,例如指的是由edge的信息生成node的信息。这三个函数的输入:

    1. ,这里,rk=i,即所有指向索引为i的节点的有向边,可以看出将要生成的是第i个node的信息,指的是所有的edge,这个函数也就是用所有更新过的edge的信息来生成一个用来产生新的node信息的,然后再由生成最终的新的node信息。

    右边的函数解释通了,左边的函数中的输入变量也就有了,也就是说,整个过程是:

    1. 首先用当前的edge信息、node信息、以及全局信息u来生成新的edge的信息,
    2. 有了新的edge信息,就能生成新的node信息;
    3. 有了新的node和edge信息,就能生成新的全局信息u,整个更新过程也就完成了。

    算法的过程如下图:

    这个算法,输入是graph的三个组成部分:node、edge、u。

    1. 函数在第一个循环中用更新edge的update function更新每一个edge,
    2. 在第二个循环中,更新每个node的特征,
    3. 然后生成,将这两个参数分别进行聚合生成相应的信息
    4. 最后更新u,
    5. 函数最后的返回值就是更新后的edge、node、u。

    算法计算步骤示意图如下:

     这里面,作者强调,函数的输入是一个集合,输出是一个单独的值,这个函数要能做到可以输入不同数量的元素,也就是说输入集合的大小是可变的,而且,输入集合中的元素无论怎么排列,都应该输出同样的信息。即invariant to permutation。这个更新的顺序作者认为不是固定的,倒过来先更新global,再更新node,再更新edge也是无所谓的

    作者给了一个具体的例子来使这个更新过程更加具体,假定我们有一个系统,是由橡胶球以及球体之间相连接的弹簧(并不是任意两个球体之间都有弹簧,而是有些球体之间有弹簧,有些没有)构成。第一步update edge的信息可理解为计算和更新球体两两之间的作用力或者弹性势能之类的量;更新node信息可以理解为是根据这些更新好的势能或作用力等表征相互作用的量来更新球体的位置速度等信息;最后更新全局的信息可能是在计算总势能之类的全局量。在这个例子里,先更新哪个都不重要。

    Relational inductive biases in graph networks

    GN block的relational inductive biases是很强的

    首先,每一个输入的graph的entity之间的relation都是不一定什么样的,也就是说GN的输入决定了实体节点之间是如何相互作用或者相互独立的;两个entity之间有直接的联系,就用一个edge连接以表示;没有直接的联系,就没有相应的edge,这两个entity之间也就不能直接相互作用。有些时候人们并不会因为graph的两个node之间没有连接而不去让他们直接发生相互作用,例如在一篇人体动作识别的paper中,作者将人体关节两两之间都加上edge,来判断动作,这是因为有些关节之间虽然不是生理结构上直接相连的,但是组合起来却能完成一些动作,例如两个手拍手,这也就体现一个问题,graph中原有的edge并不一定体现我们在计算时所需要的相互作用,或者不全面,人体关节是否直接相连跟他们能否共同完成某些动作并不是唯一的决定性的关系,有很多不相邻的关节其实在我们识别动作时应该设置上相互作用的,比如两只手。

    relational inductive biases的第二点表现是graph将entity和entity之间的relation都用set(集合)来表示,这意味着GN对这些元素的顺序的改变要具有不变性。

    第三点更新graph的edge和node的函数是在所有的node和edge上复用的,这说明GN会自动支持组合泛化,这是因为无论graph有多少node,node之间的连接是怎样的,GN都能够处理

    Design principles for graph network architectures

    作者在这里强调,GN是不对attribute representation和函数的形式有什么限制的,但是本文会主要将注意力集中在深度学习的架构上,使得GN成为一个可学习的graph-to-graph的函数近似(function approximator,这个应该是指神经网络都是起到拟合函数的作用,因此训练出来的也就是一个函数的近似)。

    Flexible representations

    GN对graph representation的支持是很灵活的,一方面是global、edge、node的特征的表示都很灵活,可以是向量、tensor、sequence、set,甚至是graph。为了能和其他的深度学习结构对接,往往会将representation选为tensor或vector。此外,对于不同任务的不同需求,GN都能满足,edge-focused GN就输出edge,例如判断entity之间相互作用的任务;node-focused的就输出node,graph-focused的就输出global。另一方面,GN对输入graph的结构也没要求,什么结构都支持。我们得到的数据可能是entity之间所有的relation都已知的,也可能是都未知的,更多的时候介于这两个极端之间。有的时候,entity是怎么设定的也不知道,这时候可能需要假设,例如将句子中的每个词汇都视作一个node(entity)。relation不明确的时候,最简单的方法是假设所有entity之间都有联系,但这种方法在entity数量很大时不太好,因为这时edge数量随node数量平方增长。因此,relation缺失的问题,也就是从没结构的数据中推测出一些稀疏的结构,也是一个很重要的未来研究的方向

    Configurable within-block structure

    GN block内部的结构是可以随意改变的,前面介绍了完整的GN block的工作过程,介绍了里面的信息的传输和使用的函数(函数未指定具体形式),但实际应用中,信息的传递结构和函数都可以随意指定,下图给出了几种形式:

    (a)是一个完整的GN block,其余的是变种。

    (b)是函数设置为RNN的简单的例子,需要额外提供一个hidden state作为输入和输出。这里面没有message-passing,意思是u、v、e之间没有交流,message-passing应该是特指有graph结构的数据的结点等元素之间按照自己的结构进行信息的传递,例如卷积操作,所以这种没有利用结构信息的就算是没有message-passing了。这种使用RNN的GN block可以用来做一些dynamic graph states的smoothing

    (c)是2017年Glimer提出的MPNN模型,

    (d) NLNN,Non-local neural network,是2018年CVPR的一篇文章提出的,这篇文章提出了各种不同的attention机制,包括intra-/self-/vertex-/graph-attention,这些attention的意思都是指的node如何进行更新。

    NLNN模型是不考虑edge的,每两个node之间都会计算一个attention weight,然后加权求和(这也是为什么叫non-local,并不是局部进行卷积,而是全局进行加权),但是一些其他和NLNN类似的工作是只计算有edge相连的节点间的attention weights的,包括Attentional multi-agent predictive modeling(Hoshen 2017)和Graph attention networks。上图其实是将图(d)的拆解开了,每次计算的时候,是一个未经过归一化的权重系数,是一个标量,然后是一个vector,是等待进行加权求和的node的特征经过函数β处理得到的,然后进行卷积的时候就如下述公式进行操作:

    这三个公式的顺序应该是1、3、2,也就是先更新了edge,然后是计算了加权求和的结果,最后用加权求和的结果更新了node。2017年Vaswani(Attention Is All You Need)提出了multi-head self-attention机制,可以并行的同时计算多个attention函数,也就是分别按照不同的权重将近邻节点加权求和,然后将不同权重得到的结果连接起来作为卷积后的结果。这就像是使用了不同类型的edge,通过不同的连接方式进行了卷积。

    接着作者简介一些其他的GN的变种,指出了完整的GN的各个部分根据需要都可以忽略,有的模型忽略了global信息,有的却只需要global信息,不需要的,就算得到了也直接忽略就好。在输入的时候,如果有的信息没有,比如没有edge信息,那么就当成attribute vector的长度为0即可。

    Composable multi-block architectures

    设计graph networks的一个关键原则就是通过将GN block组合起来得到复杂的结构。因为GN block输出和输入一样都是graph,因此内部结构不同的GN block也可以方便的组合起来,就像标准的deep learning模型输入输出都是tensor一样。不同的GN block组合起来可以复用内部的函数,就像展开的RNN一样,并且在合并input graph和hidden state的时候,可以采用LSTM或者GRU一样的gating机制,而不是简单地合并起来。

    图网络通过组合多个GN模块实现复杂结构,组合形式:

     基本的两个GN block的合并方式为具体公式为:一个图G经过GN1的运算的输出作为GN2的输入。

    在上图(a)中,GN block可以是共享的(GN1 = GN2 = · · · = GNM)也可以是不共享的(GN1 ≠ GN2 ≠ · · · ≠ GNM)

    (b)中是encoder-decoder模型,这是一种常用方式,将图输入进行编码获取到中间隐状态表示,输入GNcore中GNcore是一个共享block ,然后解码到输出图。在我们上文提到的小球弹簧系统中,可以这样理解,encoder是计算小球之间初始力和相互作用的能量,core可以理解为一个对系统的动态更新,解码器用于读取系统最终的状态,小球的位置等

    (c)recurrent GN-based architectures类似于RNN,加入了隐藏变量,这种结构适用于预测图序列,比如动态系统在时间序列下的状态轨迹。经过编码后的图必须和结构一致,这样他们才可以连接他们的和u向量,在输出的时候复制一份给然后进行解码输出。在每一步都是共享的 

     如果我们不包含全局信息u,那么某个节点的信息需要m个传播步骤后才能到达另一个我们想到达的节点,所以在不带全局信息情况下,节点可以传播的信息由最多m跳的节点和边缘集确定

    一些关于graph networks编程的问题,由于node和edge的update function都是在所有node和edge复用的,因此它们的更新都是可以并行计算的;此外,多个graph可以看成一个大的graph的不相连的子图,也可以并行起来计算。

    Discussion

    首先讨论一下graph networks的combinatorial generalization,作者说道,GN的结构很自然的会支持组合泛化,因为计算不是在整个系统的宏观层面执行的,计算是在各个entity和relation上复用的,这使得之前从来没见过的系统也能够进行处理

    接着分析了一些GN的局限性,一方面是有些问题无法解决,像是区分一些非同构的graph之类的问题,另一方面是有些问题不能直接用graph来表示,像一些recursion、control flow、conditional iteration之类的概念至少需要一些额外的假设

    深度学习模型可以直接处理原始的感受器得到的信息,但是GN需要的graph如何生成也是个问题,将得到的image,text搞成全连接的graph不是很好,而且直接将这些原始数据的基本元素视作entity也不见得合适,例如参与卷积的像素点并不是图片中的物体,而且实际物体之间的relation可能是稀疏的,如何获取这个稀疏性也是一个问题。因此,能够从原始数据中提取出entity是一个重要的问题

    另一个存在的问题是,在计算的过程中,graph也可能发生变化,例如某一个entity代表的物体可能会破碎,那么代表这个物体的node也应该相应的分离开来,类似的,有些时候物体之间的关系也会发生变化,需要添加或者去除edge,这种适应性的问题也正在被研究,现在已经有一些识别graph的underlying structure(潜在的结构)的算法可能是可以用来进行这个任务的。

    由于GN的行为和人类对世界的理解类似,都是将世界解释成物体和相互关系组成的,因此GN的行为可能会更加容易解释,变得更加易于分析,更容易可视化。探索GN的可解释性也是未来一个有趣的方向。

    虽然本文是将注意力集中在graph上的,但是与此相关的一个方向便是将deep learning和structured representation相结合,相关的工作有很多

     

    参考:https://blog.csdn.net/b224618/article/details/81380567

    展开全文
  • GRAPH --- 图的相关概念整理

    万次阅读 2011-03-04 10:53:00
    Graph更多Graph 的观念与术语被vertex v指到的vertex (vertices),称为v的 successor(s) ;指向v的vertex (vertices),称为v的 predecessor(s) 。 以「通讯录」为例, v的通讯录内所有人,都是v的successors;通讯录...
  • 本文为从CNN到GCN的联系与区别——...根据拉普拉斯矩阵的特征值和特征变量引入基于图Graph的傅里叶变换和卷积 四、Graph上的傅里叶变换和卷积 类比传统的公式,推出基于图Graph的傅里叶变换公式和卷积公式 五...
  • Graph(图基础)

    2018-12-31 20:56:12
    图1.图的概念图的分类图的存储图的遍历 1.图的概念 图是用线或边连接在一起的顶点或节点的集合。 图是顶点集 VVV 和边集 EEE 的有序对 G=(V,E)G=(V,E)G=(V,E) ...部分原代码截取(全代码到GitHub获取)...
  • 提出了一种新的基于注意力的图序列学习神经网络模型Graph2Seq。Graph2Seq模型遵循传统的编解码器方法,包含两个主要组件,一个图形编码器和一个序列解码器。本文提出的图形编码器旨在学习表达性节点嵌入,并将其重新...
  • markdown绘图插件----mermaid简介

    万次阅读 多人点赞 2016-11-13 16:22:25
    作者:黄永刚mermaid简介当撰写文档的时候,对于流程图的生成大多使用Visio等繁重的工具,没有一种轻便的工具能够画图从而简化文档的编写,就像markdown那样。...流程图(flowchart)graph LR;  A-->B;  A-->C;
  • JMeter学习笔记10-Graph Results介绍

    万次阅读 2017-05-01 16:43:52
    本文继续介绍Lisenters下的功能,这里介绍Graph Results. 1. 前提条件,和之前公共部分1到4步骤。 2. 修改Thread Group面板的参数 Number of Threads (users): 20 Ramp-Up period (in seconds): 5 勾选Forever 3. ...
  • RuntimeError:The Session graph is empty.

    万次阅读 2019-08-12 17:57:11
    raise RuntimeError('The Session graph is empty. Add operations to the ' RuntimeError: The Session graph is empty. Add operations to the graph before calling run(). 2 Graph 1. Create a new gr...
  • tensorflow 获取graph中的所有tensor

    万次阅读 2018-07-20 15:44:24
    [n.name for n in tf.get_default_graph().as_graph_def().node] 摘自https://stackoverflow.com/questions/36883949/in-tensorflow-get-the-names-of-all-the-tensors-in-a-graph 
  • tf.get_default_graph()

    万次阅读 2017-11-23 17:19:15
    tf.get_default_graph() 功能:获取当前默认计算图。 例子:import tensorflow as tfa=tf.constant([1.0,2.0],name="a") b=tf.constant([3.0,4.0],name="b") c=a+bprint(c.graph) print(tf.get_default_graph())...
  • 图数据库JanusGraph介绍及使用(一):简介 作者:家辉,日期:2018-07-01 CSDN博客: http://blog.csdn.net/gobitan   说明:这是图数据库JanusGraph系列的第一篇,后面会陆续介绍。 图数据库JanusGraph介绍及...
  • 问题来源: 在Flask接口设计中应用了基于tensorflow的keras框架,由于keras本身的会全部占用gpu显存,因此引入了如下限制机制: import tensorflow as tf from keras.backend.tensorflow_backend import set_...
  • 问题产生的原因:无法执行sess.run()的原因是tensorflow版本不同导致的,tensorflow版本2.0无法兼容版本1.0. 解决办法: tf.compat.v1.disable_eager_execution() import tensorflow as tf tf.compat.v1.disable_...
  • Unity Shader Graph初次尝试

    万次阅读 2020-02-24 09:56:37
    软件环境 Unity Version: 2018.1.2f1 边缘发光材质效果 创建工程 打开Unity并创建一个新工程 ... Package Manager打开包管理器,安装二个依赖包: ...2. Shader Graph Lightweight Render Pipeline ...
  • 问题描述:如何清除每次运行时,tensorflow中不断增加的节点并重置整个defualt graph? import tensorflow as tf # tf.reset_default_graph() with tf.variable_scope('Space_a'): a = tf.constant([1,2,3]) ...
  • Unity 2018之ShaderGraph使用基础

    万次阅读 2018-05-15 10:59:16
    创建shaderGraph 创建轻量级渲染管线 ShaderGraph是基于可编程渲染管线,我们要使用shaderGraph也就需要设置SRP。 首先创建Renderpipeline。在SRP中,unity帮我们封装了几种渲染管线: - 我们可以创建LWRP(轻...
  • ShaderGraph入门

    万次阅读 2018-05-23 08:59:39
    Shader一直是Unity开发者的一个难关,涉及到数学、图形学、shader语法等多个难题。...本文基于Unity2018.1b13版本shadergraph可视化界面配置使用ShaderGraph目前最新版本Unity(2018.1b13)中的Shade...
1 2 3 4 5 ... 20
收藏数 260,623
精华内容 104,249
关键字:

graph