1. 程式人生 > >[阿里DIN]從模型原始碼梳理TensorFlow的形狀相關操作

[阿里DIN]從模型原始碼梳理TensorFlow的形狀相關操作

# [阿里DIN]從模型原始碼梳理TensorFlow的形狀相關操作 [toc] ## 0x00 摘要 本文基於阿里推薦 DIN 和 DIEN 程式碼,梳理了下深度學習一些概念,以及TensorFlow中的相關實現。 因為篇幅所限,所以之前的整體程式碼講解中,很多細節沒有深入,所以本文會就 “TensorFlow形狀相關” 這些細節進行探討,旨在幫助小夥伴們詳細瞭解每一的步驟以及為什麼要這樣做。 涉及概念有:reduce_sum,reshape, expand_dims等。 ## 0x01 reduce_sum 因為 reduce_sum 中有降維可能,所以在這裡一起講解 ### 1.1 reduce_sum函式 `reduce_sum()` 用於計算張量tensor沿著某一維度的和,可以在求和後降維。 函式原型如下: ```python tf.reduce_sum( input_tensor, axis=None, keepdims=None, name=None, reduction_indices=None, keep_dims=None) ``` - input_tensor:待求和的tensor; - axis:指定的維,如果不指定,則計算所有元素的總和; - keepdims:是否保持原有張量的維度,設定為True,結果保持輸入tensor的形狀,設定為False,結果會降低維度,如果不傳入這個引數,則系統預設為False; - name:操作的名稱; - reduction_indices:在以前版本中用來指定軸,已棄用; - keep_dims:在以前版本中用來設定是否保持原張量的維度,已棄用; ### 1.2 維度和軸 什麼是維度?什麼是軸(axis)? **維度是用來索引一個多維陣列中某個具體數所需要最少的座標數量。** - 0維,又稱0維張量,數字,標量:1 - 1維,又稱1維張量,陣列,vector:[1, 2, 3] - 2維,又稱2維張量,矩陣,二維陣列:[[1,2], [3,4]] - 3維,又稱3維張量,立方(cube),三維陣列:[ [[1,2], [3,4]], [[5,6], [7,8]] ] - n維:你應該get到點了吧~ **再多的維只不過是是把上一個維度當作自己的元素**:1維的元素是標量,2維的元素是陣列,3維的元素是矩陣。 **axis是多維陣列每個維度的座標。**拿3維來說,數字3的座標是[0, 1, 0],那麼第一個數字0的axis是0,第二個數字1的axis是1,第三個數字0的axis是2。 讓我們再看看我們是如何得到3這個數字的: 1. 找到3所在的2維矩陣在這個3維立方的索引:0 2. 找到3所在的1維陣列在這個2維矩陣的索引:1 3. 找到3這個數這個1維陣列的索引:0 也就是說,對於[ [[1,2], [3,4]], [[5,6], [7,8]] ]這個3維情況,[[1,2],[3,4]], [[5,6], [7,8]] 這兩個矩陣的axis是0,[1,2],[3,4],[5,6],[7,8]這4個數組(二維矩陣的元素是一維陣列)的axis是1,而1,2,3,4,5,6,7,8這8個數的axis是2。 **越往裡axis就越大,依次加1。**這裡需要注意的是,**axis可以為負數**,此時表示倒數第axis個維度,這和Python中列表切片的用法類似。 ### 1.3 例子 下面舉個多維tensor例子簡單說明。下面是個 2 * 3 * 4 的tensor。 ```python [[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]], [[ 13 14 15 16] [ 17 18 19 20] [ 21 22 23 24]]] ``` tf.reduce_sum(tensor, axis=0) axis=0 說明是按第一個維度進行求和。那麼求和結果shape是3*4 ```json [[1+13 2+14 3+15 4+16] [5+17 6+18 7+19 8+20] [9+21 10+22 11+23 12+24]] ``` 依次類推,如果axis=1,那麼求和結果shape是2 * 4,即: ```json [[ 1 + 5 + 9 2 + 6+10 3 + 7+11 4 + 8+12] [13+17+21 14+18+22 15+19+23 16+20+24]] ``` 如果axis=2,那麼求和結果shape是2*3,即: ```json [[1+2+3+4 5+6+7+8 9+10+11+12] [13+14+15+16 17+18+19+20 1+22+23+24]] ``` ### 1.4 DIN使用 在DIN中使用之處如下: ```python self.item_eb = tf.concat([self.mid_batch_embedded, self.cat_batch_embedded], 1) self.item_his_eb = tf.concat([self.mid_his_batch_embedded, self.cat_his_batch_embedded], 2) self.item_his_eb_sum = tf.reduce_sum(self.item_his_eb, 1) ``` 執行時候變數結構如下: ```python item_eb = {Tensor} Tensor("concat:0", shape=(?, 36), dtype=float32) item_his_eb = {Tensor} Tensor("concat_1:0", shape=(?, ?, 36), dtype=float32) item_his_eb_sum = {Tensor} Tensor("Sum:0", shape=(?, 36), dtype=float32) ``` mid_his_batch_embedded的形狀是 [128 16 18],內容舉例: ```python [[ [0.000897633377 0.0026908936 0.00315255579 0.000602866057 5.02727926e-06 -0.000445205718 0.00215634611 -0.00388719817 0.000877270475 -0.00319912238 -0.00387874478 -0.00245183101 0.00391598418 0.00260704244 0.00323193427 0.00235629268 0.000642822124 -0.00364282541] .... ]] ``` item_his_eb 的形狀是 [128 16 36],內容舉例: ```python [[ [0.000836691819 0.00270568067 0.00341557898 -0.00352220959 -0.00171846198 0.00192829408 0.000170913059 -0.00161524978 0.00240325416 -0.00387337967 0.00190017093 -0.000109563116 -0.000323365442 -0.00386648951 -0.00287669944 -0.0022441668 -0.00368424738 -0.00336334365 -0.0216899961 0.0148526281 -0.00535430759 0.0403468758 -0.0363828801 0.000673268 0.037890628 0.0381312445 0.0224618614 -0.0360415801 -0.0321329683 -0.0523791686 -0.0239788368 -0.0435246229 0.0599715188 -0.00304881856 -0.0160646625 -0.0127659254] ...... ]] ``` item_his_eb_sum 的 形狀是 [128 36],內容舉例: ```python [ [0.00939779077 -0.00353274215 -0.0084474273 -0.00325064152 0.00785735808 -0.00633620284 -0.0135835949 -0.0220253039 0.000667564571 -0.00415133219 -0.00941559952 -0.00134759676 0.0218551699 0.00516221765 0.00148251373 0.0104214773 -0.0175076462 -0.0165384263 -0.241468206 0.34739849 -0.28044036 0.632885039 -0.317169189 0.660692692 0.219820738 0.458001763 0.14058882 -0.468434095 -0.745587289 -0.0825672 -0.468581945 -0.854899049 1.2372489 -0.23725155 0.249111429 0.081095174] ... ] ``` 可見,**item_his_eb_sum 就是按照第一維度進行sum,然後降維**。 ## 0x02 reshape ### 2.1 reshape函式 原型為 def reshape(tensor, shape, name=None) - tensor 為被調整維度的張量。 - shape 為要調整為的形狀,shape裡最多有一個維度的值可以填寫為-1,表示自動計算此維度。 - 返回一個shape形狀的新tensor 比如 ```python S = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) t = tf.reshape(S, [3, 3]) ``` 得到 ```python [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ``` ### 2.2 DIN使用 DIN之中,使用如下: ```python scores = tf.reshape(scores, [-1, tf.shape(facts)[1]]) output = facts * tf.expand_dims(scores, -1) output = tf.reshape(output, tf.shape(facts)) ``` -1 的意思是:目前我不確定,所以在執行時候程式先考慮後面的維度。 結合執行時候可以看出來,就是把 scores 中間的那個維度 1 去掉,這樣 scores 就可以進行後續其他操作。 ```python scores = {Tensor} Tensor("Attention_layer_1/Reshape_3:0", shape=(?, 1, ?), dtype=float32) facts = {Tensor} Tensor("rnn_1/gru1/transpose:0", shape=(?, ?, 36), dtype=float32) scores 的變數是: [128 1 4] [ [[0.250200331 0.250034541 0.249927863 0.249837205]] [[0.250214398 0.250093609 0.249850363 0.249841616]] [[0.250217527 0.250093311 0.249850243 0.249838948]] ..... ] scores = tf.reshape(scores, [-1, tf.shape(facts)[1]]) scores = {Tensor} Tensor("Attention_layer_1/Reshape_4:0", shape=(?, ?), dtype=float32) scores 的變數是: [128 4] [ [0.250200331 0.250034541 0.249927863 0.249837205] [0.250214398 0.250093609 0.249850363 0.249841616] [0.250217527 0.250093311 0.249850243 0.249838948] ..... ] output = facts * tf.expand_dims(scores, -1) output = tf.reshape(output, tf.shape(facts)) ``` ## 0x03 expand_dims ### 3.1 expand_dims函式 expand_dims 所實現的功能是給定一個input,在axis軸處給input增加一個為1的維度。 axis=0 代表第一維度,1代表第二維度,2代表第三維度,以此類推,比如: ```javascript # 't2' is a tensor of shape [2, 3, 5] tf.shape(tf.expand_dims(t2, 0)) # [1, 2, 3, 5] ``` 如果 axis=0,矩陣維度變成`1*2*3*5`。 如果 axis=2,矩陣就會變為`2*3*5*1`。 或者使用例子更能說明問題。 #### 3.1.1 例1 比如 ```python a = [[0.1, 0.2, 0.3], [1.1, 1.2, 1.3], [2.1, 2.2, 2.3], [3.1, 3.2, 3.3], [4.1, 4.2, 4.3]] ``` 那麼 `sess.run(tf.expand_dims(a, 1))`的結果是: ```python [ [[0.1 0.2 0.3]] [[1.1 1.2 1.3]] [[2.1 2.2 2.3]] [[3.1 3.2 3.3]] [[4.1 4.2 4.3]] ] ``` 而 `sess.run(tf.expand_dims(a, -1))` 的結果是: ```python [ [[0.1] [0.2] [0.3]] [[1.1] [1.2] [1.3]] [[2.1] [2.2] [2.3]] [[3.1] [3.2] [3.3]] [[4.1] [4.2] [4.3]] ] ``` #### 3.1.2 例2 ```python a = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3] b = [1, 2, 3, 1, 2, 3] reshapeA = tf.reshape(a, (2,3,2)) reshapeB = tf.reshape(b, (2,3)) output = reshapeA * tf.expand_dims(reshapeB, -1) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) print sess.run(reshapeA) print sess.run(reshapeB) print sess.run(tf.expand_dims(reshapeB, -1)) print sess.run(output) ``` 輸出結果是: ```python # reshapeA [[[1 2] [3 1] [2 3]] [[1 2] [3 1] [2 3]]] # reshapeB [[1 2 3] [1 2 3]] # tf.expand_dims(reshapeB, -1) [[[1] [2] [3]] [[1] [2] [3]]] # output [[[1 2] [6 2] [6 9]] [[1 2] [6 2] [6 9]]] ``` ### 3.2 DIN使用 DIN程式碼中,使用expand_dims的大概有如下: 第一處使用就是把 Mask [B, T] 擴充套件為 key_masks [B, 1, T],這樣 key_masks 的維度就和scores相同,可以進行邏輯運算。 ```python # Mask # [B, T] key_masks = tf.expand_dims(mask, 1) # [B, 1, T] paddings = tf.ones_like(scores) * (-2 ** 32 + 1) if not forCnn: scores = tf.where(key_masks, scores, paddings) # [B, 1, T] ``` 第二處使用如下: ```python output = facts * tf.expand_dims(scores, -1) output = tf.reshape(output, tf.shape(facts)) ``` 結合前面例2,我們可以知道,這樣先把scores在最後增加一維,就可以進行哈達碼積 [B, T, H] x [B, T, 1] = [B, T, H]。這裡還包括張量廣播機制,我們會在其他文章中解讀。 ## 0xFF 參考 [徹底理解 tf.reduce_sum()](https://www.jianshu.com/p/30b40b504bae) [關於numy中np.expand_dims方法的理解?](https://www.zhihu.com/question/265545749) [辨析matmul product(一般矩陣乘積),hadamard product(哈達瑪積)、kronecker product(克羅內克積)](https://blog.csdn.net/yjk13703623757/article/details/77016867/) [Tensorflow 的reduce_sum()函式到底是什麼意思](http://hp.stuhome.net/index.php/2018/03/19/tensorflow-reduce_sum-me