2025-03-19 09:29:17 +08:00

205 lines
5.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

### **1. 传统激活函数**
#### **(1) Sigmoid**
**公式**
$$\
\sigma(x) = \frac{1}{1 + e^{-x}}
\ $$
**输出范围**`(0, 1)`
**特点**
• 将输入压缩到概率区间,常用于二分类输出层。
**缺点**
◦ 梯度消失问题输入绝对值较大时导数趋近于0
◦ 计算复杂,不适合深层网络。
**代码示例**
```python
layers.Dense(64, activation='sigmoid')
```
#### **(2) Tanh双曲正切函数**
• **公式**
$$\
\tanh(x) = \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} = \frac{e^{2x}}{e^{2x} + 1} - 1
\ $$
• **输出范围**`(-1, 1)`
• **特点**
• 输出对称分布,中心化数据更易训练。
• 梯度消失问题仍存在,但比 Sigmoid 好。
• **适用场景**
• 循环神经网络RNN、双向 LSTM。
• **代码示例**
```python
layers.LSTM(128, activation='tanh')
```
---
### **2. 改进型 ReLU 系列**
#### **(1) Leaky ReLU**
• **公式**
$$\
f(x) = \begin{cases}
x & \text{if } x > 0 \\
\alpha x & \text{if } x \leq 0
\end{cases}
\ $$
• \( \alpha \):泄漏系数(通常取 `0.1`)。
• **特点**
• 解决 ReLU 的“死亡神经元”问题,允许负值区域有微小梯度。
• **代码示例**
```python
class LeakyReLU(tf.keras.layers.Layer):
def __init__(self, alpha=0.1, **kwargs):
super().__init__(**kwargs)
self.alpha = alpha
def call(self, inputs):
return tf.maximum(inputs, self.alpha * inputs)
model.add(LeakyReLU(alpha=0.2))
```
#### **(2) Parametric ReLUPReLU**
• **公式**
$$\
f(x) = \max(0, x + a \cdot x)
\ $$
• \( a \):可学习的参数(而非固定系数)。
• **特点**
• 自动调整负区间的斜率,增强模型表达能力。
• **代码示例**
```python
from tensorflow.keras.layers import PReLU
model.add(PReLU())
```
#### **(3) ELU指数线性单元**
• **公式**
$$\
f(x) = \begin{cases}
x & \text{if } x > 0 \\
\alpha (e^{x} - 1) & \text{if } x \leq 0
\end{cases}
\ $$
• \( \alpha \):平滑系数(通常取 `1`)。
• **特点**
• 在负区间输出平滑曲线,减少梯度噪声。
• **代码示例**
```python
layers.ELU(alpha=1.0)
```
#### **(4) Swish**
• **公式**
$$\
f(x) = x \cdot \sigma(x) = \frac{x}{1 + e^{-x}}
\ $$
• **特点**
• 自动归一化输出,类似于 Sigmoid但梯度更平滑。
• 在 Google 的多项任务中表现优异。
• **代码示例**
```python
layers.Swish()
```
---
### **3. 平滑型激活函数**
#### **(1) Softmax**
• **公式**
$$\
\text{Softmax}(x_i) = \frac{e^{x_i}}{\sum_{j} e^{x_j}}
\ $$
• **输出范围**`(0, 1)`(概率分布)。
• **特点**
• 常用于多分类输出层,强制输出为概率形式。
• **代码示例**
```python
layers.Dense(10, activation='softmax')
```
#### **(2) Softsign**
• **公式**
$$\
f(x) = \frac{x}{1 + |x|}
\ $$
• **输出范围**`(-1, 1)`。
• **特点**
• 输出对称且平滑,比 Sigmoid 计算更快。
• **代码示例**
```python
layers.Softsign()
```
---
### **4. 循环网络专用激活函数**
#### **(1) Tanh已介绍**
• **适用场景**
• RNN、LSTM、GRU 的隐藏层,输出范围 `(-1, 1)` 有助于稳定训练。
#### **(2) Hard Sigmoid**
• **公式**
$$\
f(x) = \max(0, \min(1, \frac{x + 1}{2}))
\ $$
• **特点**
• 计算高效,近似 Sigmoid常用于嵌入式设备。
---
### **5. 其他特殊激活函数**
#### **(1) Softmax Cross-Entropy with Logits**
• **公式**
$$\
H(y, \hat{y}) = -\sum_{i} y_i \log(\hat{y}_i)
\ $$
• **特点**
• 结合 Softmax 和交叉熵,数值稳定且高效。
• **代码示例**
```python
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')
```
#### **(2) SiLUSigmoid Linear Unit**
• **公式**
$$\
f(x) = x \cdot \sigma(x)
\ $$
• **特点**
• 线性和非线性的平滑结合,类似 Swish。
---
### **6. 激活函数选择指南**
| **任务类型** | **推荐激活函数** | **理由** |
|--------------------|--------------------------------------|----------------------------------------|
| **二分类输出层** | Sigmoid、Softmax | 输出概率形式。 |
| **多分类输出层** | Softmax | 强制归一化为概率分布。 |
| **隐藏层(通用)** | ReLU、Leaky ReLU、Swish | 平衡性能与计算效率。 |
| **RNN/序列模型** | Tanh、ELU | 稳定梯度传播,缓解长程依赖问题。 |
| **需要平滑输出** | Softsign、Softmax | 输出连续且可解释。 |
| **嵌入式设备** | Hard Sigmoid、ReLU6 | 计算快速,硬件友好。 |
---
### **7. 高级技巧**
#### **(1) 自定义激活函数**
```python
class CustomActivation(tf.keras.layers.Layer):
def __init__(self, name='custom_act', **kwargs):
super().__init__(name=name, **kwargs)
def call(self, inputs):
return tf.nn.relu(inputs) * 0.5 + 0.5 # 示例:缩放 ReLU
model.add(CustomActivation())
```
#### **(2) 混合激活函数**
在复杂模型中,不同层使用不同激活函数(如 CNN 使用 ReLURNN 使用 Tanh
---
### **总结**
**ReLU 及其变种**(如 Leaky ReLU、Swish是大多数深层网络的首选。
**Sigmoid/Tanh** 适用于特定场景(如分类输出层、循环网络)。
**平滑函数**(如 Softmax在需要概率输出时必不可少。