首页 > 解决方案 > 将 HF 变压器 BERT 模型加载到 Allen NLP 解释模块中

问题描述

我正在寻找一种将 Allen NLP 解释模块与 HuggingFace 转换器 BERT 架构 BertForSequenceClassification 模型(用 pytorch 训练)结合使用的方法。

我找到了一个 Allen NLP 指南,其中介绍了解释模块的常见用法以及与我的模型结构不同的模型。

有人可以帮助我并描述转换可以加载到Predictor对象中的 BertForSequenceClassification 模型的正确方法是什么?

在https://guide.allennlp.org/interpret#3找到的指南示例:

from allennlp.interpret.saliency_interpreters import SimpleGradient
from allennlp.predictors import Predictor

inputs = {"sentence": "a very well-made, funny and entertaining picture."}
archive = (
    "https://storage.googleapis.com/allennlp-public-models/"
    "basic_stanford_sentiment_treebank-2020.06.09.tar.gz"
)
predictor = Predictor.from_path(archive)
interpreter = SimpleGradient(predictor)
interpretation = interpreter.saliency_interpret_from_json(inputs)

print(interpretation)

标签: huggingface-transformersallennlp

解决方案


推荐阅读