TensorFlow pad函数使用方法
tf.pad(
tensor,
paddings,
mode='CONSTANT',
name=None,
constant_values=0
)
tensor 被填充的张量
paddings 填充的格式
mode 填充模式:"CONSTANT"、"REFLECT"、"SYMMETRIC"
name 该操作张量的名称
constant_values 用于在"CONSTANT"模式下,设置的填充值
import tensorflow as tf
t1 = tf.constant([[1, 2, 3], [4, 5, 6]]) #
# [1,1]代表在第一维度pad,即在t1的第一维上填充
# [2,2]代表在第二维度pad,即在t1的第二维上填充
paddings1 = tf.constant([[1, 1], [2, 2]])
t2 = tf.constant([[[1,2,3], [4,5,6], [7,8,9]]]) #
# [0,0]代表在第一维度pad,即在t2的第一维上填充
# [2,2]代表在第二维度pad,即在t2的第二维上填充
# [1,1]代表在第二维度pad,即在t2的第三维上填充
paddings2 = tf.constant([[0, 0], [2, 2], [1, 1]])
with tf.Session() as sess:
# 例子1 二维
op = tf.pad(t1, paddings1, "CONSTANT")
print(t1.get_shape())
print(paddings1.get_shape())
print(sess.run(op))
# 例子2 三维
print(t2.get_shape())
print(paddings2.get_shape())
op = tf.pad(t2, paddings2, "CONSTANT")
print(sess.run(op))
- 输出结果