TensorFlow XLNet モデルの変換

危険

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

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

XLNet (トランスフォーマーからの双方向エンコーダー表現) の事前トレーニング済みモデルは公開されています。

サポートされるモデル

事前トレーニングされた XLNet モデルリストの次のモデルが現在サポートされています。

事前トレーニング済みのベース XLNet モデルのダウンロード

XLNet-Base、Cased を使用してアーカイブをダウンロードして解凍します。

アーカイブが解凍されると、次のファイルが含まれるディレクトリー cased_L-12_H-768_A-12 が作成されます。

  • 事前トレーニングされた重み (3 つのファイル) を含む TensorFlow チェックポイント (xlnet_model.ckpt)

  • (非) トークン化に使用される文章モデル (spiece.model)

  • モデルのハイパーパラメーターを指定する構成ファイル (xlnet_config.json)

アーカイブから pb-file を取得するには、次の手順を実行する必要があります。

  1. コマンドを実行

    cd ~
    mkdir XLNet-Base
    cd XLNet-Base
    git clone https://github.com/zihangdai/xlnet
    wget https://storage.googleapis.com/xlnet/released_models/cased_L-12_H-768_A-12.zip
    unzip cased_L-12_H-768_A-12.zip
    mkdir try_save
    
  2. 次の Python スクリプトを ~/XLNet-Base/xlnet に保存して実行します。

    元のモデル・リポジトリーは、Python2 上の TensorFlow 1.13.1 でテストされています。

    from collections import namedtuple
    
    import tensorflow as tf
    from tensorflow.python.framework import graph_io
    
    import model_utils
    import xlnet
    
    LENGTHS = 50
    BATCH = 1
    OUTPUT_DIR = '~/XLNet-Base/try_save/'
    INIT_CKPT_PATH = '~/XLNet-Base/xlnet_cased_L-12_H-768_A-12/xlnet_model.ckpt'
    XLNET_CONFIG_PATH = '~/XLNet-Base/xlnet_cased_L-12_H-768_A-12/xlnet_config.json'
    
    FLags = namedtuple('FLags', 'use_tpu init_checkpoint')
    FLAGS = FLags(use_tpu=False, init_checkpoint=INIT_CKPT_PATH)
    
    xlnet_config = xlnet.XLNetConfig(json_path=XLNET_CONFIG_PATH)
    run_config = xlnet.RunConfig(is_training=False, use_tpu=False, use_bfloat16=False, dropout=0.1, dropatt=0.1,)
    
    
    sentence_features_input_idx = tf.compat.v1.placeholder(tf.int32, shape=[LENGTHS, BATCH], name='input_ids')
    sentence_features_segment_ids = tf.compat.v1.placeholder(tf.int32, shape=[LENGTHS, BATCH], name='seg_ids')
    sentence_features_input_mask = tf.compat.v1.placeholder(tf.float32, shape=[LENGTHS, BATCH], name='input_mask')
    
    with tf.compat.v1.Session() as sess:
       xlnet_model = xlnet.XLNetModel(xlnet_config=xlnet_config, run_config=run_config,
                                     input_ids=sentence_features_input_idx,
                                     seg_ids=sentence_features_segment_ids,
                                     input_mask=sentence_features_input_mask)
    
       sess.run(tf.compat.v1.global_variables_initializer())
       model_utils.init_from_checkpoint(FLAGS, True)
    
       # Save the variables to disk.
       saver = tf.compat.v1.train.Saver()
    
       # Saving checkpoint
       save_path = saver.save(sess, OUTPUT_DIR + "model.ckpt")
    
       # Freezing model
       outputs = ['model/transformer/dropout_2/Identity']
       graph_def_freezed = tf.compat.v1.graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), outputs)
    
       # Saving non-frozen and frozen  model to pb
       graph_io.write_graph(sess.graph.as_graph_def(), OUTPUT_DIR, 'model.pb', as_text=False)
       graph_io.write_graph(graph_def_freezed,OUTPUT_DIR, 'model_frozen.pb',
                            as_text=False)
    
       # Write to tensorboard
       with tf.compat.v1.summary.FileWriter(logdir=OUTPUT_DIR, graph_def=graph_def_freezed) as writer:
          writer.flush()
    

