TensorFlow2.0(4):填充與復制


 

 

 

注:本系列所有博客將持續更新並發布在github上,您可以通過github下載本系列所有文章筆記文件。

 

1 pad()

 

tf.pad函數主要是用來對tensor的大小進行擴展,包括水平、垂直、深度(通道)等,方法定義如下:

pad(tensor, paddings, mode="CONSTANT", name=None, constant_values=0)

輸入參數:

  • tensor:輸入的tensor

  • paddings:設置填充的大小

  • mode:填充方式,默認是CONSTANT,還有REFLECT和SYMMETRIC

  • name:名稱

  • constant_values:CONSTANT填充方式的填充值,默認是0

參數paddings必須是形狀為(n, 2)的一個list,這里的n是tensor的秩,也就是維度大小。例如當tensor為一個shape為(12,)的tensor時,paddings必須是形如[[x,y]]的一個list,x表示在第一維度前填充值的個數,y表示在第一維度后填充值的個數:

In [1]:
import tensorflow as tf
In [2]:
a = tf.range(1,13)
In [3]:
a
Out[3]:
<tf.Tensor: id=3, shape=(12,), dtype=int32, numpy=array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12], dtype=int32)>
In [4]:
tf.pad(a, [[3,0]])  # 3表示在第一維度前填充3個0,0表示不填充
Out[4]:
<tf.Tensor: id=5, shape=(15,), dtype=int32, numpy=
array([ 0,  0,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12],
      dtype=int32)>
 

當tensor是二維時,paddings必須是shape為(2,2)的list:

In [5]:
a = tf.reshape(a, [3, 4])
In [6]:
a
Out[6]:
<tf.Tensor: id=7, shape=(3, 4), dtype=int32, numpy=
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]], dtype=int32)>
In [7]:
tf.pad(a, [[1,1],[3,0]], constant_values=3)  # 第一維度前后各填充一行,第二維度前填充兩行,后不填充,填充值為3
Out[7]:
<tf.Tensor: id=10, shape=(5, 7), dtype=int32, numpy=
array([[ 3,  3,  3,  3,  3,  3,  3],
       [ 3,  3,  3,  1,  2,  3,  4],
       [ 3,  3,  3,  5,  6,  7,  8],
       [ 3,  3,  3,  9, 10, 11, 12],
       [ 3,  3,  3,  3,  3,  3,  3]], dtype=int32)>
 

對於3維tensor,paddings是一個shape為(3,2)的list:

In [8]:
a = tf.reshape(a, [2, 2, 3])
In [9]:
a
Out[9]:
<tf.Tensor: id=12, shape=(2, 2, 3), dtype=int32, numpy=
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]], dtype=int32)>
In [10]:
tf.pad(a, [[1, 0],[1,1],[1,0]])  # 第一維度前填充1塊數據,后不填充,第二維度前后各填充1行,第三維度前填充1列,后不填充
Out[10]:
<tf.Tensor: id=14, shape=(3, 4, 4), dtype=int32, numpy=
array([[[ 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,  7,  8,  9],
        [ 0, 10, 11, 12],
        [ 0,  0,  0,  0]]], dtype=int32)>
In [11]:
a = tf.range(1,13)
In [12]:
a = tf.reshape(a,[3,4])
In [13]:
a
Out[13]:
<tf.Tensor: id=20, shape=(3, 4), dtype=int32, numpy=
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]], dtype=int32)>
 

當指定填充模式mode為'REFLECT'時,指的是以各維度邊緣為對稱軸進行填充(不包括邊緣數據也就是對稱軸本身),且填充的規模不能大於該維度原有規模-1:

