如何使用Hugging Face Transformers Pipeline调用Cross-Encoder模型?
如何用Hugging Face Transformers Pipeline调用Cross-Encoder模型?
问题背景
Hugging Face Hub上有一系列来自sentence_transformers库的Cross-Encoder模型,例如cross-encoder/mmarco-mMiniLMv2-L12-H384-v1。常规调用方式有两种:
- 通过sentence_transformers库的CrossEncoder类调用
# 使用sentence_transformers库 from sentence_transformers import CrossEncoder model_name = 'cross-encoder/mmarco-mMiniLMv2-L12-H384-v1' model = CrossEncoder(model_name) scores = model.predict([ ['How many people live in Berlin?', 'How many people live in Berlin?'], ['Berlin has a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers.', 'New York City is famous for the Metropolitan Museum of Art.'] ]) print(scores)
输出:
array([ 0.36782095, -4.2674575 ], dtype=float32)
- 通过transformers库直接调用模型与分词器
# 使用transformers库直接调用 from transformers import AutoTokenizer, AutoModelForSequenceClassification import torch model = AutoModelForSequenceClassification.from_pretrained('cross-encoder/mmarco-mMiniLMv2-L12-H384-v1') tokenizer = AutoTokenizer.from_pretrained('cross-encoder/mmarco-mMiniLMv2-L12-H384-v1') features = tokenizer( ['How many people live in Berlin?', 'How many people live in Berlin?'], ['Berlin has a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers.', 'New York City is famous for the Metropolitan Museum of Art.'], padding=True, truncation=True, return_tensors="pt" ) model.eval() with torch.no_grad(): scores = model(**features).logits print(scores)
输出:
tensor([[10.7615], [-8.1277]])
但直接传入已加载的模型和分词器到pipeline时会报错:
from transformers import AutoTokenizer, AutoModelForSequenceClassification from transformers import pipeline import torch model = AutoModelForSequenceClassification.from_pretrained('cross-encoder/mmarco-mMiniLMv2-L12-H384-v1') tokenizer = AutoTokenizer.from_pretrained('cross-encoder/mmarco-mMiniLMv2-L12-H384-v1') pipe = pipeline(model=model, tokenizer=tokenizer)
报错信息:
RuntimeError: Inferring the task automatically requires to check the hub with a model_id defined as a `str`.
问题解答
1. 如何使用Hugging Face Transformers Pipeline调用Cross-Encoder模型?
报错核心原因是:传入已加载的模型对象而非字符串形式的model_id时,pipeline无法自动推断任务类型。解决方法是明确指定task为text-classification,同时注意Cross-Encoder的输入是文本对,调用时需用元组或列表形式传入成对文本。
方法一:直接传入model_id字符串(推荐,自动处理任务推断)
from transformers import pipeline # 直接指定model_id,pipeline会自动加载模型、分词器并推断任务 pipe = pipeline("text-classification", model="cross-encoder/mmarco-mMiniLMv2-L12-H384-v1") # 输入文本对(用元组或列表包裹) results = pipe([ ("How many people live in Berlin?", "How many people live in Berlin?"), ("Berlin has a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers.", "New York City is famous for the Metropolitan Museum of Art.") ]) print(results)
方法二:传入已加载的模型和分词器,指定task
from transformers import AutoTokenizer, AutoModelForSequenceClassification from transformers import pipeline import torch model = AutoModelForSequenceClassification.from_pretrained('cross-encoder/mmarco-mMiniLMv2-L12-H384-v1') tokenizer = AutoTokenizer.from_pretrained('cross-encoder/mmarco-mMiniLMv2-L12-H384-v1') # 明确指定task为text-classification pipe = pipeline("text-classification", model=model, tokenizer=tokenizer) # 调用方式同上 results = pipe([ ("How many people live in Berlin?", "How many people live in Berlin?"), ("Berlin has a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers.", "New York City is famous for the Metropolitan Museum of Art.") ]) print(results)
2. 若需要model_id,能否将其作为args或kwargs传入pipeline?
可以,有两种传入方式:
- 作为
model参数的字符串值传入 - 在指定task后,直接将model_id作为位置参数传入
示例1:通过model参数传入
from transformers import pipeline pipe = pipeline(task="text-classification", model="cross-encoder/mmarco-mMiniLMv2-L12-H384-v1")
示例2:作为位置参数传入(task之后)
from transformers import pipeline pipe = pipeline("text-classification", "cross-encoder/mmarco-mMiniLMv2-L12-H384-v1")
如果需要单独指定分词器的model_id,也可以通过tokenizer参数传入:
from transformers import pipeline pipe = pipeline( "text-classification", model="cross-encoder/mmarco-mMiniLMv2-L12-H384-v1", tokenizer="cross-encoder/mmarco-mMiniLMv2-L12-H384-v1" )
内容的提问来源于stack exchange,提问作者alvas
相关产品推荐
相关产品推荐

