企业级安全保障: 符合 ISO 27001 和 SOC 2 Type I 标准。
Ultralytics
返回 Ultralytics 词汇表

SwiGLU

探索 LLMs 和 Ultralytics YOLO26 中使用的先进激活函数 SwiGLU。了解其门控机制如何改善神经网络的训练和效率。

SwiGLU (Swish Gated Linear Unit) 是一种先进的 激活函数神经网络 架构组件,用于增强深度机器学习中传统的全连接网络 (FFN)。SwiGLU 结合了 Swish 激活函数平滑、非单调的特性与门控线性单元 (GLU) 机制,提供了动态、依赖数据的特征路由。通过对输入应用线性投影,使其中一个分支通过 Swish 激活,并与另一个线性分支进行逐元素相乘,网络获得了更出色的表达能力。这使得现代 AI 架构能够比旧版 深度学习 模型中使用的标准静态层更有效地捕捉复杂的非线性依赖关系。

Link to this sectionSwiGLU 的工作原理#

与传统全连接网络简单地将输入映射到更高维度、应用基础非线性变换再映射回来不同,SwiGLU 引入了一种乘法门控机制。输入被拆分为两个参数化投影:“门 (gate)”和“值 (value)”。门分支使用 SiLU / Swish 函数激活,该函数保留了较小的负值并确保在几乎所有地方都能获得平滑的非零导数。随后,这个被激活的“门”与“值”分支进行逐元素相乘。这种动态过滤允许神经网络智能地控制信息流,避免了旧架构中常见的“死神经元”问题,并在模型训练过程中稳定了梯度信号,这是一个在 注意力机制 中被广泛研究的概念。

Link to this section区分 SwiGLU 与其他激活函数#

While standard Activation Functions like ReLU use a fixed threshold to clip negative values to zero, SwiGLU dynamically adjusts activations based on the input data itself. Compared to GELU, which weights inputs by their probability under a Gaussian distribution, SwiGLU specifically leverages parameterized linear layers to learn how to gate information. In essence, SwiGLU is not just an element-wise mathematical calculation; it functions as a comprehensive structural component that often replaces the entire hidden layer mechanism inside a Transformer block. For an extensive comparison of mathematical properties, researchers often refer to comprehensive activation function guides.

Link to this section实际应用#

由于其计算效率和显著的性能提升,SwiGLU 已成为现代 AI 系统中的基础组件。

Link to this section在 PyTorch 中实现 SwiGLU#

对于使用 Ultralytics Platform 为边缘设备构建自定义网络或适配视觉模型的开发者而言,通过 PyTorch 文档 实现 SwiGLU 非常简单。(或者,其他生态系统中的开发者可以使用 TensorFlow 实现)。以下简明的 Python 代码片段演示了使用 PyTorch 内置的 F.silu 函数构建的基础 SwiGLU 模块:

import torch
import torch.nn as nn
import torch.nn.functional as F


class SwiGLU(nn.Module):
    def __init__(self, in_features, hidden_features):
        super().__init__()
        # SwiGLU requires two projections: one for the gate, one for the value
        self.gate_proj = nn.Linear(in_features, hidden_features)
        self.value_proj = nn.Linear(in_features, hidden_features)
        self.out_proj = nn.Linear(hidden_features, in_features)

    def forward(self, x):
        # Element-wise multiplication of the SiLU-activated gate and the linear value
        hidden = F.silu(self.gate_proj(x)) * self.value_proj(x)
        return self.out_proj(hidden)


# Example usage with a dummy input tensor
module = SwiGLU(in_features=512, hidden_features=1365)
output = module(torch.randn(1, 512))

这种针对激活块的结构化方法确保了尖端的神经网络架构能够从复杂的 训练数据 中提取更丰富的表征,无论是应用于 自然语言处理 (NLP) 还是实时空间分析。为了更深入地了解如何构建和加速高效模型,开发者通常会参考 arXiv 上关于原始 GLU 变体 的基础研究、Meta 的开源仓库 以及 PyTorch 的优化文档,以最大化硬件吞吐量。

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 的未来!

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