import tensorflow as tf
tf.__version__
'1.10.1'
可以先看一下pad的含义是什么(有道词典的翻译如下):
其中有“填补”的意思,我们就可以推断此函数应该是填充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".
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]]
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]]
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]]
使用方法是一样的,就是在配置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)