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.
是否有一种方法来重塑一个张量,并用0填充任何溢出?我知道ndarray。整形可以做到这一点,但正如我所理解的,将一个张量转换为一个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的rebuild()文档说,TensorShapes需要拥有相同数量的元素,所以最好的方法可能是一个pad(),然后再整形()?
I'm trying to achieve:
我试图达到的目标:
a = tf.Tensor([[1,2],[3,4]])
tf.reshape(a, [2,3])
a => [[1, 2, 3],
[4, 0 ,0]]
2 个解决方案
#1
9
Tensorflow now offers the pad function which performs padding on a tensor in a number of ways(like opencv2's padding function for arrays):
Tensorflow现在提供了pad函数,它可以通过多种方式对张量进行填充(比如opencv2对数组的填充函数):
https://www.tensorflow.org/versions/r0.8/api_docs/python/array_ops.html#pad
https://www.tensorflow.org/versions/r0.8/api_docs/python/array_ops.html垫
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]]
#2
11
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. rebuild()会给您一个错误)。但是,您可以通过几个不同的操作符实现相同的结果:
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])
#1
9
Tensorflow now offers the pad function which performs padding on a tensor in a number of ways(like opencv2's padding function for arrays):
Tensorflow现在提供了pad函数,它可以通过多种方式对张量进行填充(比如opencv2对数组的填充函数):
https://www.tensorflow.org/versions/r0.8/api_docs/python/array_ops.html#pad
https://www.tensorflow.org/versions/r0.8/api_docs/python/array_ops.html垫
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]]
#2
11
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. rebuild()会给您一个错误)。但是,您可以通过几个不同的操作符实现相同的结果:
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])