python - 未能加载 tensorflow BERT 预训练模型
问题描述
我尝试加载一个 BERT 预训练模型来执行 NER 任务。但是系统找不到预训练的模型文件。
我在终端中使用了以下代码,该文件夹包含 model.ckpt-1000000、model.ckpt-1000000.index、model.ckpt-1000000.meta 文件。
python run_ner.py \
--do_train=true \
--do_eval=true \
--vocab_file=vocab.txt \
--bert_config_file=bert_config.json \
--init_checkpoint=model.ckpt-1000000 \
--num_train_epochs=10.0 \
--data_dir=NCBI-disease/ \
--output_dir=epoch1
错误信息是
2019-08-04 23:26:41.272281: W tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at save_restore_v2_ops.cc:184 : Not found: model.ckpt-1000000.data-00000-of-00001; No such file or directory
File "/Users/anaconda/envs/BIOBERT/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1328, in _do_run
run_metadata)
File "/Users/anaconda/envs/BIOBERT/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1348, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: model.ckpt-1000000.data-00000-of-00001; No such file or directory
[[node checkpoint_initializer_161 (defined at run_ner.py:422) ]]
注意:模型文件的原始名称为model.ckpt-1000000.data-00000-of-00001、model.ckpt-1000000.index和model.ckpt-1000000.meta。我也试过
python run_ner.py \
--do_train=true \
--do_eval=true \
--vocab_file=vocab.txt \
--bert_config_file=bert_config.json \
--init_checkpoint=model.ckpt-1000000.data-00000-of-00001 \
--num_train_epochs=10.0 \
--data_dir=NCBI-disease/ \
--output_dir=epoch1
那么错误将是
File "/Users/anaconda/envs/BIOBERT/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 326, in NewCheckpointReader
return CheckpointReader(compat.as_bytes(filepattern), status)
File "/Users/SichengZhou/anaconda/envs/BIOBERT/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 528, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.DataLossError: Unable to open table file ./model.ckpt-1000000.data-00000-of-00001: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?
解决方案
模型文件的原始名称应为model.ckpt-1000000.data-00000-of-00001、model.ckpt-1000000.index和model.ckpt-1000000.meta。
您收到的错误是因为 ckpt 文件和您正在运行的 python 文件(run_ner.py)位于不同的路径中。
python run_ner.py \
--do_train=true \
--do_eval=true \
--vocab_file=vocab.txt \
--bert_config_file=bert_config.json \
--init_checkpoint=<path to folder where ckpt files are saved>/model.ckpt-1000000 \
--num_train_epochs=10.0 \
--data_dir=NCBI-disease/ \
--output_dir=epoch1
运行时不要更改--init_checkpoint=model.ckpt-1000000
为。--init_checkpoint=model.ckpt-1000000.data-00000-of-00001
run_ner.py
希望这可以帮助。
推荐阅读
- filter - 堆叠在查询公式上
- ruby-on-rails - Plesk Obsidian & Rails 应用程序与公用文件夹失败
- google-analytics - BigQuery 数据与 Google Merchandise Store 演示帐户之间的差异
- pandas - pandas 从 groupby 和 max 返回辅助列
- flutter - 尝试上传新版本 APK 文件时,AirWatch 错误应用程序版本已存在
- python - Python 类范围在嵌套递归函数中丢失
- c# - 如何在由单词组成的 Trie 中节省空间?
- qemu - 如何创建一个新的系统或电路板来被 QEMU 模拟
- python - 奇怪的`ArviZ`安装问题
- postgresql - Postgres加入不区分大小写的文本字段