说一下我理解的tf.pad(),先来看一下定义:
def pad(tensor, paddings, mode="CONSTANT", name=None, constant_values=0):
什么意思呢?目的就是对输入tensor进行扩展,那么扩展的宽度就由paddings来控制了;至于mode和constant_values则表示对tensor扩展时填充的方式。
一维tensor扩展:
import tensorflow as tf
tensor = tf.constant([[1, 2, 3]])
paddings = tf.constant([[1, 2], [3, 4]])
result = tf.pad(tensor, paddings)
with tf.Session() as sess:
print(sess.run(result))
[[0 0 0 0 0 0 0 0 0 0]
[0 0 0 1 2 3 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]]
从输出结果可以看出,对一维矩阵[[1, 2, 3]](其实还是二维的)四个方向进行扩展,paddings=[[1, 2], [3, 4]]分别就对应着上、下、左、右四个边界扩展的宽度;
二维tensor扩展:
import tensorflow as tf
tensor = tf.constant([[1, 2], [3, 4]])
paddings = tf.constant([[1, 2], [3, 4]])
result = tf.pad(tensor, paddings)
with tf.Session() as sess:
print(sess.run(result))
[[0 0 0 0 0 0 0 0 0]
[0 0 0 1 2 0 0 0 0]
[0 0 0 3 4 0 0 0 0]
[0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0]]
同上;
三维tensor扩展:
import tensorflow as tf
tensor = tf.constant([[[1, 2, 3], [3, 4, 5]], [[5, 6, 7], [7, 8, 9]]]) # shape: (2, 2, 3)
paddings = tf.constant([[1, 2], [3, 4], [5, 6]])
result = tf.pad(tensor, paddings)
with tf.Session() as sess:
print(tensor.shape) # shape: (2, 2, 3)
print(sess.run(result))
print(result.shape) # shape: (5, 9, 14)
输出结果如下:
paddings是一个(3 imes 2)的矩阵,第一行[1, 2]表示对tensor的第一个维度进行扩展;第二行[3, 4]对tensor的第二个维度进行扩展;第三行[5, 6]对tensor的第三个维度进行扩展;
可以看到,paddings的要求都是(N imes 2)的矩阵,其中(N)可能就是与tensor的维度相关了吧。
[[[0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 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0 0 0 0 0 0 0 0]]
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0]]]
参数mode
tf.pad()方法提供了三种填充tensor的方式:
mode="CONSTNAT", constant_values=0: 默认,以常数值0来填充;mode="REFLECT"mode="SYMMETRIC"
不同mode对tensor的shape有着不同的要求。