如何使用经过训练的 BERT 模型检查点进行预测?

Jee*_*thi 5 python neural-network tensorflow google-cloud-tpu bert-language-model

我使用 SQUAD 2.0 训练了 BERT,并使用BERT-master在输出目录中获得了model.ckpt.data, model.ckpt.meta, model.ckpt.index(F1 score : 81) 以及predictions.json, 等等/run_squad.py

python run_squad.py \
  --vocab_file=$BERT_LARGE_DIR/vocab.txt \
  --bert_config_file=$BERT_LARGE_DIR/bert_config.json \
  --init_checkpoint=$BERT_LARGE_DIR/bert_model.ckpt \
  --do_train=True \
  --train_file=$SQUAD_DIR/train-v2.0.json \
  --do_predict=True \
  --predict_file=$SQUAD_DIR/dev-v2.0.json \
  --train_batch_size=24 \
  --learning_rate=3e-5 \
  --num_train_epochs=2.0 \
  --max_seq_length=384 \
  --doc_stride=128 \
  --output_dir=gs://some_bucket/squad_large/ \
  --use_tpu=True \
  --tpu_name=$TPU_NAME \
  --version_2_with_negative=True
Run Code Online (Sandbox Code Playgroud)

我尝试将model.ckpt.meta, model.ckpt.index,复制model.ckpt.data$BERT_LARGE_DIR目录并按run_squad.py如下方式更改标志以仅预测答案而不使用数据集进行训练:

python run_squad.py \
  --vocab_file=$BERT_LARGE_DIR/vocab.txt \
  --bert_config_file=$BERT_LARGE_DIR/bert_config.json \
  --init_checkpoint=$BERT_LARGE_DIR/model.ckpt \
  --do_train=False \
  --train_file=$SQUAD_DIR/train-v2.0.json \
  --do_predict=True \
  --predict_file=$SQUAD_DIR/dev-v2.0.json \
  --train_batch_size=24 \
  --learning_rate=3e-5 \
  --num_train_epochs=2.0 \
  --max_seq_length=384 \
  --doc_stride=128 \
  --output_dir=gs://some_bucket/squad_large/ \
  --use_tpu=True \
  --tpu_name=$TPU_NAME \
  --version_2_with_negative=True
Run Code Online (Sandbox Code Playgroud)

它抛出bucket directory/model.ckpt不存在错误。

如何利用训练后生成的检查点并将其用于预测?

Ash*_*'Sa 3

通常,训练后的检查点是在训练时在参数指定的目录中创建的--output_dir。(这就是gs://some_bucket/squad_large/你的情况)。每个检查站都会有一个编号。你必须找出最大的数字;例子:model.ckpt-12345。现在,--init_checkpoint使用输出目录和最后保存的检查点(编号最高的模型)在评估/预测中设置参数。(在你的情况下,它应该是类似的--init_checkpoint=gs://some_bucket/squad_large/model.ckpt-<highest number>