tensorflow 笔记 16:tf.pad

函数:

  • tf.compat.v1.pad
  • tf.pad

函数表达式如下:

tf.pad(
    tensor,
    paddings,
    mode='CONSTANT',
    name=None,
    constant_values=0
)

函数用途:对各个维度进行填充,padding

输入:

  •   tensor :是要填充的张量;  shape 维度为 : (N1,N2,N3,...);
  •   padings:填充方式,也是一个张量,shape : (n,2), n :表示需要的pad的tensor的维度个数;
  •   mode:有三种取值:分别是"CONSTANT" ,"REFLECT", "SYMMETRIC",对padding 的维度也有限制,如下padded;
  •      mode="CONSTANT" 是直接填充 constant_values;
  •      mode = "REFLECT" 是轴对称填充(对称轴为边界列),此时constant_values 无效,用tensor 中的值填充;
  •            mode = "SYMMETRIC" 是轴对称填充(对称轴为边界线),此时 constant_values 无效,用tensor 中的值填充;
  •   constant_values:要填充的 value 值,默认为0;

padding shape举个例子:

要求:pad 维度为(n,2) n:为tensor 的维度个数; 

第一组:

input tensor,shape【3,4,5】三维 tensor

padding shape:【3,2】

第二组:

input tensor,shape【3,4,5,6】四维 tensor

padding shape:【4,2】

padding 的每一维度,都有两个数,第一个表示前面添加几维度,第二个表示 后面添加几维度;

padded 填充之后的每一维度:

The padded size of each dimension D of the output is:

paddings[D, 0] + tensor.dim_size(D) + paddings[D, 1]

if mode == "REFLECT" or mode == "SYMMETRIC":

paddings[D, 0] +  paddings[D, 1] <= tensor.dim_size(D) - 1

举个例子-》填充后的tensor shape:

tensor shape : (3,5,4)

padding = [[1,1],[2,2],[1,0]]

padded shape: (3+1+1,5+2+2,4+1+0)= (5,9,5)

REFLECT:的填充方式使用的是一种通过对称轴进行对称复制的方式进行填充(复制时不包括对称轴,边界的那一列是对称轴),通过使用tensor边缘作为对称;

SYMMETRIC:的填充方式于REFLECT填充方式类似,也是按照对称轴就是复制的,只是它包括对称轴(边界外的线是对称轴)。 

举例一:(来自官方):

 1 t = tf.constant([[1, 2, 3], [4, 5, 6]]) #shape(2,3)
 2 paddings = tf.constant([[1, 1,], [2, 2]]) # shape(2,2),第一维度,前面补一维度,后面补一维度;第二维度,前面补两维度,后面补两维度;
 3 # 'constant_values' is 0.
 4 # rank of 't' is 2.
 5 tf.pad(t, paddings, "CONSTANT")  # [[0, 0, 0, 0, 0, 0, 0],
 6                                  #  [0, 0, 1, 2, 3, 0, 0],
 7                                  #  [0, 0, 4, 5, 6, 0, 0],
 8                                  #  [0, 0, 0, 0, 0, 0, 0]]
 9 
10 tf.pad(t, paddings, "REFLECT")  # [[6, 5, 4, 5, 6, 5, 4],
11                                 #  [3, 2, 1, 2, 3, 2, 1],  # 黄色为对称轴
12                                 #  [6, 5, 4, 5, 6, 5, 4],
13                                 #  [3, 2, 1, 2, 3, 2, 1]]
14 
15 tf.pad(t, paddings, "SYMMETRIC")  # [[2, 1, 1, 2, 3, 3, 2],
16                                   #  [2, 1, 1, 2, 3, 3, 2], #
17                                   #  [5, 4, 4, 5, 6, 6, 5],
18                                   #  [5, 4, 4, 5, 6, 6, 5]]

举例二:

import tensorflow as tf
import numpy as np
                                                                                                                                                                                     
m1 = tf.random_normal([1,2,3,4],mean=0.0,stddev=1.0,dtype=tf.float32)

m2 = tf.pad(m1,[[2,0],[0,0],[0,0],[0,0]],constant_values = 1)

m2_s = tf.shape(m2) # shape(3,2,3,4)

with tf.Session() as sess:
    print(sess.run(m1))
    print(sess.run(m2))
    print(sess.run(m2_s)) 

output:

# m1
[[[[-1.8582115  -1.170714   -0.4478178   2.0172668 ]
   [-0.74805504 -0.08016825 -0.7742696  -0.02516617]
   [-0.8256318   0.591446   -0.00889379  1.7998788 ]]

  [[ 0.00565176 -0.31549874  1.5197186   0.07842494]
   [ 0.00609808  1.9219669  -0.42632174  1.5106113 ]
   [ 0.67241013 -0.38563538 -0.976289    0.2032768 ]]]]

#m2
[[[[ 1.          1.          1.          1.        ]
   [ 1.          1.          1.          1.        ]
   [ 1.          1.          1.          1.        ]]

  [[ 1.          1.          1.          1.        ]
   [ 1.          1.          1.          1.        ]
   [ 1.          1.          1.          1.        ]]]


 [[[ 1.          1.          1.          1.        ]
   [ 1.          1.          1.          1.        ]
   [ 1.          1.          1.          1.        ]]

  [[ 1.          1.          1.          1.        ]
   [ 1.          1.          1.          1.        ]
   [ 1.          1.          1.          1.        ]]]

 [[[-1.2366703  -1.0050759  -0.3843815   1.0201392 ]
   [-1.3438475   0.8829414  -1.3399163   1.078826  ]
   [-0.09356844  0.35896888  1.5112561   0.28352356]]

  [[ 0.45909956 -0.23824279 -0.31440428  1.1913226 ]
   [-0.40780786  0.58995795 -0.9147027   0.05860058]
   [-0.0659609   1.4536899  -0.12121342 -0.9752257 ]]]]

#output shape
[3 2 3 4]
View Code
原文地址:https://www.cnblogs.com/lovychen/p/11177336.html