一、环境
TensorFlow API r1.12
CUDA 9.2 V9.2.148
cudnn64_7.dll
Python 3.6.3
Windows 10
二、官方说明
对带填充的张量的边缘填充指定值
https://tensorflow.google.cn/api_docs/python/tf/pad
paddings 必须是一个形状为 [n, 2] 的整数类型的张量,n 为 输入 tensor 的阶
对于输入的每个维度 D,paddings [D, 0] 表示待填充的张量在该维度上要填充多少个值,padddings[D, 1] 表示待填充的张量在该维度上要填充多少个值
填充模式为 “REFECT” 时,paddings [D, 0] 和 paddings [D, 1] 都必须不大于 tensor.dim_size(D) -1
填充模式为 “SYMMETRIC” 时,paddings [D, 0] 和 paddings [D, 1] 都必须不大于 tensor.dim_size(D)
输出的每个维度 D 填充后的大小:
paddings [D, 0] + tensor.dim_size(D) + paddings [D, 1]
tf.pad(
tensor,
paddings,
mode='CONSTANT',
name=None,
constant_values=0
)
参数:
tensor:待填充的张量
paddings:类型为 int32 的二维张量,形状为 [n, 2],简单来说就是 n 行 2 列的张量,这里 n 与 tensor 的阶相同, 这里的 2 代表待填充张量的首、尾填充位置,即在待填充张量的相应的维度的首、尾各添加多少行或列
mode:设置填充内容, "CONSTANT"、"REFLECT" 或 "SYMMETRIC" (部分大小写)
name:可选参数,操作的名称
constant_values:必须和 tensor 具有相同的类型,设置为 "CONSTANT" 模式时,使用标量值填充
返回:
和 tensor 具有相同类型的张量
三、实例
实例1:
>>> import tensorflow as tf
>>> t = tf.constant(value=[[1,2,3],[4,5,6]])
>>> paddings = tf.constant(value=[[1,1],[2,2]])
>>> padded_result_1 = tf.pad(tensor=t, paddings=paddings, mode="CONSTANT")
>>> padded_result_2 = tf.pad(tensor=t, paddings=paddings, mode="REFLECT")
>>> padded_result_3 = tf.pad(tensor=t, paddings=paddings, mode="SYMMETRIC")
>>> with tf.Session() as sess:
... print(sess.run(padded_result_1))
... print(sess.run(padded_result_2))
... print(sess.run(padded_result_3))
...
# CONSTANT模式时,[[1,1],[2,2]]表示给待填充的张量上、下各添加 1 行,左、右个添加 2 行
# 全部使用 0 填充
[[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]]
# REFLECT模式时,[[1,1],[2,2]]表示给待填充的张量上、下各添加 1 行,左、右个添加 2 行
# 映射填充:上复制 tensor 的下,下复制 tensor 的上,左复制tensor 的右,右复制 tensor 的左
[[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]]
# SYMMETRIC模式时,[[1,1],[2,2]]表示给待填充的张量上、下各添加 1 行,左、右个添加 2 行
# 对称填充:上复制 tensor 的上,下复制 tensor 的下,左复制tensor 的左,右复制 tensor 的右
[[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:
>>> import tensorflow as tf
>>> t = tf.constant(value=[[1,2,3],[4,5,6],[6,8,9]])
>>> paddings = tf.constant(value=[[1,2],[2,1]])
>>> padded_result_1 = tf.pad(tensor=t, paddings=paddings, mode="CONSTANT")
>>> padded_result_2 = tf.pad(tensor=t, paddings=paddings, mode="REFLECT")
>>> padded_result_3 = tf.pad(tensor=t, paddings=paddings, mode="SYMMETRIC")
>>> with tf.Session() as sess:
... print(sess.run(padded_result_1))
... print(sess.run(padded_result_2))
... print(sess.run(padded_result_3))
...
# CONSTANT模式时,[[1,2],[2,1]]表示给待填充的张量上添加 1 行、下添加 2 行,左添加 2 行、右添加 1 行
# 全部使用 0 填充
[[0 0 0 0 0 0]
[0 0 1 2 3 0]
[0 0 4 5 6 0]
[0 0 6 8 9 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
# REFLECT模式时,[[1,2],[2,1]]表示给待填充的张量上添加 1 行、下添加 2 行,左添加 2 行、右添加 1 行
# 映射填充:上复制 tensor 的中间行及下面的行,下复制 tensor 的中间及上面的行,排列顺序与原 tensor 相反
# 左复制 tensor 的中间及右侧列,右复制 tensor 的中间及左侧的列,排列顺序与原 tensor 相反
[[6 5 4 5 6 5]
[3 2 1 2 3 2]
[6 5 4 5 6 5]
[9 8 6 8 9 8]
[6 5 4 5 6 5]
[3 2 1 2 3 2]]
# SYMMETRIC模式时,,[[1,2],[2,1]]表示给待填充的张量上添加 1 行、下添加 2 行,左添加 2 行、右添加 1 行
# 对称填充:上复制 tensor 的中间行及上面的行,下复制 tensor 的中间行及下面的行,排列顺序与原 tensor 相反
# 左复制tensor 的中间及左侧的列,右复制 tensor 的中间及右侧的列,排列顺序与原 tensor 相反
[[2 1 1 2 3 3]
[2 1 1 2 3 3]
[5 4 4 5 6 6]
[8 6 6 8 9 9]
[8 6 6 8 9 9]
[5 4 4 5 6 6]]