In [14]:
tf.pad(a, [[2,1],[3,1]],mode='REFLECT')  # 對第二個維度填充時,如果大於3就回產生異常,因為3已經可以把第二維度所有數據復制一遍
Out[14]:
<tf.Tensor: id=22, shape=(6, 8), dtype=int32, numpy=
array([[12, 11, 10,  9, 10, 11, 12, 11],
       [ 8,  7,  6,  5,  6,  7,  8,  7],
       [ 4,  3,  2,  1,  2,  3,  4,  3],
       [ 8,  7,  6,  5,  6,  7,  8,  7],
       [12, 11, 10,  9, 10, 11, 12, 11],
       [ 8,  7,  6,  5,  6,  7,  8,  7]], dtype=int32)>
 

SYMMETRIC填充模式與REFLECT填充模式一樣,都是以邊緣為對稱軸進行賦值填充,不過SYMMETRIC模式會對對稱軸進行賦值,所以指定的規模最大可以為原規模:

In [15]:
tf.pad(a, [[2,1],[4,1]],mode='SYMMETRIC')  # 這時候對第二個維度填充規模可以為4,但是超過4舊貨產生異常
Out[15]:
<tf.Tensor: id=24, shape=(6, 9), dtype=int32, numpy=
array([[ 8,  7,  6,  5,  5,  6,  7,  8,  8],
       [ 4,  3,  2,  1,  1,  2,  3,  4,  4],
       [ 4,  3,  2,  1,  1,  2,  3,  4,  4],
       [ 8,  7,  6,  5,  5,  6,  7,  8,  8],
       [12, 11, 10,  9,  9, 10, 11, 12, 12],
       [12, 11, 10,  9,  9, 10, 11, 12, 12]], dtype=int32)>
 

2 tile()

tile()方法對指定維度進行復制,定義如下:

tile(input, multiples, name=None):

  • input:需要復制的tensor
  • multiples: 各維度需要復制的次數,0表示去除數據,1表示不復制,2表示復制一次

參數multiples是一個長度與tensor的秩相等的list,例如當tensor的shape為(12,)時,multiples的shape也必須為只有一個元素的list,例如multiples=[2],表示對第一維度復制1次:

In [21]:
a = tf.range(12)
In [22]:
tf.tile(a,[2])
Out[22]:
<tf.Tensor: id=33, shape=(24,), dtype=int32, numpy=
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11,  0,  1,  2,  3,  4,
        5,  6,  7,  8,  9, 10, 11], dtype=int32)>
 

當tensor的shape為(3,4)時,multiples是一個包含兩個元素的list:

In [23]:
a = tf.reshape(a, [3,4])
In [24]:
tf.tile(a, [2,3])  # 第一維度復制1次,第二維度復制2次
Out[24]:
<tf.Tensor: id=37, shape=(6, 12), dtype=int32, numpy=
array([[ 0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3],
       [ 4,  5,  6,  7,  4,  5,  6,  7,  4,  5,  6,  7],
       [ 8,  9, 10, 11,  8,  9, 10, 11,  8,  9, 10, 11],
       [ 0,  1,  2,  3,  0,  1,  2,  3,  0,  1,  2,  3],
       [ 4,  5,  6,  7,  4,  5,  6,  7,  4,  5,  6,  7],
       [ 8,  9, 10, 11,  8,  9, 10, 11,  8,  9, 10, 11]], dtype=int32)>
 

當tensor的shape為(2,2,3時,multiples是一個包含3個元素list:

In [25]:
a = tf.reshape(a, [2,2,3])
In [26]:
tf.tile(a, [2,1,2])
Out[26]:
<tf.Tensor: id=41, shape=(4, 2, 6), dtype=int32, numpy=
array([[[ 0,  1,  2,  0,  1,  2],
        [ 3,  4,  5,  3,  4,  5]],

       [[ 6,  7,  8,  6,  7,  8],
        [ 9, 10, 11,  9, 10, 11]],

       [[ 0,  1,  2,  0,  1,  2],
        [ 3,  4,  5,  3,  4,  5]],

       [[ 6,  7,  8,  6,  7,  8],
        [ 9, 10, 11,  9, 10, 11]]], dtype=int32)>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM