学习中心

课程:

Surrogate Modeling Theory

深度神经网络激活函数进阶介绍


继续 第一部分 关于深度神经网络(DNN)的讨论,本文将进一步详细探讨激活函数。激活函数为深度神经网络引入非线性特性,使网络能够学习并表征数据中的复杂模式。若没有激活函数,无论层数多少,DNN 都可能会退化为线性模型,限制其解决复杂任务的能力。不同的激活函数通过影响网络优化过程中的梯度流,影响网络的学习动态和性能,进而影响收敛速度,并避免梯度消失或爆炸等问题。激活函数的选择会显著影响网络的泛化及其整体准确性。

深度神经网络激活函数选项

在 DNN 中,激活函数定义了如何将输入的加权和转换为网络某一层中的一个或多个节点的输出。

可用的激活函数包括 线性、ReLU(修正线性单元)、ELU(指数线性单元)、Sigmoid 和 tanh。每一层可以使用不同的激活函数。

The Settings window for the Deep Neural Network function, with the Layers section and Data section expanded.

不同的激活函数选项。

非线性激活函数旨在刻画广义的非线性行为。例如,它们可以表示多项式函数无法表达的饱和效应。下表描述了每个激活函数的特性。

激活函数 表达式 图形 属性
线性 A graph of an upwards diagonal blue line. 线性 激活函数有时也被称为 无激活函数。其输出无界:。
ReLU A graph of a horizontal line that becomes an upwards diagonal line after passing through the origin ReLU 激活函数是分段线性:负值为零,正值为线性。ReLU 激活函数生成的网络有时比其他激活函数更容易训练。其输出无界:。
ELU
A graph of a smooth blue line that curves upward. ELU 激活函数 与 ReLU 激活函数类似,但更平滑且计算量更大。其输出无界:。
Sigmoid A graph of a smooth blue line that exhibits a wide s-like shape. Sigmoid 激活函数是平滑非线性,呈 S 形,主要用于概率估计和分类。其输出有界:。
tanh
A graph of a blue line that exhibits an s-like shape tanh 激活函数具有平滑的非线性特性,呈 S 形,以零为中心且对称。其输出有界:. This is the default option.
softplus A graph of a blue line that curves upward. softplus 激活函数是 ReLU 的平滑近似,处处可微,为正,且无界。当相较于 ReLU 的硬拐角更倾向于平滑行为时,更加有用。
LeakyReLU A graph of a blue line that is sloped upwards slightly that becomes an upwards diagonal line after passing the origin. LeakyReLU 激活函数是 ReLU 的变种,对于负输入引入了极小非零斜率,正输入采用线性响应。在上方无界,且允许负输出,通过对负值保持极小梯度而避免濒死 ReLU 问题。
GELU A graph of a blue line that dips downwards before curving upwards. GELU 激活函数具有平滑非线性,通过输入的大小进行加权,常用于现供深度学习模型,提供 ReLU 的平滑替代方案。它对所有实输入都有定义,输出无界,可正可负。

深度神经网络模型的解析表达式

原则上,可以使用 COMSOL Multiphysics® 中的通用参数估计求解器来优化神经网络的偏置和权重。然而,这种方法效率很低,实际中需要使用专用的 深度神经网络 函数特征和 Adam 优化器。尽管如此,至少对于小型网络,了解这种表达式如何呈现仍具有指导意义。对于采用 tanh 激活函数的 用于热执行器的网络模型 共有 25 个网络参数,对应的 DNN 函数如下:

dnn(x1,x2)=tanh(w3_11*(tanh(w2_11*(tanh(w1_11*(x1)+w1_21*(x2)+b1_1))+w2_21*(tanh(w1_12*(x1)+w1_22*(x2)+b1_2))+w2_31*(tanh(w1_13*(x1)+w1_23*(x2)+b1_3))+w2_41*(tanh(w1_14*(x1)+w1_24*(x2)+b1_4))+b2_1))+w3_21*(tanh(w2_12*(tanh(w1_11*(x1)+w1_21*(x2)+b1_1))+w2_22*(tanh(w1_12*(x1)+w1_22*(x2)+b1_2))+w2_32*(tanh(w1_13*(x1)+w1_23*(x2)+b1_3))+w2_42*(tanh(w1_14*(x1)+w1_24*(x2)+b1_4))+b2_2))+b3_1)

在此表达式中,变量 w1_11、w1_12 等为网络权重,变量 b1_1、b1_2 等为网络偏置。下图展示了以图形方式表示的网络,其中显示了偏置和部分权重:

A column containing two cyan circles, a column containing four dark blue circles, a column of two dark blue circles, and then a column containing one cyan circle, which are connected through a series of lines and show a mesh-like structure as well as many text labels annotated next to certain lines and in every circle.

热执行器模型的神经网络架构。

上述权重与偏置的最优值列于下表,且均以缩放后的输入数据为基准。由于网络采用 tanh 激活函数,其输出被限制在 区间内,因此必须先将数据归一化,方可进行优化。本例网络极小,尚可直观呈现;若换作稍大但仍属小规模的 网络,其参数已达 4321 个,管理此类表达式显然不切实际。在 COMSOL 中进行优化时,不会显式生成此类表达式,而是在底层进行网络计算。此外,计算前和计算后的缩放操作会自动完成。

w1_11 w1_12 w1_13 w1_14
6.479052 -83.5153 -740.349 1028.273
w1_21 w1_22 w1_23 w1_24
-0.01446 -2.50149 0.217145 -0.23627
b1_1 b1_2 b1_3 b1_4
-0.03275 9.940952 -2.92705 2.314871
w2_11 w2_12 w2_21 w2_22
1.882123 131.0688 0.004636 -0.17448
w2_31 w2_32 w2_41 w2_42
-235.551 -211.39 -61.658 -81.0836
b2_1 b2_2
-175.955 -115.824
w3_11 w3_21
-141.037 -148.391
b3_1
8.237812

ReLU 和线性激活函数在回归中的应用

对于回归任务,可以使用 ReLU 和 线性 激活函数的组合作为 tanh 激活函数的替代方案,这样会产生一个分段线性的近似函数。例如,对于学习中心课程中关于代理模型的文章 第 2 部分 中构建的函数曲面回归模型所使用的网络 ,激活函数可以采用以下配置:除最后一层使用 线性 激活函数外,所有层均使用 ReLU 激活函数,如下图所示:

Part of the Settings window for the Deep Neural Network function, with the Layers section expanded.

使用 ReLU 和 线性 激活函数的网络层配置。

在此示例中,学习率 降低至 1e-4,训练周期数 设置为 10,000。

The Settings window for the Deep Neural Network function, with the Training and Validation sections expanded.

使用 ReLU 和 线性 激活函数的网络的 训练和验证 栏中的设置。

A line graph containing one blue line and one green line with the epoch number on the x-axis and loss value on the y-axis. A line graph containing one blue line and one green line with the epoch number on the x-axis and loss value on the y-axis.

使用 ReLU 和 线性 激活函数的网络收敛图。

该网络训练更为容易,但与使用 tanh 激活函数相比,损失值更大,且会出现一定程度的过拟合。当使用 ReLU 和 线性 激活函数时,所得激活函数的曲面为分段线性,其分辨率由网络规模决定,如下图所示的几种不同的网络配置。图中使用的网络配置为: 、 和 。进一步降低模型复杂度会导致欠拟合。

A rainbow-colored, smooth surface plot with a few hills and valleys. A rainbow-colored, smooth surface plot with a few hills and valleys.

当使用 ReLU 和 线性 激活函数时,_网络的函数曲面。_

A rainbow-colored, smooth surface plot with a few hills and valleys. A rainbow-colored, smooth surface plot with a few hills and valleys.

当使用 ReLU 和 线性 激活函数时,_网络的函数曲面。_

A rainbow-colored, smooth surface plot with a few hills and valleys. A rainbow-colored, smooth surface plot with a few hills and valleys.

使用 ReLU 和 线性 激活函数的网络_的函数曲面。_

如果使用一个非常简单的网络 ,我们会得到一个欠拟合的模型。如下收敛图所示,损失停滞在约 0.2 的相对较高水平。

This is not the greatest caption in the world. A line graph containing a blue line and green line with the epoch number on the x-axis and loss value on the y-axis. This is not the greatest caption in the world. A line graph containing a blue line and green line with the epoch number on the x-axis and loss value on the y-axis.

网络 的收敛在损失值为 0.2 处停滞。

网络复杂度过低,无法准确表示数据。使用相同的求解器参数时,生成的神经网络函数几乎退化为常数函数。如下图所示。

A rectangular surface plot in 3D space with a rainbow color distribution, of which the majority is a dark red color. A rectangular surface plot in 3D space with a rainbow color distribution, of which the majority is a dark red color.

欠拟合的网络模型导致函数几乎为常数。

您可以在 ReLU 和 线性 激活函数的模型文件中直接观察到这一现象,点击 此处 下载。


请提交与此页面相关的反馈,或点击此处联系技术支持。