【问题标题】:biobert for keras version of huggingface transformersbiobert for keras 版本的拥抱脸变压器
【发布时间】:2020-06-17 18:06:14
【问题描述】:

(也发布在https://github.com/dmis-lab/biobert/issues/98

您好,有人知道如何使用 huggingface 转换器(2.4.1 版)将 biobert 作为 keras 层加载吗?我尝试了几种可能性,但都没有奏效。我发现的只是如何使用 pytorch 版本,但我对 keras 层版本感兴趣。以下是我的两次尝试(我将 biobert 文件保存到文件夹“biobert_v1.1_pubmed”中)。

尝试 1:

biobert_model = TFBertModel.from_pretrained('bert-base-uncased')
biobert_model.load_weights('biobert_v1.1_pubmed/model.ckpt-1000000')

错误信息:

AssertionError: Some objects had attributes which were not restored:
    : ['tf_bert_model_4/bert/embeddings/word_embeddings/weight']
    : ['tf_bert_model_4/bert/embeddings/position_embeddings/embeddings']
   (and many more lines like above...)

尝试 2:

biobert_model = TFBertModel.from_pretrained("biobert_v1.1_pubmed/model.ckpt-1000000", config='biobert_v1.1_pubmed/bert_config.json')

错误信息:

NotImplementedError: Weights may only be loaded based on topology into Models when loading TensorFlow-formatted weights (got by_name=True to load_weights).

任何帮助表示赞赏!我对拥抱脸的变形金刚库的体验几乎为零。我也尝试加载以下两个模型,但似乎它们只支持 pytorch 版本。

【问题讨论】:

  • 您运行的是哪个版本的transformers?另外,当您尝试直接从 huggingface 加载 PyTorch 模型时,它是否有效?
  • @dennlinger,transformers 的版本是 2.4.1。不确定 pytorch 版本是否有效,我的所有其他代码都在 tensorflow-keras 中,我无法使用 pytorch 版本(除非我在 pytorch 中重新实现其他所有内容,重新训练和重新评估所有其他系统......可能会如果我决定迁移到 pytorch,那么将来会这样做,但不是现在!)
  • 我的意思是问题在于您正在加载的检查点与 Huggingface 兼容,相反,它们是适合原始 BERT 模型 by Google 的实现。因此,我认为加载模型的机会很小。如果您愿意使用 PyTorch,那么您可以通过 Google 将权重从 TF 模型导出到 PyTorch 检查点,该检查点再次与 Huggingface AFAIK 兼容。
  • 我知道权重可以转换为 huggingface 的 pytorch 版本,但我不知道它们不能转换为 huggingface 的 tf-keras 版本...我会继续努力,但感谢 dennlinger

标签: keras nlp keras-layer huggingface-transformers


【解决方案1】:

可能有点晚了,但我找到了一个不太优雅的解决这个问题的方法。转换器库中的 tf bert 模型可以通过 PyTorch 保存文件加载。

第 1 步: 使用以下命令将 tf 检查点转换为 Pytorch 保存文件(更多:https://github.com/huggingface/transformers/blob/master/docs/source/converting_tensorflow_models.rst

transformers-cli convert --model_type bert\
  --tf_checkpoint=./path/to/checkpoint_file \
  --config=./bert_config.json \
  --pytorch_dump_output=./pytorch_model.bin

第 2 步:确保将以下文件合并到一个目录中

  • config.json - bert 配置文件(必须从 bert_config.json 重命名!)
  • pytorch_model.bin - 我们刚刚转换的那个
  • vocab.txt - bert 词汇文件

第 3 步:从我们刚刚创建的目录中加载模型

model = TFBertModel.from_pretrained('./pretrained_model_dir', from_pt=True)

实际上还有一个参数“from_tf”,根据文档,它应该与 tf 样式检查点一起使用,但我无法让它工作。见:https://huggingface.co/transformers/main_classes/model.html#transformers.PreTrainedModel.from_pretrained

【讨论】:

    猜你喜欢
    • 2022-06-28
    • 2021-09-15
    • 2021-02-16
    • 2021-11-24
    • 2020-07-22
    • 2021-08-16
    • 2021-12-12
    • 2020-12-18
    • 1970-01-01
    相关资源
    最近更新 更多