Tensorflow Tensor reshape and pad with zeros

后端 未结 2 2050
死守一世寂寞
死守一世寂寞 2021-02-07 13:21

Is there a way to reshape a tensor and pad any overflow with zeros? I know ndarray.reshape does this, but as I understand it, converting a Tensor to an ndarray would require fli

2条回答
  •  无人共我
    2021-02-07 14:13

    As far as I know, there's no built-in operator that does this (tf.reshape() will give you an error if the shapes do not match). However, you can achieve the same result with a few different operators:

    a = tf.constant([[1, 2], [3, 4]])
    
    # Reshape `a` as a vector. -1 means "set this dimension automatically".
    a_as_vector = tf.reshape(a, [-1])
    
    # Create another vector containing zeroes to pad `a` to (2 * 3) elements.
    zero_padding = tf.zeros([2 * 3] - tf.shape(a_as_vector), dtype=a.dtype)
    
    # Concatenate `a_as_vector` with the padding.
    a_padded = tf.concat([a_as_vector, zero_padding], 0)
    
    # Reshape the padded vector to the desired shape.
    result = tf.reshape(a_padded, [2, 3])
    

提交回复
热议问题