我正在研究模型的可解释性。我想使用AllenAI演示来检查其他模型上的显着性映射和对抗性攻击方法(在本演示中实现)。我使用教程这里并在本地机器上运行演示。现在,我想要加载经过预先训练的模型,这是使用这个("cardiffnlp/twitter-roberta-base-sentiment-latest“从hugging面代码加载的),我不知道如何将模型添加到演示中。我查看了教程这里,但本指南仅基于在AllenNLP中实现的模型。
这些是我在roberta_sentiment_twitter文件中对新目录( allennlp_demo )所做的更改,但肯定不是这样,因为主实现只使用allennlp中实现的模型。
#in model.json
{
"id": "roberta-sentiment-twitter",
"pretrained_model_id": "cardiffnlp/twitter-roberta-base-sentiment-latest"
}
#in api.py
import os
from allennlp_demo.common import config, http
from transformers import AutoModelForSequenceClassification
from transformers import AutoTokenizer, AutoConfig
if __name__ == "__main__":
MODEL = f"cardiffnlp/twitter-roberta-base-sentiment-latest"
tokenizer = AutoTokenizer.from_pretrained(MODEL)
config = AutoConfig.from_pretrained(MODEL)
# model = AutoModelForSequenceClassification.from_pretrained(MODEL)
endpoint = AutoModelForSequenceClassification.from_pretrained(MODEL)
endpoint.run()
#in test_api.py
from allennlp_demo.common.testing import ModelEndpointTestCase
from allennlp_demo.roberta_sentiment_twitter.api import RobertaSentimentAnalysisModelEndpoint
class TestRobertaSentimentTwitterModelEndpoint(ModelEndpointTestCase):
endpoint = RobertaSentimentAnalysisModelEndpoint()
predict_input = {"sentence": "a very well-made, funny and entertaining picture."}有什么简单的方法可以在AllenNLP演示中加载我的模型吗?
在将来,我还想向这个演示中添加一些其他的可解释性方法。对此也有什么指导吗?
发布于 2022-06-10 23:29:30
如果您只是对从各种显着性解释器获得输出感兴趣,这个指南章解释了如何使用API (您不需要前端演示代码)。如果您想将解释器应用于您的自定义模型,您可能还会发现如何在自定义模型上使用Allen NLP解释很有帮助。
为了添加自定义解释器,可以从源安装allennlp,并将方法添加到allennlp/解释器中。你也可以在https://github.com/allenai/allennlp做个公关。
https://stackoverflow.com/questions/72469258
复制相似问题