首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在AllenNLP之外添加一个预先训练过的模型到AllenNLP演示中

在AllenNLP之外添加一个预先训练过的模型到AllenNLP演示中
EN

Stack Overflow用户
提问于 2022-06-02 00:10:32
回答 1查看 119关注 0票数 0

我正在研究模型的可解释性。我想使用AllenAI演示来检查其他模型上的显着性映射和对抗性攻击方法(在本演示中实现)。我使用教程这里并在本地机器上运行演示。现在,我想要加载经过预先训练的模型,这是使用这个("cardiffnlp/twitter-roberta-base-sentiment-latest“从hugging面代码加载的),我不知道如何将模型添加到演示中。我查看了教程这里,但本指南仅基于在AllenNLP中实现的模型。

这些是我在roberta_sentiment_twitter文件中对新目录( allennlp_demo )所做的更改,但肯定不是这样,因为主实现只使用allennlp中实现的模型。

代码语言:javascript
复制
#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演示中加载我的模型吗?

在将来,我还想向这个演示中添加一些其他的可解释性方法。对此也有什么指导吗?

EN

回答 1

Stack Overflow用户

发布于 2022-06-10 23:29:30

如果您只是对从各种显着性解释器获得输出感兴趣,这个指南章解释了如何使用API (您不需要前端演示代码)。如果您想将解释器应用于您的自定义模型,您可能还会发现如何在自定义模型上使用Allen NLP解释很有帮助。

为了添加自定义解释器,可以从源安装allennlp,并将方法添加到allennlp/解释器中。你也可以在https://github.com/allenai/allennlp做个公关。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72469258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档