遇见 YOLO26: 下一代视觉 AI。
Ultralytics
返回 Ultralytics 词汇表

Softmax

探索 AI 中的 Softmax 函数。了解它如何使用 Ultralytics YOLO26 和神经网络将逻辑值转换为多类分类的概率。

Softmax is a mathematical function pivotal to the field of artificial intelligence, specifically serving as the final step in many classification algorithms. It transforms a vector of raw numbers, often called logits, into a vector of probabilities. This transformation ensures that the output values are all positive and sum up to exactly one, effectively creating a valid probability distribution. Because of this property, Softmax is the standard activation function used in the output layer of neural networks designed for multi-class classification, where the system must choose a single category from more than two mutually exclusive options.

Link to this sectionSoftmax 的工作原理#

在典型的 深度学习 (DL) 工作流程中,网络的各层执行复杂的矩阵乘法和加法。最后一层在激活之前的输出由称为 logits 的原始分数组成。这些值的范围可以从负无穷大到正无穷大,使得它们难以直接解释为置信度。

Softmax 通过执行两个主要操作来解决这个问题:

  1. 指数化: 它计算每个输入数字的指数。这一步确保所有值都是非负的(因为 $e^x$ 总是正的),并对远低于最大值的值进行惩罚,同时突出显示最大的分数。

  2. 归一化: 它将这些指数化后的值求和,并用每个单独的指数除以该总和。这个 归一化过程 对数字进行缩放,使它们代表整体的一部分,让开发者能够将其解释为百分比置信度分数。

Link to this section实际应用#

输出清晰概率的能力使得 Softmax 在各种行业和 机器学习 (ML) 任务中不可或缺。

  • 图像分类: 在计算机视觉中,模型使用 Softmax 对图像进行分类。例如,当 Ultralytics YOLO26 分类模型分析一张照片时,它可能会为“金毛寻回犬”、“德国牧羊犬”和“贵宾犬”等类别生成分数。Softmax 将这些分数转换为概率(例如 0.85、0.10、0.05),表明该图像包含金毛寻回犬的置信度很高。这对于从自动照片整理到 医疗保健 AI 中的医学诊断等各种应用至关重要。
  • 自然语言处理 (NLP): Softmax 是 大语言模型 (LLMs) 中文本生成的引擎。当像 Transformer 这样的模型生成句子时,它会通过计算词汇表中每个单词的分数来预测下一个单词(token)。Softmax 将这些分数转化为概率,使模型能够选择最可能的下一个单词,从而实现流畅的 机器翻译 和对话式 AI。
  • 强化学习: 强化学习 中的智能体经常使用 Softmax 来选择动作。智能体可能不会总是选择价值最高的动作,而是利用概率来探索不同的策略,在机器人控制或游戏等环境中平衡探索与利用。

Link to this sectionPython 代码示例#

以下示例演示了如何加载预训练的 YOLO26 分类模型,并获取通过 Softmax 生成的概率分数。

from ultralytics import YOLO

# Load a pre-trained YOLO26 classification model
model = YOLO("yolo26n-cls.pt")

# Run inference on a sample image
results = model("https://ultralytics.com/images/bus.jpg")

# The model applies Softmax internally. Access the top prediction:
# The 'probs' attribute contains the probability distribution.
top_prob = results[0].probs.top1conf.item()
top_class = results[0].names[results[0].probs.top1]

print(f"Predicted Class: {top_class}")
print(f"Confidence (Softmax Output): {top_prob:.4f}")

Link to this section区分 Softmax 与相关概念#

While Softmax is dominant in multi-class scenarios, it is important to distinguish it from other mathematical functions used in model training and architecture design:

  • Sigmoid Sigmoid 函数也将值缩放在 0 到 1 之间,但它独立处理每个输出。这使得 Sigmoid 非常适合 二元分类(是/否)或类别不互斥的多标签分类(例如,图像可能同时包含“人”和“背包”)。Softmax 强制概率总和为 1,使类别之间形成竞争关系。
  • ReLU (修正线性单元) ReLU 主要用于网络的隐藏层以引入非线性。与 Softmax 不同,ReLU 不会将输出限制在特定范围内(它只是对负输入输出零,对正输入输出输入值本身),并且不会生成概率分布。
  • Argmax: 虽然 Softmax 为所有类别提供 概率,但 Argmax 函数 通常与它结合使用,以选择具有最高概率的单一索引。Softmax 提供“软”置信度,而 Argmax 提供“硬”最终决策。

Link to this section高级集成#

在现代 ML 流水线中,Softmax 通常在损失函数内隐式计算。例如,交叉熵损失 将 Softmax 和负对数似然结合到一个数学步骤中,以提高训练过程中的数值稳定性。像 Ultralytics Platform 这样的平台会自动处理这些复杂性,让用户无需手动实现这些数学运算即可训练出强大的模型。

Explore solutions

Real-time AI that works with your team

