【发布时间】: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