tensorflow去掉某一维度_TensorFlow2.0:维度变换

**## 一 reshape函数 重排列**```In [1]: import tensorflow as tf                                                 In [2]: a = tf.random.normal([4,28,28,3],mean=1,stddev=1)                       In [3]: a.shape                                                                 Out[3]: TensorShape([4, 28, 28, 3])In [4]: a.ndim                                                                  Out[4]: 4In [5]: tf.reshape(a,[4,28*28,3]).shape                                         Out[5]: TensorShape([4, 784, 3])In [6]: tf.reshape(a,[4,-1,3]).shape#未知维度时可以用-1填充                     Out[6]: TensorShape([4, 784, 3])In [7]: tf.reshape(a,[4,28*28*3]).shape                                         Out[7]: TensorShape([4, 2352])In [8]: tf.reshape(a,[4,-1]).shape                                              Out[8]: TensorShape([4, 2352])``````In [9]: tf.reshape(tf.reshape(a,[4,-1]),[4,28,28,3]).shape                      Out[9]: TensorShape([4, 28, 28, 3])In [10]: tf.reshape(tf.reshape(a,[4,-1]),[4,14,-1,3]).shape                     Out[10]: TensorShape([4, 14, 56, 3])In [11]: tf.reshape(tf.reshape(a,[4,-1]),[4,1,-1,3]).shape                      Out[11]: TensorShape([4, 1, 784, 3])```**## 二 tf.transpose( ) 函数 转置**    --tf.transpose( )转置函数,默认将所有的轴进行转置,当输入参数perm时,按照perm对应的轴的顺序进行排列    如a. shape = [4,2,5,7]时,若输入参数perm = [3,0,2,1]则当axis=3对应的维度为7,当axis=0对应维度为4,当axis=2对应维度为5,当axis=1对应维度为2,故tf.transpose(a,perm=[3,0,2,1]).shape = [7,4,5,2]```In [12]: a = tf.random.normal([4,3,2,1])                                        In [13]: a.shape                                                                Out[13]: TensorShape([4, 3, 2, 1])In [14]: tf.transpose(a).shape                                                  Out[14]: TensorShape([1, 2, 3, 4])In [16]: tf.transpose(a,perm=[0,1,3,2]).shape#axis=1,shape=4;axis=1,shape=3;axis    ...: =3,shape=1;axis=2,shape=2                                              Out[16]: TensorShape([4, 3, 1, 2])``````In [3]: a.shape                                                                 Out[3]: TensorShape([4, 28, 28, 3])In [4]: tf.transpose(a,[2,0,1,3]).shape                                         Out[4]: TensorShape([28, 4, 28, 3])In [5]: tf.transpose(a,perm = [0,3,2,1]).shape                                  Out[5]: TensorShape([4, 3, 28, 28])In [6]: tf.transpose(a,perm = [0,3,1,2]).shape                                  Out[6]: TensorShape([4, 3, 28, 28])```**## 三 tf.expand_dims( ) 函数 维度扩充**```In [7]: a = tf.random.normal([4,35,8])                                          In [8]: a.shape                                                                 Out[8]: TensorShape([4, 35, 8])In [9]: tf.expand_dims(a,axis=0).shape                                          Out[9]: TensorShape([1, 4, 35, 8])In [10]: tf.expand_dims(a,axis=3).shape                                         Out[10]: TensorShape([4, 35, 8, 1])``````In [11]: a = tf.random.normal([4,35,8])                                         In [12]: a.shape                                                                Out[12]: TensorShape([4, 35, 8])In [13]: tf.expand_dims(a,axis=0).shape                                         Out[13]: TensorShape([1, 4, 35, 8])In [14]: tf.expand_dims(a,axis=3).shape                                         Out[14]: TensorShape([4, 35, 8, 1])In [15]: tf.expand_dims(a,axis=-1).shape                                        Out[15]: TensorShape([4, 35, 8, 1])In [16]: tf.expand_dims(a,axis=-4).shape                                        Out[16]: TensorShape([1, 4, 35, 8])```**## 四 tf.squeeze( )函数 去掉为1的维度**```In [19]: a = tf.zeros([1,2,1,1,3])                                              In [20]: a.shape                                                                Out[20]: TensorShape([1, 2, 1, 1, 3])In [21]: tf.squeeze(a).shape                                                    Out[21]: TensorShape([2, 3])In [22]: tf.squeeze(a,axis=0).shape                                             Out[22]: TensorShape([2, 1, 1, 3])In [23]: tf.squeeze(a,axis=2).shape                                             Out[23]: TensorShape([1, 2, 1, 3])In [24]: tf.squeeze(a,axis=3).shape                                             Out[24]: TensorShape([1, 2, 1, 3])In [25]: tf.squeeze(a,axis=-2).shape                                            Out[25]: TensorShape([1, 2, 1, 3])```

你可能感兴趣的:(tensorflow去掉某一维度_TensorFlow2.0:维度变换)