TensorFlow - 将未知大小的张量填充到特定大小? [英] TensorFlow - Pad unknown size tensor to a specific size?

查看:98
本文介绍了TensorFlow - 将未知大小的张量填充到特定大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法用特定的填充值将可变大小的张量填充到给定的形状?例如给定张量:

Is there a way to pad a tensor of variable size to a given shape with a specific pad value? For example given the tensors:

[[1, 2],
 [3, 4]]

[[1, 2, 3],
 [4, 5, 6]]

有没有办法有一个通用的操作,它可以用一个值来填充它们(比如,用值 -1 塑造 [2, 4]) 导致:

Is there a way to have a generic operation which would take either and pad them with a value (say, to shape [2, 4] with value -1) to result in:

[[1, 2, -1, -1],
 [3, 4, -1, -1]]

[[1, 2, 3, -1],
 [4, 5, 6, -1]]

分别是?我的推理(如果有更好的解决方案)是我有来自 TFRecords 文件的示例,其中一部分具有可变长度.对于处理,静态长度使它们更易于使用.

respectively? My reasoning (in case there is a better solution) is that I have examples from a TFRecords file, part of which has a variable length. For processing, a static length makes them easier to work with.

推荐答案

是的.有.只要你不需要改变张量的秩,就很简单了.

Yes. There is. Provided you do not need to change the rank of the tensor, it's very simple.

tf.pad() 接受常规带有张量的 python 列表.填充的格式是该维度的每一侧要填充多少对的列表.

tf.pad() accepts regular python lists with tensors. The format of the padding is a list of pairs of how much to pad on each side of that dimension.

例如

t = tf.constant([[1, 2], [3, 4]])
paddings = [[0, 0], [0, 4-tf.shape(t)[0]]]
out = tf.pad(t, paddings, 'CONSTANT', constant_values=-1)
sess.run(out)
# gives: 
# array([[ 1,  2, -1, -1],
#       [ 3,  4, -1, -1]], dtype=int32)

<小时>

如果你想把它概括为一个有用的函数,你可以这样做:


If you want to generalise this to a useful function, you could do something like:

def pad_up_to(t, max_in_dims, constant_values):
    s = tf.shape(t)
    paddings = [[0, m-s[i]] for (i,m) in enumerate(max_in_dims)]
    return tf.pad(t, paddings, 'CONSTANT', constant_values=constant_values)

其中 max_in_dims 本质上是输出的所需形状.注意:如果您提供的形状在任何维度上都严格小于 t,则此函数将失败.

where max_in_dims is essentially the desired shape of the output. Note: this function will fail if you provide a shape that is strictly smaller than t in any dimension.

你可以像这样使用它:

t = tf.constant([[1, 2], [3, 4]]) # shape = [2, 2]
t_padded = pad_up_to(t, [2, 4], -1) # shape = [2, 4], padded with -1s

t = tf.placeholder(tf.float32, [None, None]) # shape = [?, ?]
t_padded = pad_up_to(t, [5,5], -1) # shape = [5, 5], padded with -1s
t_np = np.random.uniform(0, 1, [3,4]) # shape = [3,4], no padding
t_padded_out = sess.run(t_padded, {t: t_np})
t_np2 = np.random.uniform(0, 1, [2,1]) # shape = [2,1], no padding
t_padded_out2 = sess.run(t_padded, {t: t_np2})

虽然维度大小是动态计算的,但维度的数量不是,所以请确保max_in_dims与t.shape的元素数量相同.

Although the dimension sizes are calculated dynamically, the number of dimensions is not, so make sure that max_in_dims has the same number of elements as t.shape.

这篇关于TensorFlow - 将未知大小的张量填充到特定大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