01
引言
在大型语言模型(LLMs)中,理解模型对其预测结果的置信度对于提升性能和可靠性至关重要。衡量置信度的最有效方法之一是通过 logits —— 即模型为每个潜在输出生成的原始得分。虽然这些 logits 并不是概率,但它们提供了关于模型在将预测转化为最终概率之前,对其预测的信心程度的宝贵洞察。较高的 logits 值表示模型对某个特定结果更有信心,而较低的值则表明不确定性较高。
对于 AI 工程师而言,利用 logits 作为置信度评分对于微调模型行为、检测不确定区域以及确保模型预测符合预期至关重要。通过分析 logits,工程师可以深入理解模型做出决策的机制,从而优化性能、提升可信度,并构建更加可靠的 AI 系统。本文将探讨为何 logits 是衡量置信度的强大工具,以及它们如何帮助你在处理 LLM 时取得更优的效果。
02
在LLM推理过程中,Logits之所以重要,是因为它们代表了模型在将每个潜在Token转换为概率之前,为词汇表中的每个候选词分配的原始未归一化得分。通过分析Logits,AI工程师可以评估模型对其预测的置信度——较高的Logits表明模型对某些Token具有更强的偏好,而较低的Logits则暗示不确定性。这种直接分析Logits的能力,使工程师能够更深入地了解模型的决策过程,从而优化输出、识别潜在的模型弱点,并调优行为,以实现更可靠、更准确的预测。

在上述示例中,output.scores提供了每个生成Token的Logits,可以用来评估模型在生成每一步的置信度。
03
import torchfrom torch.nn.functional import softmaxfrom transformers import MllamaForConditionalGeneration, AutoProcessordef load_llm_model(model_id):model = MllamaForConditionalGeneration.from_pretrained(model_id,torch_dtype=torch.bfloat16,device_map="auto",)processor = AutoProcessor.from_pretrained(model_id)return model, processor# Generate text using the modeldef generate_with_confidence(model, processor, inputs, max_new_tokens=2048):generated_ids = model.generate(**inputs,max_new_tokens=max_new_tokens,do_sample=False,output_scores=True,return_dict_in_generate=True)# Extract scores and generated token IDsscores = generated_ids.scoresgenerated_token_ids = generated_ids.sequences[:, inputs.input_ids.shape[-1]:]# Decode tokens into human-readable textgenerated_text = processor.batch_decode(generated_token_ids,skip_special_tokens=True,clean_up_tokenization_spaces=False)[0]# Compute confidence scoresconfidence_scores = compute_confidence_scores(scores, generated_token_ids, processor)return generated_text, confidence_scores# Compute confidence scores for each tokendef compute_confidence_scores(scores, generated_token_ids, processor):confidence_scores = []for i, logits inenumerate(scores):probabilities = softmax(logits, dim=-1)token_id = generated_token_ids[0, i].item()token = processor.tokenizer.decode([token_id])confidence = probabilities[0, token_id].item()confidence_scores.append((token, confidence))return confidence_scores
Example usageAssuming `model` is your LLM or VLM and `processor` is the corresponding processor`inputs` is a dictionary containing tokenized inputs# generated_text, confidence_scores = generate_with_confidence(model, processor, inputs)print("Generated Text:", generated_text)print("Confidence Scores:", confidence_scores
04
from vllm import SamplingParamssampling_params = SamplingParams(temperature=0.1,top_p=0.001,repetition_penalty=1.05,max_tokens=8192,stop_token_ids=[],logprobs=1)
05
from vllm import LLMtext_1 = "What is the capital of France?texts_2 = ["The capital of Brazil is Brasilia.","The capital of France is Paris."]model = LLM(model = "BAAI/bge-reranker-v2-m3",task = "score",enforce_eager = True)outputs = model.score(text_1, texts_2)for text_2,output in zip(texts_2, outputs):score = output.outputs.scoreprint(f"Pair: {[text_1, text_2]} |score: {score}")
06
-
提升模型可信度: 通过识别模型的高置信度预测结果,工程师能更精准地判断哪些输出值得信赖并投入使用,从而降低决策风险。
-
优化模型性能: 置信度评分能清晰暴露模型的预测盲区,为工程师指明需要微调参数、补充训练数据或调整架构的关键环节,系统性提升模型表现。 -
改善用户体验: 在对话机器人、文档自动化处理等用户场景中,置信度机制可实现:1)自动过滤低置信结果保证输出质量;2)当置信不足时主动要求用户补充输入,构建透明可信的人机交互闭环。
-
输出质量控制 : 基于置信度阈值(如设定0.7为临界值)的智能过滤机制,能自动筛除低质量预测,确保最终输出始终维持工程级可靠性标准。这种动态质量控制体系,已成为工业级AI应用的标配方案。
07
随着AI模型的不断发展,理解和运用置信度评分将变得尤为关键,它们有助于构建更智能、更可靠的系统。置信度评分赋予工程师做出明智决策的能力,确保模型在实际应用中发挥最佳表现。它们是打造更值得信赖、更高效AI解决方案的核心要素。
点击上方小卡片关注我
添加个人微信,进专属粉丝群!

