拥抱脸的 Transformer 模型在 Google Colab 上没有给出所需的输出

问题描述 投票:0回答:1

我尝试运行模型“AdapterHub/bert-base-uncased-pf-conll2003”(此处的模型描述)进行 NLP 中的标记分类。我使用 Google Colab 来完成此任务。

代码块是

from transformers import AutoModelWithHeads
from transformers import pipeline
from transformers import AutoTokenizer

model = AutoModelWithHeads.from_pretrained("bert-base-uncased")
adapter_name = model.load_adapter("AdapterHub/bert-base-uncased-pf-conll2003", source="hf")
model.active_adapters = adapter_name

tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
token_classification = pipeline("token-classification", model=model, tokenizer=tokenizer)
res = token_classification("Take out the trash bag from the bin and replace it.")
print(res)

我没有得到想要的输出。我正在尝试找出词性。

我收到了带有一些指令的输出,例如

The model 'BertModelWithHeads' is not supported for token-classification. Supported models are ['AlbertForTokenClassification', 'BertForTokenClassification', 'BigBirdForTokenClassification', 'BloomForTokenClassification', 'CamembertForTokenClassification', 'CanineForTokenClassification', 'ConvBertForTokenClassification', 'Data2VecTextForTokenClassification', 'DebertaForTokenClassification', 'DebertaV2ForTokenClassification', 'DistilBertForTokenClassification', 'ElectraForTokenClassification', 'ErnieForTokenClassification', 'EsmForTokenClassification', 'FlaubertForTokenClassification', 'FNetForTokenClassification', 'FunnelForTokenClassification', 'GPT2ForTokenClassification', 'GPT2ForTokenClassification', 'IBertForTokenClassification', 'LayoutLMForTokenClassification', 'LayoutLMv2ForTokenClassification', 'LayoutLMv3ForTokenClassification', 'LiltForTokenClassification', 'LongformerForTokenClassification', 'LukeForTokenClassification', 'MarkupLMForTokenClassification', 'MegatronBertForTokenClassification', 'MobileBertForTokenClassification', 'MPNetForTokenClassification', 'NezhaForTokenClassification', 'NystromformerForTokenClassification', 'QDQBertForTokenClassification', 'RemBertForTokenClassification', 'RobertaForTokenClassification', 'RobertaPreLayerNormForTokenClassification', 'RoCBertForTokenClassification', 'RoFormerForTokenClassification', 'SqueezeBertForTokenClassification', 'XLMForTokenClassification', 'XLMRobertaForTokenClassification', 'XLMRobertaXLForTokenClassification', 'XLNetForTokenClassification', 'YosoForTokenClassification', 'XLMRobertaAdapterModel', 'RobertaAdapterModel', 'AlbertAdapterModel', 'BeitAdapterModel', 'BertAdapterModel', 'BertGenerationAdapterModel', 'DistilBertAdapterModel', 'DebertaV2AdapterModel', 'DebertaAdapterModel', 'BartAdapterModel', 'MBartAdapterModel', 'GPT2AdapterModel', 'GPTJAdapterModel', 'T5AdapterModel', 'ViTAdapterModel'].
[{'entity': 'LABEL_0', 'score': 0.99992573, 'index': 1, 'word': 'take', 'start': 0, 'end': 4}, {'entity': 'LABEL_0', 'score': 0.99998, 'index': 2, 'word': 'out', 'start': 5, 'end': 8}, {'entity': 'LABEL_0', 'score': 0.9999567, 'index': 3, 'word': 'the', 'start': 9, 'end': 12}, {'entity': 'LABEL_0', 'score': 0.99988997, 'index': 4, 'word': 'trash', 'start': 13, 'end': 18}, {'entity': 'LABEL_0', 'score': 0.99968195, 'index': 5, 'word': 'bag', 'start': 19, 'end': 22}, {'entity': 'LABEL_0', 'score': 0.9999827, 'index': 6, 'word': 'from', 'start': 23, 'end': 27}, {'entity': 'LABEL_0', 'score': 0.99989295, 'index': 7, 'word': 'the', 'start': 28, 'end': 31}, {'entity': 'LABEL_0', 'score': 0.9999454, 'index': 8, 'word': 'bin', 'start': 32, 'end': 35}, {'entity': 'LABEL_0', 'score': 0.9999777, 'index': 9, 'word': 'and', 'start': 36, 'end': 39}, {'entity': 'LABEL_0', 'score': 0.9999732, 'index': 10, 'word': 'replace', 'start': 40, 'end': 47}, {'entity': 'LABEL_0', 'score': 0.9999782, 'index': 11, 'word': 'it', 'start': 48, 'end': 50}, {'entity': 'LABEL_0', 'score': 0.9999728, 'index': 12, 'word': '.', 'start': 50, 'end': 51}]
 

提前致谢

python nlp google-colaboratory huggingface-transformers bert-language-model
1个回答
1
投票
# be sure you have the dependencies
pip install adapters
pip install -U adapter-transformers

在管道之外创建模型

from transformers import AutoModelWithHeads
from transformers import pipeline

model = AutoModelWithHeads.from_pretrained("bert-base-uncased")
adapter_name = model.load_adapter("AdapterHub/bert-base-uncased-pf-conll2003", source="hf")
model.active_adapters = adapter_name

token_classification = pipeline("token-classification", model=model)
res = token_classification("Take out the trash bag from the bin and replace it.")
print(res)
© www.soinside.com 2019 - 2024. All rights reserved.