TensorFlow2.0:数据的填充与复制

**

一 tf.pad( )填充函数

**

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

paddings参数的设置方法:

假设输入tensor有两个维度,则paddings参数设置为paddings = [[a,b],[c,d]]
a,b表示为axis=0维度上最前面填充a行,最后面填充b行,
c,d表示为axis=1维度上最前面填充c列,最后面填充d列,

同理,假设输入的tensor有三个维度,则paddings参数设置为paddings = [[a,b],[c,d],[e,f]]
a,b表示为axis=0维度上最前面填充a行,最后面填充b行,
c,d表示为axis=1维度上最前面填充c列,最后面填充d列,
e,f表示为axis=2维度上最前面填充e,最后面填充f,


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

In [4]: a = tf.reshape(a,[3,3])                                                 

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

In [6]: tf.pad(a,[[0,0],[0,0]])#不填充                                          
Out[6]: 
<tf.Tensor: id=9, shape=(3, 3), dtype=int32, numpy=
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]], dtype=int32)>

In [7]: tf.pad(a,[[1,0],[0,0]])#在axis=0上最前面填充1行,得到的维度应为[4,3]     
Out[7]: 
<tf.Tensor: id=12, shape=(4, 3), dtype=int32, numpy=
array([[0, 0, 0],
       [0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]], dtype=int32)>

In [8]: tf.pad(a,[[0,1],[0,0]])#在axis=0的最后面填充1行                         
Out[8]: 
<tf.Tensor: id=15, shape=(4, 3), dtype=int32, numpy=
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8],
       [0, 0, 0]], dtype=int32)>

In [9]: tf.pad(a,[[0,0],[1,0]])#在axis=1的最前面填充1列                         
Out[9]: 
<tf.Tensor: id=18, shape=(3, 4), dtype=int32, numpy=
array([[0, 0, 1, 2],
       [0, 3, 4, 5],
       [0, 6, 7, 8]], dtype=int32)>

In [10]: tf.pad(a,[[0,0],[0,1]])#在axis=1的最后面填充1列                        
Out[10]: 
<tf.Tensor: id=21, shape=(3, 4), dtype=int32, numpy=
array([[0, 1, 2, 0],
       [3, 4, 5, 0],
       [6, 7, 8, 0]], dtype=int32)>

In [11]: tf.pad(a,[[1,1],[0,0]])#在axis=0最前面和最后面分别填充                 
Out[11]: 
<tf.Tensor: id=24, shape=(5, 3), dtype=int32, numpy=
array([[0, 0, 0],
       [0, 1, 2],
       [3, 4, 5],
       [6, 7, 8],
       [0, 0, 0]], dtype=int32)>

In [12]: tf.pad(a,[[0,0],[1,1]])#在axis=1最前和最后各填充                       
Out[12]: 
<tf.Tensor: id=27, shape=(3, 5), dtype=int32, numpy=
array([[0, 0, 1, 2, 0],
       [0, 3, 4, 5, 0],
       [0, 6, 7, 8, 0]], dtype=int32)>

In [13]: tf.pad(a,[[1,1],[1,1]])#在axis=0和axis=1的最前和最后各进行填充         
Out[13]: 
<tf.Tensor: id=30, shape=(5, 5), dtype=int32, numpy=
array([[0, 0, 0, 0, 0],
       [0, 0, 1, 2, 0],
       [0, 3, 4, 5, 0],
       [0, 6, 7, 8, 0],
       [0, 0, 0, 0, 0]], dtype=int32)>

iamge padding

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

In [19]: tf.pad(a,[[0,0],[2,2],[2,2],[2,2],[0,0]],constant_values=255).shape                                                                                                                                
Out[19]: TensorShape([4, 32, 32, 32, 3])

**

二 tf.tile( ) 数据复制函数

**

    tf.tile(  
        input,     #输入  
        multiples,  #某一维度上复制的次数  
        name=None  
    )

multiples参数设置方法:

假设输入Input有两个维度,则可以设置multiples = [a,b]
a表示在维度axis=0上复制a次,
b表示在维度axis=1上复制b次.

同理,假设Input有三个维度,则可以设置multiples = [a,b,c]
a表示在维度axis=0上复制a次,
b表示在维度axis=1上复制b次.
c表示在维度axis=2上复制c次,

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

In [4]: a = tf.reshape(a,[3,3])                                                                                                                                                                             

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

In [6]: tf.tile(a,[1,1])#表示不复制                                                                                                                                                                         
Out[6]: 
<tf.Tensor: id=9, shape=(3, 3), dtype=int32, numpy=
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]], dtype=int32)>

In [7]: tf.tile(a,[1,2])#在axis=1维度上复制2次                                                                                                                                                              
Out[7]: 
<tf.Tensor: id=12, shape=(3, 6), dtype=int32, numpy=
array([[0, 1, 2, 0, 1, 2],
       [3, 4, 5, 3, 4, 5],
       [6, 7, 8, 6, 7, 8]], dtype=int32)>

In [8]: tf.tile(a,[2,2])#在axis=0和axis=1维度上分别复制2,次,先复制小维度,再复制大维度                                                                                                                                                  
Out[8]: 
<tf.Tensor: id=15, shape=(6, 6), dtype=int32, numpy=
array([[0, 1, 2, 0, 1, 2],
       [3, 4, 5, 3, 4, 5],
       [6, 7, 8, 6, 7, 8],
       [0, 1, 2, 0, 1, 2],
       [3, 4, 5, 3, 4, 5],
       [6, 7, 8, 6, 7, 8]], dtype=int32)>

In [9]: tf.tile(a,[2,1])#在axis=0维度上复制2次                                                                                                                                                              
Out[9]: 
<tf.Tensor: id=18, shape=(6, 3), dtype=int32, numpy=
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8],
       [0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]], dtype=int32)>

tile VS broadcast_to
broadcast_to相当于先expand_dims,再tile

In [12]: a = tf.reshape(tf.range(9),[3,3])                                                                                                                                                                  

In [13]: a                                                                                                                                                                                                  
Out[13]: 
<tf.Tensor: id=28, shape=(3, 3), dtype=int32, numpy=
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]], dtype=int32)>

In [14]: aa = tf.expand_dims(a,axis=0)#先扩充维度                                                                                                                                                           

In [15]: aa                                                                                                                                                                                                 
Out[15]: 
<tf.Tensor: id=31, shape=(1, 3, 3), dtype=int32, numpy=
array([[[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]]], dtype=int32)>

In [16]: bb = tf.tile(aa,[2,1,1])                                                                                                                                                                           

In [17]: bb                                                                                                                                                                                                 
Out[17]: 
<tf.Tensor: id=34, shape=(2, 3, 3), dtype=int32, numpy=
array([[[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]],

       [[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]]], dtype=int32)>

In [18]: cc = tf.broadcast_to(a,[2,3,3])                                                                                                                                                                    

In [19]: cc                                                                                                                                                                                                 
Out[19]: 
<tf.Tensor: id=37, shape=(2, 3, 3), dtype=int32, numpy=
array([[[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]],

       [[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]]], dtype=int32)>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值