10 億ワードのベンチマークで TensorFlow 言語モデルを変換

危険

ここで説明されているコードは非推奨になりました。従来のソリューションの適用を避けるため使用しないでください。下位互換性を確保するためにしばらく保持されますが、最新のアプリケーションでは使用してはなりません

このガイドでは、非推奨となった変換方法について説明します。新しい推奨方法に関するガイドは、Python チュートリアルに記載されています。

10 億ワードのベンチマークで TensorFlow 言語モデルを変換

TensorFlow は、10 億ワードのベンチマークで事前トレーニングされた言語モデルを提供します。

IR 変換用のモデルをダウンロードするには、次の手順に従います。

  1. モデルを保存する新しいディレクトリーを作成します。

    mkdir lm_1b
    
  2. lm_1b ディレクトリーに移動します。

    cd lm_1b
    
  3. モデルの GraphDef ファイルをダウンロードします。

    wget http://download.tensorflow.org/models/LM_LSTM_CNN/graph-2016-09-10.pbtxt
    
  4. 12 個のチェックポイント共有ファイルを保存する新しいディレクトリーを作成します。

    mkdir ckpt
    
  5. ckpt ディレクトリーに移動します。

    cd ckpt
    
  6. 12 個のチェックポイント共有ファイルをダウンロードします。

    wget http://download.tensorflow.org/models/LM_LSTM_CNN/all_shards-2016-09-10/ckpt-base
    wget http://download.tensorflow.org/models/LM_LSTM_CNN/all_shards-2016-09-10/ckpt-char-embedding
    wget http://download.tensorflow.org/models/LM_LSTM_CNN/all_shards-2016-09-10/ckpt-lstm
    wget http://download.tensorflow.org/models/LM_LSTM_CNN/all_shards-2016-09-10/ckpt-softmax0
    wget http://download.tensorflow.org/models/LM_LSTM_CNN/all_shards-2016-09-10/ckpt-softmax1
    wget http://download.tensorflow.org/models/LM_LSTM_CNN/all_shards-2016-09-10/ckpt-softmax2
    wget http://download.tensorflow.org/models/LM_LSTM_CNN/all_shards-2016-09-10/ckpt-softmax3
    wget http://download.tensorflow.org/models/LM_LSTM_CNN/all_shards-2016-09-10/ckpt-softmax4
    wget http://download.tensorflow.org/models/LM_LSTM_CNN/all_shards-2016-09-10/ckpt-softmax5
    wget http://download.tensorflow.org/models/LM_LSTM_CNN/all_shards-2016-09-10/ckpt-softmax6
    wget http://download.tensorflow.org/models/LM_LSTM_CNN/all_shards-2016-09-10/ckpt-softmax7
    wget http://download.tensorflow.org/models/LM_LSTM_CNN/all_shards-2016-09-10/ckpt-softmax8
    

事前トレーニングされたモデルファイルをダウンロードすると、次の階層を持つ lm_1b ディレクトリーが作成されます。

lm_1b/
    graph-2016-09-10.pbtxt
    ckpt/
        ckpt-base
        ckpt-char-embedding
        ckpt-lstm
        ckpt-softmax0
        ckpt-softmax1
        ckpt-softmax2
        ckpt-softmax3
        ckpt-softmax4
        ckpt-softmax5
        ckpt-softmax6
        ckpt-softmax7
        ckpt-softmax8
../../../../../../_images/lm_1b.svg

凍結されたモデルには、VariableVariable_1 の 2 つの変数が残っています。これは、モデルが推論されるたびに変数をトレーニングし続けることを意味します。

このグラフの最初の推論時に、変数は初期値で初期化されます。lstm ノードの実行後、実行結果はこれら 2 つの変数に代入されます。

lm_1b グラフの推論ごとに、lstm 初期状態データは前回の推論の変数から取得され、lstm の現在の推論の状態が同じ変数に再割り当てされます。

これは、モデルが入力として受け取る単語のコンテキストを記憶するのに役立ちます。

10 億ワードのベンチマークによる TensorFlow 言語モデルを IR に変換

モデル・オプティマイザーは、出力モデルが推論のみを目的としていると想定します。したがって、これらの変数を削除し、アプリケーション・レベルでセルと非表示の状態を維持を解決する必要があります。

モデルの変換には制限があり、元のモデルの形状を変更できないため、元の形状を保持する必要があります。

lm_1b 中間表現 (IR) を生成するには、TensorFlow lm_1b モデルをパラメーターとともにモデル・オプティマイザーに提供します。

mo
--input_model lm_1b/graph-2016-09-10.pbtxt  \
--input_checkpoint lm_1b/ckpt               \
--input_model_is_text                       \
--input_shape [50],[50],[1,9216],[1,9216]    \
--output softmax_out,lstm/lstm_0/concat_2,lstm/lstm_1/concat_2 \
--input char_embedding/EmbeddingLookupUnique/Unique:0,char_embedding/EmbeddingLookupUnique/Unique:1,Variable/read,Variable_1/read

説明:

  • --input char_embedding/EmbeddingLookupUnique/Unique:0,char_embedding/EmbeddingLookupUnique/Unique:1,Variable/read,Variable_1/read および --input_shape [50],[50],[1,9216],[1,9216] は、変数をプレースホルダーに置き換えます。

  • --output softmax_out,lstm/lstm_0/concat_2,lstm/lstm_1/concat_2 は出力ノード名と LSTM セル状態の名前を指定します。