我使用AllenNLP对一个用于语义角色标记的BERT模型进行了精细的调整。这会产生一个模型目录(如果还记得的话,序列化目录?)其中包含以下内容:
best.th
config.json
meta.json
metrics_epoch_0.json
metrics_epoch_10.json
metrics_epoch_11.json
metrics_epoch_12.json
metrics_epoch_13.json
metrics_epoch_14.json
metrics_epoch_1.json
metrics_epoch_2.json
metrics_epoch_3.json
metrics_epoch_4.json
metrics_epoch_5.json
metrics_epoch_6.json
metrics_epoch_7.json
metrics_epoch_8.json
metrics_epoch_9.json
metrics.json
model_state_e14_b0.th
model_state_e15_b0.th
model.tar.gz
out.log
training_state_e14_b0.th
training_state_e15_b0.th
vocabulary其中vocabulary是一个包含labels.txt和non_padded_namespaces.txt的文件夹。
现在,我希望在学习相关任务时,使用此库:https://github.com/wilsonlau-uw/BERT-EE (即我想利用一些传输学习)来学习相关任务事件提取时,使用这个经过精细调整的模型BERT模型作为初始化。config.ini文件有一个用于fine_tuned_path的行,在这里我可以指定一个已经经过精细调整的模型。我提供了AllenNLP序列化目录的路径,并得到以下错误:
2022-04-05 13:07:28,112 - INFO - setting seed 23
2022-04-05 13:07:28,113 - INFO - loading fine tuned model in /data/projects/SRL/ser_pure_clinical_bert-large_thyme_and_ontonotes/
Traceback (most recent call last):
File "main.py", line 65, in <module>
model = BERT_EE()
File "/data/projects/SRL/BERT-EE/model.py", line 88, in __init__
self.__build(self.use_fine_tuned)
File "/data/projects/SRL/BERT-EE/model.py", line 118, in __build
self.__get_pretrained(self.fine_tuned_path)
File "/data/projects/SRL/BERT-EE/model.py", line 110, in __get_pretrained
self.__model = BERT_EE_model.from_pretrained(path)
File "/home/richier/anaconda3/envs/allennlp/lib/python3.7/site-packages/transformers/modeling_utils.py", line 1109, in from_pretrained
f"Error no file named {[WEIGHTS_NAME, TF2_WEIGHTS_NAME, TF_WEIGHTS_NAME + '.index', FLAX_WEIGHTS_NAME]} found in "
OSError: Error no file named ['pytorch_model.bin', 'tf_model.h5', 'model.ckpt.index', 'flax_model.msgpack'] found in directory /data/projects/SRL/ser_pure_clinical_bert-large_thyme_and_ontonotes/ or `from_tf` and `from_flax` set to False.当然,序列化目录中没有任何这些文件,因此出现了错误。我试过解压缩model.tar.gz,但它只有:
config.json
weights.th
vocabulary/
vocabulary/.lock
vocabulary/labels.txt
vocabulary/non_padded_namespaces.txt
meta.json深入研究上面链接的GitHub回购的代码库,我可以看到BERT_EE_model是从变压器库中继承来的,所以诀窍似乎是将AllenNLP模型变成BertPreTrainedModel.from_pretrained()可以加载的格式。
任何帮助都将不胜感激!
发布于 2022-04-07 19:04:28
我想我已经搞清楚了。基本上,我必须重新加载我的模型存档,访问底层模型和令牌程序,然后保存这些:
from allennlp.models.archival import load_archive
from allennlp_models.structured_prediction import SemanticRoleLabeler, srl, srl_bert
archive = load_archive('ser_pure_clinical_bert-large_thyme_and_ontonotes/model.tar.gz')
bert_model = archive.model.bert_model #type is transformers.models.bert.modeling_bert.BertModel
bert_model.save_pretrained('ser_pure_clinical_bert-large_thyme_and_ontonotes_save_pretrained/')
bert_tokenizer = archive.dataset_reader.bert_tokenizer
bert_tokenizer.save_pretrained('ser_pure_clinical_bert-large_thyme_and_ontonotes_save_pretrained/')(对于大多数人来说,最后一部分可能不那么有趣,但在我提到的config.ini中,目录pretrained_model_name_or_path需要传递给行pretrained_model_name_or_path而不是fine_tuned_path。)
https://stackoverflow.com/questions/71755917
复制相似问题