事前トレーニング済みの大規模 XLNet モデルのダウンロード

XLNet-Base、Cased を使用してアーカイブをダウンロードして解凍します。

アーカイブが解凍されると、次のファイルが含まれるディレクトリー cased_L-12_H-1024_A-16 が作成されます。

  • 事前トレーニングされた重み (3 つのファイル) を含む TensorFlow チェックポイント (xlnet_model.ckpt)

  • (非) トークン化に使用される文章モデル (spiece.model)

  • モデルのハイパーパラメーターを指定する構成ファイル (xlnet_config.json)

アーカイブから pb-file を取得するには、次の手順を実行する必要があります。

  1. コマンドを実行します。

    cd ~
    mkdir XLNet-Large
    cd XLNet-Large
    git clone https://github.com/zihangdai/xlnet
    wget https://storage.googleapis.com/xlnet/released_models/cased_L-24_H-1024_A-16.zip
    unzip cased_L-24_H-1024_A-16.zip
    mkdir try_save
    
  2. 次の Python スクリプトを ~/XLNet-Large/xlnet に保存して実行します。

    from collections import namedtuple
    
    import tensorflow as tf
    from tensorflow.python.framework import graph_io
    
    import model_utils
    import xlnet
    
    LENGTHS = 50
    BATCH = 1
    OUTPUT_DIR = '~/XLNet-Large/try_save'
    INIT_CKPT_PATH = '~/XLNet-Large/cased_L-24_H-1024_A-16/xlnet_model.ckpt'
    XLNET_CONFIG_PATH = '~/XLNet-Large/cased_L-24_H-1024_A-16/xlnet_config.json'
    
    FLags = namedtuple('FLags', 'use_tpu init_checkpoint')
    FLAGS = FLags(use_tpu=False, init_checkpoint=INIT_CKPT_PATH)
    
    xlnet_config = xlnet.XLNetConfig(json_path=XLNET_CONFIG_PATH)
    run_config = xlnet.RunConfig(is_training=False, use_tpu=False, use_bfloat16=False, dropout=0.1, dropatt=0.1,)
    
    
    sentence_features_input_idx = tf.compat.v1.placeholder(tf.int32, shape=[LENGTHS, BATCH], name='input_ids')
    sentence_features_segment_ids = tf.compat.v1.placeholder(tf.int32, shape=[LENGTHS, BATCH], name='seg_ids')
    sentence_features_input_mask = tf.compat.v1.placeholder(tf.float32, shape=[LENGTHS, BATCH], name='input_mask')
    
    with tf.compat.v1.Session() as sess:
       xlnet_model = xlnet.XLNetModel(xlnet_config=xlnet_config, run_config=run_config,
                                     input_ids=sentence_features_input_idx,
                                     seg_ids=sentence_features_segment_ids,
                                     input_mask=sentence_features_input_mask)
    
       sess.run(tf.compat.v1.global_variables_initializer())
       model_utils.init_from_checkpoint(FLAGS, True)
    
       # Save the variables to disk.
       saver = tf.compat.v1.train.Saver()
    
       # Saving checkpoint
       save_path = saver.save(sess, OUTPUT_DIR + "model.ckpt")
    
       # Freezing model
       outputs = ['model/transformer/dropout_2/Identity']
       graph_def_freezed = tf.compat.v1.graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), outputs)
    
       # Saving non-frozen and frozen  model to pb
       graph_io.write_graph(sess.graph.as_graph_def(), OUTPUT_DIR, 'model.pb', as_text=False)
       graph_io.write_graph(graph_def_freezed,OUTPUT_DIR, 'model_frozen.pb',
                            as_text=False)
    
       # Write to tensorboard
       with tf.compat.v1.summary.FileWriter(logdir=OUTPUT_DIR, graph_def=graph_def_freezed) as writer:
          writer.flush()
    

スクリプトは ~/XLNet-Large/xlnet に保存する必要があります。

凍結された TensorFlow XLNet モデルを IR に変換

モデルの XLNet 中間表現 (IR) を生成するには、次のパラメーターを使用してモデル変換を実行します。

mo --input_model path-to-model/model_frozen.pb \
   --input "input_mask[50,1],input_ids[50,1],seg_ids[50,1]"