机器人技术中的 AI

使用 Ultralytics YOLO 模型为智能机器赋能。机器人技术中的视觉 AI 可推动自主导航、感知、物体跟踪和实时控制。

了解更多
Real-time AI that works with your team

物流中的 AI

使用 Ultralytics YOLO 模型简化物流。视觉 AI 可实现包裹检查、分拣、车辆跟踪和实时仓库安全监控。

了解更多
Real-time AI that works with your team

零售业 AI

使用 Ultralytics YOLO 模型重塑零售业。视觉 AI 推动库存跟踪、货架监控、队列管理和更智能的客户洞察。

了解更多
Real-time AI that works with your team

医疗保健中的 AI

利用 Ultralytics YOLO 模型构建医疗保健解决方案。医疗保健中的视觉 AI 可助力更快速的医学影像分析、更智能的诊断和患者监测。

了解更多
Real-time AI that works with your team

制造业中的 AI

使用 Ultralytics YOLO 模型优化制造业。视觉 AI 推动质量控制、缺陷检测、PPE 合规性和装配线自动化。

了解更多
Real-time AI that works with your operation

汽车中的 AI

将计算机视觉应用于汽车行业,并配合 Ultralytics YOLO 模型。汽车视觉 AI 可提升道路安全、辅助驾驶和车辆自动化,打造更智能的道路。

了解更多
Real-time AI tailored to your operation

农业中的 AI

借助 Ultralytics YOLO 模型,将视觉 AI 引入智慧农业。赋能作物监测、牲畜追踪和精准农业,实现更高、更智能的产量。

了解更多
Real-time AI that works with your team

机器人技术中的 AI

使用 Ultralytics YOLO 模型为智能机器赋能。机器人技术中的视觉 AI 可推动自主导航、感知、物体跟踪和实时控制。

了解更多
Real-time AI that works with your team

物流中的 AI

使用 Ultralytics YOLO 模型简化物流。视觉 AI 可实现包裹检查、分拣、车辆跟踪和实时仓库安全监控。

了解更多
Real-time AI that works with your team

零售业 AI

使用 Ultralytics YOLO 模型重塑零售业。视觉 AI 推动库存跟踪、货架监控、队列管理和更智能的客户洞察。

了解更多
Real-time AI that works with your team

医疗保健中的 AI

利用 Ultralytics YOLO 模型构建医疗保健解决方案。医疗保健中的视觉 AI 可助力更快速的医学影像分析、更智能的诊断和患者监测。

了解更多
Real-time AI that works with your team

制造业中的 AI

使用 Ultralytics YOLO 模型优化制造业。视觉 AI 推动质量控制、缺陷检测、PPE 合规性和装配线自动化。

了解更多
Real-time AI that works with your operation

汽车中的 AI

将计算机视觉应用于汽车行业,并配合 Ultralytics YOLO 模型。汽车视觉 AI 可提升道路安全、辅助驾驶和车辆自动化,打造更智能的道路。

了解更多
Real-time AI tailored to your operation

农业中的 AI

借助 Ultralytics YOLO 模型,将视觉 AI 引入智慧农业。赋能作物监测、牲畜追踪和精准农业,实现更高、更智能的产量。

了解更多
Real-time AI that works with your team

机器人技术中的 AI

使用 Ultralytics YOLO 模型为智能机器赋能。机器人技术中的视觉 AI 可推动自主导航、感知、物体跟踪和实时控制。

了解更多
Real-time AI that works with your team

物流中的 AI

使用 Ultralytics YOLO 模型简化物流。视觉 AI 可实现包裹检查、分拣、车辆跟踪和实时仓库安全监控。

了解更多
Real-time AI that works with your team

零售业 AI

使用 Ultralytics YOLO 模型重塑零售业。视觉 AI 推动库存跟踪、货架监控、队列管理和更智能的客户洞察。

了解更多
Real-time AI that works with your team

医疗保健中的 AI

利用 Ultralytics YOLO 模型构建医疗保健解决方案。医疗保健中的视觉 AI 可助力更快速的医学影像分析、更智能的诊断和患者监测。

了解更多
Real-time AI that works with your team

制造业中的 AI

使用 Ultralytics YOLO 模型优化制造业。视觉 AI 推动质量控制、缺陷检测、PPE 合规性和装配线自动化。

了解更多
Real-time AI that works with your operation

汽车中的 AI

将计算机视觉应用于汽车行业,并配合 Ultralytics YOLO 模型。汽车视觉 AI 可提升道路安全、辅助驾驶和车辆自动化,打造更智能的道路。

了解更多
Real-time AI tailored to your operation

农业中的 AI

借助 Ultralytics YOLO 模型,将视觉 AI 引入智慧农业。赋能作物监测、牲畜追踪和精准农业,实现更高、更智能的产量。

了解更多

让我们一起构建 AI 的未来!

开启你的机器学习未来之旅