Python Tensorflow 张量重塑并用零填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34141430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:30:34  来源:igfitidea点击:

Tensorflow Tensor reshape and pad with zeros

pythonnumpytensorflow

提问by Aidan Gomez

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 flip-flopping between the GPU and CPU.

有没有办法重塑张量并用零填充任何溢出?我知道 ndarray.reshape 会这样做,但据我所知,将 Tensor 转换为 ndarray 需要在 GPU 和 CPU 之间进行触发器。

Tensorflow's reshape() documentation says the TensorShapes need to have the same number of elements, so perhaps the best way would be a pad() and then reshape()?

Tensorflow 的 reshape() 文档说 TensorShapes 需要具有相同数量的元素,所以也许最好的方法是 pad() 然后 reshape()?

I'm trying to achieve:

我正在努力实现:

a = tf.Tensor([[1,2],[3,4]])
tf.reshape(a, [2,3])
a => [[1, 2, 3],
      [4, 0 ,0]]

采纳答案by DMTishler

Tensorflow now offers the pad function which performs padding on a tensor in a number of ways(like opencv2's padding function for arrays): https://www.tensorflow.org/api_docs/python/tf/pad

Tensorflow 现在提供 pad 函数,它以多种方式对张量执行填充(如 opencv2 的数组填充函数):https://www.tensorflow.org/api_docs/python/tf/pad

tf.pad(tensor, paddings, mode='CONSTANT', name=None)

example from the docs above:

上面文档中的示例:

# 't' is [[1, 2, 3], [4, 5, 6]].
# 'paddings' is [[1, 1,], [2, 2]].
# rank of 't' is 2.
pad(t, paddings, "CONSTANT") ==> [[0, 0, 0, 0, 0, 0, 0],
                                  [0, 0, 1, 2, 3, 0, 0],
                                  [0, 0, 4, 5, 6, 0, 0],
                                  [0, 0, 0, 0, 0, 0, 0]]

pad(t, paddings, "REFLECT") ==> [[6, 5, 4, 5, 6, 5, 4],
                                 [3, 2, 1, 2, 3, 2, 1],
                                 [6, 5, 4, 5, 6, 5, 4],
                                 [3, 2, 1, 2, 3, 2, 1]]

pad(t, paddings, "SYMMETRIC") ==> [[2, 1, 1, 2, 3, 3, 2],
                                   [2, 1, 1, 2, 3, 3, 2],
                                   [5, 4, 4, 5, 6, 6, 5],
                                   [5, 4, 4, 5, 6, 6, 5]]

回答by mrry

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:

据我所知,没有内置的运算符可以执行此操作(tf.reshape()如果形状不匹配,则会出现错误)。但是,您可以使用几个不同的运算符获得相同的结果:

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])