本文通过简单的全连接网络,测试了一下TensorFlow中激活函数的训练时间。使用的数据是mnist,网络结构如下:
Model: “sequential”
Layer (type) Output Shape Param #
flatten (Flatten) (None, 784) 0
dense (Dense) (None, 128) 100480
dense_1 (Dense) (None, 64) 8256
dense_2 (Dense) (None, 32) 2080
dense_3 (Dense) (None, 16) 528
dense_4 (Dense) (None, 10) 170
Total params: 111,514
Trainable params: 111,514
Non-trainable params: 0
结果如下:
序号 | 激活函数 | 训练时间 |
1 | tanh | 6.410 |
2 | sofsign | 6.421 |
3 | sigmoid | 6.469 |
4 | relu | 6.481 |
5 | swish | 6.768 |
6 | nn.leaky_relu | 6.848 |
7 | nn.relu6 | 6.900 |
8 | elu | 6.915 |
9 | gelu | 6.969 |
10 | linear | 6.988 |
11 | LeakyReLU | 7.089 |
12 | hard_sigmoid | 7.363 |
13 | softplus | 7.467 |
14 | selu | 7.563 |
15 | nn.crelu | 7.843 |
总结:
- 对于简单的识别任务和简单的网络,复杂的激活函数并不能提高收敛速度。反而是传统的饱和形激活函数速度更快。这也解释了为什么在早期的阶段,sigmoid激活函数会应用那么广泛
- 针对的不同的任务,需要选择不同的激活函数。
- relu函数适合很多情况,而且效果不错,建议作为首选
程序:
import tensorflow as tf
import time
mnist = tf.keras.datasets.mnist
(training_images,training_labels