小编Sam*_* He的帖子

在 Huggingface 变压器管道中使用非默认模型时出现 KeyError

我在情感分析管道中使用默认模型没有任何问题。

# Allocate a pipeline for sentiment-analysis
nlp = pipeline('sentiment-analysis')

nlp('I am a black man.')

>>>[{'label': 'NEGATIVE', 'score': 0.5723695158958435}]
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试通过添加特定模型来稍微自定义管道时。它抛出一个KeyError。

nlp = pipeline('sentiment-analysis',
               tokenizer = AutoTokenizer.from_pretrained("DeepPavlov/bert-base-cased-conversational"),
               model = AutoModelWithLMHead.from_pretrained("DeepPavlov/bert-base-cased-conversational"))

nlp('I am a black man.')



>>>---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-55-af7e46d6c6c9> in <module>
      3                tokenizer = AutoTokenizer.from_pretrained("DeepPavlov/bert-base-cased-conversational"),
      4             model = AutoModelWithLMHead.from_pretrained("DeepPavlov/bert-base-cased-conversational"))
----> 5 nlp('I am a black man.')
      6 
      7 

~/opt/anaconda3/lib/python3.7/site-packages/transformers/pipelines.py in __call__(self, *args, **kwargs)
    721         outputs = super().__call__(*args, **kwargs)
    722         scores = np.exp(outputs) / np.exp(outputs).sum(-1, keepdims=True)
--> 723 …
Run Code Online (Sandbox Code Playgroud)

huggingface-transformers

5
推荐指数
1
解决办法
3932
查看次数

标签 统计

huggingface-transformers ×1