• tf.reshape()


    1.tf.reshape

    reshape(tensor, shape, name=None)

    作用:重塑张量。给定张量,此操作将返回与形状为shape的张量具有相同值的张量。 如果“形状”的一个分量为特殊值-1,则将计算该尺寸的大小,以使总大小保持恒定。 具体来说,[-1]的“形状”会展平为一维。 “形状”的至多一个分量可以为-1。 如果“ shape”为一维或更高,则该操作将返回一个形状为“ shape”的张量,其中填充了“ tensor”的值。 在这种情况下,“形状”所隐含的元素数量必须与“张量”中的元素数量相同。 

    举例:

    For example:
      ```
      # tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
      # tensor 't' has shape [9]
      reshape(t, [3, 3]) ==> [[1, 2, 3],
                              [4, 5, 6],
                              [7, 8, 9]]
    
      # tensor 't' is [[[1, 1], [2, 2]],
      #                [[3, 3], [4, 4]]]
      # tensor 't' has shape [2, 2, 2]
      reshape(t, [2, 4]) ==> [[1, 1, 2, 2],
                              [3, 3, 4, 4]]
    
      # tensor 't' is [[[1, 1, 1],
      #                 [2, 2, 2]],
      #                [[3, 3, 3],
      #                 [4, 4, 4]],
      #                [[5, 5, 5],
      #                 [6, 6, 6]]]
      # tensor 't' has shape [3, 2, 3]
      # pass '[-1]' to flatten 't'
      reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]
    
      # -1 can also be used to infer the shape
      # -1 is inferred to be 9:
      reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
                               [4, 4, 4, 5, 5, 5, 6, 6, 6]]
    
      # -1 is inferred to be 2:
      reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
                               [4, 4, 4, 5, 5, 5, 6, 6, 6]]
    
      # -1 is inferred to be 3:
      reshape(t, [ 2, -1, 3]) ==> [[[1, 1, 1],
                                    [2, 2, 2],
                                    [3, 3, 3]],
                                   [[4, 4, 4],
                                    [5, 5, 5],
                                    [6, 6, 6]]]
    
      # tensor 't' is [7]
      # shape `[]` reshapes to a scalar
      reshape(t, []) ==> 7
      ```
      Args:
        tensor: A `Tensor`.
        shape: A `Tensor`. Must be one of the following types: `int32`, `int64`.
          Defines the shape of the output tensor.
        name: A name for the operation (optional).
    
      Returns:
        A `Tensor`. Has the same type as `tensor`.
      """
    

    bert中源码:  

    # If the input is a 2D tensor of shape [batch_size, seq_length], we
    # reshape to [batch_size, seq_length, 1].
    if input_ids.shape.ndims == 2:
      input_ids = tf.expand_dims(input_ids, axis=[-1])
    
    embedding_table = tf.get_variable(
          name=word_embedding_name,
          shape=[vocab_size, embedding_size],
          initializer=create_initializer(initializer_range))
    
    flat_input_ids = tf.reshape(input_ids, [-1]) #【batch_size*seq_length*input_num】
  • 相关阅读:
    Hoppz板子
    [cf] Invoking the Magic
    [acm]关于字符的处理
    [acm]排序总结
    [acm]循环队列(不是自己写queue)
    [acm]关于map的一些知识
    [acm]二进制枚举
    P1005 矩阵取数游戏 区间DP
    ICPC North Central NA Contest 2017 (部分)
    灾后重建 弗洛伊德最短路
  • 原文地址:https://www.cnblogs.com/nxf-rabbit75/p/12096601.html
Copyright © 2020-2023  润新知