import tensorflow as tf
tf.__version__
'1.10.1'
我们今天来看一个函数:tf.pad
可以先看一下pad的含义是什么(有道词典的翻译如下):
- n. 衬垫;护具;便笺簿;填补
- vi. 步行;放轻脚步走
- vt. 填补;走
- n. (Pad)人名;(英)帕德(男子教名 Patrick 的昵称)
其中有“填补”的意思,我们就可以推断此函数应该是填充tensor的作用。
来看一下帮助信息,并重复其例子
help(tf.pad)
Help on function pad in module tensorflow.python.ops.array_ops:
pad(tensor, paddings, mode='CONSTANT', name=None, constant_values=0)
Pads a tensor.
This operation pads a `tensor` according to the `paddings` you specify.
`paddings` is an integer tensor with shape `[n, 2]`, where n is the rank of
`tensor`. For each dimension D of `input`, `paddings[D, 0]` indicates how
many values to add before the contents of `tensor` in that dimension, and
`paddings[D, 1]` indicates how many values to add after the contents of
`tensor` in that dimension. If `mode` is "REFLECT" then both `paddings[D, 0]`
and `paddings[D, 1]` must be no greater than `tensor.dim_size(D) - 1`. If
`mode` is "SYMMETRIC" then both `paddings[D, 0]` and `paddings[D, 1]` must be
no greater than `tensor.dim_size(D)`.
The padded size of each dimension D of the output is:
`paddings[D, 0] + tensor.dim_size(D) + paddings[D, 1]`
For example:
```python
t = tf.constant([[1, 2, 3], [4, 5, 6]])
paddings = tf.constant([[1, 1,], [2, 2]])
# 'constant_values' is 0.
# rank of 't' is 2.
tf.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]]
tf.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]]
tf.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]]
```
Args:
tensor: A `Tensor`.
paddings: A `Tensor` of type `int32`.
mode: One of "CONSTANT", "REFLECT", or "SYMMETRIC" (case-insensitive)
name: A name for the operation (optional).
constant_values: In "CONSTANT" mode, the scalar pad value to use. Must be
same type as `tensor`.
Returns:
A `Tensor`. Has the same type as `tensor`.
Raises:
ValueError: When mode is not one of "CONSTANT", "REFLECT", or "SYMMETRIC".
- 从帮助中可以看出:
- 此函数是用来填充tensor用的,是按照padding来填充tensor,从而获取想要的shape的tensor的。
- 此函数有5个入参:tensor(需要填补的tensor)、paddings(填补的方向和个数tensor)、mode(填充的方式,可选的有三种)、name(节点名字)、constant_values(填充的常数值)
- 返回一个改变后的tensor
- 三种mode可选,默认情况是填充常数0
重复函数的例子如下–>验证一下
sess = tf.Session()
t = tf.constant([[1, 2, 3], [4, 5, 6]])
paddings = tf.constant([[1, 1,], [2, 2]])
print('tensor is :')
print(t.eval(session=sess))
print('\n')
print('paddings is :')
print(paddings.eval(session=sess))
tensor is :
[[1 2 3]
[4 5 6]]
paddings is :
[[1 1]
[2 2]]
tt = tf.pad(t, paddings, "CONSTANT")
print('params t paddings CONSTANT results is :')
print(tt.eval(session=sess))
params t paddings CONSTANT results is :
[[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]]
- 可以看出上面的结果是在tensor的增加了两行,上下各一行;同时增加了四列,左右各两列;都是常数0
tt = tf.pad(t, paddings, "REFLECT")
print('params t paddings REFLECT results is :')
print(tt.eval(session=sess))
params t paddings REFLECT results is :
[[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]]
- 这个我刚看起来的时候是一头雾水,不知道是如何填充的;如何搞清楚的呢:
- 1、查了“REFLECT”的含义是:反射。应该与反射有关
- 2、仔细查看,核心是矩阵[[1,2,3],[4,5,6]],而外面的第一行正好和中心的第二行是一样的[4,5,6],而下面正好和上面是一样的[1,2,3];左右正好也是按照反射的方式填充的。
- 3、上面的分析可以大概理解了,而且从上面的help中可以看出mode为reflect的时候paddings的dim是有要求的,不能超过相应的tensor的dim的
tt = tf.pad(t, paddings, "SYMMETRIC")
print('params t paddings SYMMETRIC results is :')
print(tt.eval(session=sess))
params t paddings SYMMETRIC results is :
[[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]]
- 有了上面的理解,自然就知道了此填充的含义:可以理解成就近填充。
上面是按照二维的情况来理解tf.pad函数,可以扩展到多维的情况
使用方法是一样的,就是在配置paddings的时候会需要确定好要扩展的维度。
按照help的说法是:paddings的维度是(n,2),即:[[0,0],[0,0],[0,0],…];其中每一行就是一维的左右添加的个数。
- 我们来看一下三维的情况下如何变的:
t = tf.constant([[[1, 2, 3], [4, 5, 6]],[[7, 8, 9], [10, 11, 12]]])
paddings = tf.constant([[1, 1,], [2, 2],[1,0]])
print('tensor is :')
print(t.eval(session=sess))
print('t shape is :',t.get_shape())
print('\n')
print('paddings is :')
print(paddings.eval(session=sess))
tensor is :
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
('t shape is :', TensorShape([Dimension(2), Dimension(2), Dimension(3)]))
paddings is :
[[1 1]
[2 2]
[1 0]]
tt = tf.pad(t, paddings, "CONSTANT")
print('params t paddings CONSTANT results is :')
print(tt.eval(session=sess))
params t paddings CONSTANT results is :
[[[ 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 1 2 3]
[ 0 4 5 6]
[ 0 0 0 0]
[ 0 0 0 0]]
[[ 0 0 0 0]
[ 0 0 0 0]
[ 0 7 8 9]
[ 0 10 11 12]
[ 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]]]
print('tt shape is: ')
print(tt.get_shape())
tt shape is:
(4, 6, 4)
- 维度的变化应该是:paddings的行求和+原始dim