IT评测·应用市场-qidao123.com

标题: 从Swish到SwiGLU:激活函数的进化与革命,qwen2.5应用的激活函数 [打印本页]

作者: 熊熊出没    时间: 2025-3-13 20:35
标题: 从Swish到SwiGLU:激活函数的进化与革命,qwen2.5应用的激活函数
swiGLU和RMSNorm

1.什么是swiGLU

SwiGLU(Swish-Gated Linear Unit)是一种结合了Swish激活函数GLU(Gated Linear Unit)门控机制的激活函数,广泛应用于当代大型语言模型中
1.什么是Swish激活函数

1.1 Swish激活函数

Swish 激活函数是一种平滑的、非单调的激活函数,由 Google Brain 团队在 2017 年提出。它结合了 ReLU 的非线性特性与 Sigmoid 函数的平滑特性,旨在办理 ReLU 在某些环境下的局限性,比方梯度消失和“死亡神经元”题目。
1.2 Swish 激活函数的定义

Swish 的数学表达式为:
                                         Swish                            (                            x                            )                            =                            x                            ⋅                            σ                            (                            β                            x                            )                                  \text{Swish}(x) = x \cdot \sigma(\beta x)                     Swish(x)=x⋅σ(βx)
此中:

当                                    β                         =                         1                              \beta = 1                  β=1 时,Swish 函数可以简化为:
$$\text{Swish}(x) = x \cdot \sigma(x) $$
1.3Swish 的特性

1.4Swish 的优点

1.5Swish 的缺点

1.6Swish 的变体

1.7Swish 的代码实现

以下是一个基于 PyTorch 的 Swish 激活函数的实现:
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. class Swish(nn.Module):
  5.     def __init__(self, beta=1.0):
  6.         super(Swish, self).__init__()
  7.         self.beta = beta
  8.     def forward(self, x):
  9.         return x * torch.sigmoid(self.beta * x)
  10. # 使用 SiLU(Swish-1)作为激活函数
  11. class SiLU(nn.Module):
  12.     def forward(self, x):
  13.         return x * torch.sigmoid(x)
复制代码
1.18为什么 Swish 被广泛使用?

Swish 的平滑性和非单调性使其在练习深度神经网络时表现精彩,尤其是在处理复杂数据集和长间隔依赖关系时。它在许多当代深度学习模型中被广泛使用,比方 Transformer、BERT 和一些大型语言模型。
通过上述介绍,可以更好地理解 Swish 激活函数的特性及其在深度学习中的告急性。
2.Gated Linear Unit (GLU)

即门控线性单元,是一种引入了门控机制的激活函数,广泛应用于深度学习模型中,尤其是在处理序列数据和自然语言处理任务时表现精彩。
2.1GLU 的定义

GLU 的核心思想是将输入数据通过两个线性变换,此中一个变换的效果通过 Sigmoid 函数举行非线性处理,另一个保持线性,然后两者逐元素相乘。其数学表达式为:
                                         GLU                            (                            x                            )                            =                            σ                            (                                       W                               1                                      x                            +                                       b                               1                                      )                            ⊙                            (                                       W                               2                                      x                            +                                       b                               2                                      )                                  \text{GLU}(x) = \sigma(W_1 x + b_1) \odot (W_2 x + b_2)                     GLU(x)=σ(W1​x+b1​)⊙(W2​x+b2​)
此中:

2.2GLU 的工作原理

2.3GLU 的上风

2.4GLU 的变体

GLU 的变体通过替换 Sigmoid 激活函数来进一步优化性能,常见的变体包括:
2.5GLU 的代码实现

以下是一个基于 PyTorch 的 GLU 实现示例:
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. class GLU(nn.Module):
  5.     def __init__(self, input_dim, output_dim):
  6.         super(GLU, self).__init__()
  7.         self.linear1 = nn.Linear(input_dim, output_dim)
  8.         self.linear2 = nn.Linear(input_dim, output_dim)
  9.     def forward(self, x):
  10.         gate = torch.sigmoid(self.linear1(x))  # 门控信号
  11.         linear = self.linear2(x)               # 线性信号
  12.         return gate * linear                   # 逐元素相乘
复制代码
3.SwiGLU(Swish-Gated Linear Unit)

一种结合了 Swish 激活函数GLU(Gated Linear Unit) 特点的激活函数,广泛应用于当代深度学习模型中,尤其是在大型语言模型(如 LLaMA 和 PaLM)中表现精彩。
3.1SwiGLU 的定义

SwiGLU 的数学表达式为:
                                         SwiGLU                            (                            x                            )                            =                            Swish                            (                                       Linear                               1                                      (                            x                            )                            )                            ⊗                                       Linear                               2                                      (                            x                            )                                  \text{SwiGLU}(x) = \text{Swish}(\text{Linear}_1(x)) \otimes \text{Linear}_2(x)                     SwiGLU(x)=Swish(Linear1​(x))⊗Linear2​(x)
此中:

3.2SwiGLU 的工作原理

3.3SwiGLU 的上风

3.4SwiGLU 的应用场景

SwiGLU 广泛应用于以下范畴:

3.5SwiGLU 的代码实现

以下是一个基于 PyTorch 的 SwiGLU 实现示例:
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. class SwiGLU(nn.Module):
  5.     def __init__(self, input_dim, hidden_dim):
  6.         super(SwiGLU, self).__init__()
  7.         self.linear1 = nn.Linear(input_dim, hidden_dim)
  8.         self.linear2 = nn.Linear(input_dim, hidden_dim)
  9.         self.swish = nn.SiLU()  # 使用内置的 Swish 激活函数
  10.     def forward(self, x):
  11.         return self.linear1(x) * self.swish(self.linear2(x))
  12. # 示例输入
  13. x = torch.randn(4, 128)  # Batch size 4, input dimension 128
  14. model = SwiGLU(input_dim=128, hidden_dim=256)
  15. output = model(x)
  16. print(output.shape)  # 输出维度为 (4, 256)
复制代码
3.6总结

SwiGLU 通过结合 Swish 的平滑非线性特性和 GLU 的门控机制,提供了一种高效的激活函数,实用于必要复杂非线性转换的任务,如自然语言处理和盘算机视觉。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4