经过多次试错和代码挖掘
现在,开始。。。
TL;DR
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import pipeline
import torch
# cross-encoder/ms-marco-MiniLM-L-12-v2
model = AutoModelForSequenceClassification.from_pretrained('cross-encoder/mmarco-mMiniLMv2-L12-H384-v1')
tokenizer = AutoTokenizer.from_pretrained('cross-encoder/mmarco-mMiniLMv2-L12-H384-v1')
pipe = pipeline("text-classification", model=model, tokenizer=tokenizer)
pipe([{"text": 'How many people live in Berlin?', "text_pair": 'Berlin has a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers.'},
{"text": 'How many people live in Berlin?', "text_pair": 'New York City is famous for the Metropolitan Museum of Art.'},
[out]:
[{'label': 'LABEL_0', 'score': 0.99997878074646},
{'label': 'LABEL_0', 'score': 0.0002951461647171527},
{'label': 'LABEL_0', 'score': 0.027012893930077553}]
但是输出与使用
sentence_transformers
!
是的,它不是,因为softmax被应用于输出。
有一个分类函数应用于模型后推理,位于
https://github.com/huggingface/transformers/blob/main/src/transformers/pipelines/text_classification.py#L27
class ClassificationFunction(ExplicitEnum):
SIGMOID = "sigmoid"
SOFTMAX = "softmax"
NONE = "none"
尤其是在
https://github.com/huggingface/transformers/blob/main/src/transformers/pipelines/text_classification.py#L184
def postprocess(self, model_outputs, function_to_apply=None, top_k=1, _legacy=True):
# `_legacy` is used to determine if we're running the naked pipeline and in backward
# compatibility mode, or if running the pipeline with `pipeline(..., top_k=1)` we're running
# the more natural result containing the list.
# Default value before `set_parameters`
if function_to_apply is None:
if self.model.config.problem_type == "multi_label_classification" or self.model.config.num_labels == 1:
function_to_apply = ClassificationFunction.SIGMOID
elif self.model.config.problem_type == "single_label_classification" or self.model.config.num_labels > 1:
function_to_apply = ClassificationFunction.SOFTMAX
elif hasattr(self.model.config, "function_to_apply") and function_to_apply is None:
function_to_apply = self.model.config.function_to_apply
else:
function_to_apply = ClassificationFunction.NONE
TL;DR(这次是真的)
要复制推出自己的标记化+转发函数的结果,您必须显式设置分类函数并覆盖后处理函数,即。
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import pipeline
from transformers.pipelines.text_classification import ClassificationFunction
model = AutoModelForSequenceClassification.from_pretrained('cross-encoder/mmarco-mMiniLMv2-L12-H384-v1')
tokenizer = AutoTokenizer.from_pretrained('cross-encoder/mmarco-mMiniLMv2-L12-H384-v1')
pipe = pipeline("text-classification", model=model, tokenizer=tokenizer, function_to_apply=ClassificationFunction.NONE)
pipe([{"text": 'How many people live in Berlin?', "text_pair": 'Berlin has a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers.'},
{"text": 'How many people live in Berlin?', "text_pair": 'New York City is famous for the Metropolitan Museum of Art.'},
{"text": 'Hello how are you?', "text_pair": "I'm fine, thank you"},
])
[out]:
[{'label': 'LABEL_0', 'score': 10.761542320251465},
{'label': 'LABEL_0', 'score': -8.127744674682617},
{'label': 'LABEL_0', 'score': -3.5840566158294678}]