経験的記憶(純粋な手作業)=>TF 20のLSTMとGRU(return_sequencesとreturn_state)パラメータソースコード


前言
お知らせ:
      :   LSTM   GRU   ,    Tensorflow20 LSTM GRU     )

追加の説明
ソースコードを見ると背が高くない.いろいろなブログをめくってみると、内容は互いに引用するのではなく、互いに「参考」していることに気づきます.のそして絶望する時.ドキュメントをめくるかもしれませんが、詳細ではないドキュメントもあります.この時、ソースコードを見るのがあなたの最善の理解方法です.(LSTMとGRU部分のソースコードはやはりきれいです)
タイトルが書けない:TF 20=>Tensorflow 2.0(Stable)tk ===> tensorflow.kerasLSTMとGRUはすでにtkに置かれている.layersモジュールにあります.
return_sequences = True
return_state = True

               ,   LSTM   GRU    。
           ???
 ,   !

      :
    import tensorflow.keras as tk
    tk.layers.GRU()
    tk.layers.LSTM()
     pycharm ctrl+         ~~~

LSTMソース
メインソースの一部を切り取りました.
...
...
  states = [new_h, new_c]           #    ,        h,         c

if self.return_sequences:         #   return_sequences  True
  output = outputs                    #        LSTM      y,    return
else:                             #   return_sequences  False
  output = last_output                #     LSTM         ,     return

if self.return_state:             #   return_state  False
  return [output] + list(states)      #          output + [new_h, new_c]
else:                             #   return_state  False
  return output                       #             output

   :    return    。        ,       。 

GRUソース
...
...
########            #########################################
  last_output, outputs, runtime, states = self._defun_gru_call( 
      inputs, initial_state, training, mask)
#####################################################################          
...
...

#########       ,         LSTM       ###################
if self.return_sequences:
  output = outputs
else:
  output = last_output

if self.return_state:
  return [output] + list(states)
else:
  return output

今私たちが探しているポイントは、statesがどうやって手に入れたのかだけです.?「self._defun_gru_call」という関数のソースコードをクリックし続けると、statesが直接露出していることがわかります.
states = [new_h]
return ..., states

現在、ソースコードのほとんどの分析が完了しています.振り返ってまとめてみましょう.
LSTM   GRU    return_sequences   return_state            !!!
    return_sequences:     output     ,(             )
    return_state:      output  ,                  states  
    
               output  ,   states  :

LSTM   GRU   output  :     ,   。
LSTM   GRU   ststes  :
    LSTM  states  :  [H, C]    #      LSTM   ,          ,LSTM C H
    GRU  states  :   [H]       #      GRU   ,          ,GRU   H

最終使用層の概要:
LSTM:
次の4つの組み合わせがあります.
  • return_sequences=Falseかつreturn_state=False(デフォルト)
       :          LSTM     Y 
    
  • return_sequences=Trueかつreturn_state = False
       :        LSTM     Y  
    
  • return_sequences=Falseかつreturn_state = True
       :       LSTM     Y        C + H   (    )
  • return_sequences=Trueかつreturn_state = True
       :     LSTM     Y     C + H   (    )  (   Atention)

  • GRU:
    次の4つの組み合わせがあります.
  • return_sequences=Falseかつreturn_state=False(デフォルト)
       :  LSTM
    
  • return_sequences=Trueかつreturn_state = False
       :  LSTM 
    
  • return_sequences=Falseかつreturn_state = True
       :         LSTM     Y         H(    )
  • return_sequences=Trueかつreturn_state = True
       :       LSTM     Y      H(    )  (   Atention)