Google Colab 无法拥抱脸部模型

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

我喜欢使用 BERT 模型来标记词性。为此,我使用了 Hugging Face 库。

当我在 Hugging Face API 上运行模型时,我得到了输出

但是,当我在 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-ud_pos", source="hf")
model.active_adapters = adapter_name

tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
token_classification = pipeline("token-classification", model=model, tokenizer=tokenizer, aggregation_strategy="NONE")
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'].
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-18-79b43720402e> in <cell line: 12>()
     10 tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
     11 token_classification = pipeline("token-classification", model=model, tokenizer=tokenizer, aggregation_strategy="NONE")
---> 12 res = token_classification("Take out the trash bag from the bin and replace it.")
     13 print(res)

4 frames
/usr/local/lib/python3.10/dist-packages/transformers/pipelines/token_classification.py in aggregate(self, pre_entities, aggregation_strategy)
    346                 score = pre_entity["scores"][entity_idx]
    347                 entity = {
--> 348                     "entity": self.model.config.id2label[entity_idx],
    349                     "score": score,
    350                     "index": pre_entity["index"],

密钥错误:16

我不明白模型在 Hugging Face API 中运行正常,为什么无法在 Google Colab 上运行?

提前谢谢您。

python nlp google-colaboratory huggingface-transformers bert-language-model
2个回答
1
投票

您可以这样做:

from transformers import AutoModelWithHeads, AutoTokenizer, pipeline

tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
model = AutoModelWithHeads.from_pretrained("bert-base-uncased")
model.load_adapter(
    "AdapterHub/bert-base-uncased-pf-ud_pos",
    source="hf",
    set_active=True,
)
token_classification = pipeline(
    "token-classification",
    model=model,
    tokenizer=tokenizer,
)

此管道创建部分将显示警告,即

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'].

你可以忽略它,管道仍然可以工作。这是一个例子:

>>> token_classification("Take out the trash bag from the bin and replace it")
[{'entity': 'VERB','score': 0.99986637, 'index': 1, 'word': 'take', 'start': 0, 'end': 4},
 {'entity': 'ADP', 'score': 0.9829973, 'index': 2, 'word': 'out', 'start': 5, 'end': 8},
 {'entity': 'DET', 'score': 0.9998791, 'index': 3, 'word': 'the', 'start': 9, 'end': 12},
 {'entity': 'NOUN', 'score': 0.9958676, 'index': 4, 'word': 'trash', 'start': 13, 'end': 18},
 {'entity': 'NOUN', 'score': 0.99657273, 'index': 5, 'word': 'bag', 'start': 19, 'end': 22},
 {'entity': 'ADP', 'score': 0.99989176, 'index': 6, 'word': 'from', 'start': 23, 'end': 27},
 {'entity': 'DET', 'score': 0.99982834, 'index': 7, 'word': 'the', 'start': 28, 'end': 31},
 {'entity': 'NOUN', 'score': 0.99584526, 'index': 8, 'word': 'bin', 'start': 32, 'end': 35},
 {'entity': 'CCONJ', 'score': 0.99962616, 'index': 9, 'word': 'and', 'start': 36, 'end': 39},
 {'entity': 'VERB', 'score': 0.99976975, 'index': 10, 'word': 'replace', 'start': 40, 'end': 47},
 {'entity': 'PRON', 'score': 0.9989698, 'index': 11, 'word': 'it', 'start': 48, 'end': 50}]

0
投票

adapter-transformers
软件包已弃用,并由
adapters
取代(有关更多信息,请参阅介绍适配器)。

这里是基于 Riccardo 答案的更新代码和来自 适配器文档 的示例:

from transformers import BertTokenizer, pipeline, BertForTokenClassification
from adapters import BertAdapterModel

# Load pre-trained BERT tokenizer from Hugging Face
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

# Load pre-trained BERT model from Hugging Face Hub
# The `BertAdapterModel` class is specifically designed for working with adapters
# It can be used with different prediction heads
model = BertAdapterModel.from_pretrained('bert-base-uncased')

# Load pre-trained task adapter from Adapter Hub
adapter_name = model.load_adapter("AdapterHub/bert-base-uncased-pf-ud_pos", source="hf", set_active=True)

# Activate the adapter we just loaded, so that it is used in every forward pass
model.set_active_adapters(adapter_name)

token_classification = pipeline("token-classification", model=model, tokenizer=tokenizer)
token_classification("Take out the trash bag from the bin and replace it.")

这是输出:

[{'entity': 'VERB',
  'score': 0.9998685,
  'index': 1,
  'word': 'take',
  'start': None,
  'end': None},
 {'entity': 'ADP',
  'score': 0.9836626,
  'index': 2,
  'word': 'out',
  'start': None,
  'end': None},
 {'entity': 'DET',
  'score': 0.9998733,
  'index': 3,
  'word': 'the',
  'start': None,
  'end': None},
 {'entity': 'NOUN',
  'score': 0.9964223,
  'index': 4,
  'word': 'trash',
  'start': None,
  'end': None},
 {'entity': 'NOUN',
  'score': 0.9971655,
  'index': 5,
  'word': 'bag',
  'start': None,
  'end': None},
 {'entity': 'ADP',
  'score': 0.99989533,
  'index': 6,
  'word': 'from',
  'start': None,
  'end': None},
 {'entity': 'DET',
  'score': 0.9998233,
  'index': 7,
  'word': 'the',
  'start': None,
  'end': None},
 {'entity': 'NOUN',
  'score': 0.9966047,
  'index': 8,
  'word': 'bin',
  'start': None,
  'end': None},
 {'entity': 'CCONJ',
  'score': 0.9996582,
  'index': 9,
  'word': 'and',
  'start': None,
  'end': None},
 {'entity': 'VERB',
  'score': 0.9997621,
  'index': 10,
  'word': 'replace',
  'start': None,
  'end': None},
 {'entity': 'PRON',
  'score': 0.9989335,
  'index': 11,
  'word': 'it',
  'start': None,
  'end': None},
 {'entity': 'PUNCT',
  'score': 0.99996936,
  'index': 12,
  'word': '.',
  'start': None,
  'end': None}]
© www.soinside.com 2019 - 2024. All rights reserved.