Pooling and Activation Functions Comparison in Soft LeNet-5 for Image Classification
What is the best combination of pooling and activation functions for image classification of handwritten digits and numbers? We answer this by testing average vs. max pooling with many activation functions and comparing the performance of each combination. We use LeNet-5 as the base convolutional neural network and modify it. We used the Adam optimizer and SoftMax for the last layer, hence the name Soft LeNet-5.
Soft LeNet-5 is what we called the CNN architecture we used in this work, which is based on the original LeNet architecture idea by LeCun et al. (1988) using simplification of Andrew Ng that he presented in his CNN course, adam optimization, and softmax for the last layer, hence the prefix “Soft”.
Here are two main questions that we would like to answer in this work:
- Which yields better performance? average pooling or maximum pooling on Soft LeNet-5?
- Which activation function achieves the best performance on Soft LeNet-5?
Soft LeNet-5 Architecture
Building on top of LeNet-51, we used the same naming for the layers, C for convolution layers, F for fully connected layers, and S for subsampling, but we used the term pooling in the graph as well. We used a similar style when drawing the CNN.
Soft LeNet-5 Architecture
Pooling Methods
The term pooling, which is sometimes also called downsampling or subsampling, is used to extract features and reduce the size of the input matrix. Average pooling and maximum pooling are among the most common pooling techniques used in Convolutional Neural Networks.
Average Pooling
Average pooling was used by the famous LeNet-5 CNN. The filter performs pairwise multiplication (the first element of the filter is multiplied by the first element in the input window), and then the average of the numbers in the resultant matrix is computed as the output. This is repeated for each window, resulting in a smaller matrix.
Max Pooling
It gained more attention later as it seemed to outperform average pooling (see Scherer et al. (2010)). Similar to average pooling, pairwise multiplication is performed between the window of the input matrix and the filter, and the maximum value in the matrix is selected as the output (see Ranzato et al. (2007)). This is repeated for each window.
Activation Functions
Hidden layers use activation functions to learn non-linear complex patterns.
The most famous activation function was sigmoid, then Tanh, and later ReLU, which was widely adopted by the community. Several activation functions were introduced after that.
Here we briefly talk about each of these activation functions:
- Logistic
- Tanh
- ReLU
- Leaky ReLU
- ELU
- GELU
- Swish
Sigmoid
Two of the most common sigmoid functions are the logistic sigmoid and Tanh. The logistic function was historically the go-to activation function2 for networks.
\[f(x) = \frac{1}{1+e^{-x}}\]Later, it was replaced by hyperbolic tangent, Tanh, which achieved faster convergence and superior accuracy 34.
\[f(x) = \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}\]ReLU
When ReLU came53, it was the hot thing after Tanh, for the significant improvement, despite being used way before that6. Today, many consider it the go-to activation function in CNN hidden layers7, despite advances that offer more accurate activation functions (e.g., ELU, GELU, and Leaky ReLU). ReLU, which stands for Rectified Linear Unit, addresses the vanishing gradient problem in Tanh when higher layers are saturated8. This can cause slower convergence and may lead to a local minimum 8.
\[f(x) = \begin{cases} x & \text{if } x > 0 \\ 0 & \text{if } x \le 0 \end{cases}\]Leaky ReLU
It was an update on ReLU published in 2013 by Mass et al. to address the problem of dead neurons in exchange for giving up the hard-zero sparsity in ReLU8, which can help the training if the gradient still propagates along some paths3.
\[f(x) = \begin{cases} x & \text{if } x > 0 \\ \alpha x & \text{if } x \le 0 \end{cases}\]ELU
Exponential Linear Unit (ELU) was introduced by Clevert et al. in 2015 to speed up learning in deep neural networks910 as it pushes the mean of the activations to zero. It was reported to outperform ReLU and Leaky ReLU, and yielded higher accuracy9.
\[f(x) = \begin{cases} x & \text{if } x > 0 \\ \alpha (e^x - 1) & \text{if } x \le 0 \end{cases}\]GELU
It stands for Gaussian Error Linear Unit, which was proposed by Hendrycks & Gimpel (2016). The idea is to use the CDF (Cumulative Distribution Function) of the normal distribution \(\mathcal{N}(0, 1)\), which we denote as \(\Phi(x)\). They also advised using an optimizer with momentum. We used the Adam optimizer, which satisfies this. Hendrycks & Gimpel in 2016 reported that GELU outperformed ReLU and ELU in several AI tasks10
\[f(x) = x \, \Phi(x)\]Swish
Swish is intended to replace ReLU, especially on deep networks7. It is sometimes called Swish-17 or SiLU11 when \(\beta = 1\). Swish achieved performance similar to or better than ReLU (see Ramachandran et al. 2017). Other experiments show that GELU outperformed SiLU, and that SiLU outperformed both ELU and ReLU10.
\[f(x) = x \, \sigma(\beta x) = \frac{x}{1 + e^{-x}}\]Experiment Setup
We compare different pooling and activation functions on Soft LeNet-5. We used the TensorFlow with Adam optimization and Categorical Cross Entropy for the loss. The source code of the Soft LeNet-5 Experiment is publicly available on GitHub: https://github.com/runzbuzz/labs/tree/main/cnn-pooling-vs-activation.
Datasets
We used the following datasets directly from TensorFlow Datasets:
- MNIST: The is the famous dataset about hand written digits, the modified NIST.
- KMNIST: The Kuzushiji-MNIST is a Japanese dataset from Kuzushiji.
- Fashion MNIST: This dataset include images from Zalando’s clothes.
- EMNIST: EMNIST is short for Extended MNIST, which includes hand written letters and digits12.
Results and Discussion
Testing Performance
Pooling
For the logistic sigmoid, max pooling outperformed average pooling across all datasets, and the difference was significant (which also agrees with Hendrycks & Gimpel10).
For the remaining activation functions, max pooling slightly outperformed average pooling across all datasets. The exception is GELU on MNIST, where average pooling outperformed max pooling.
Activation Functions
The logistic sigmoid consistently scored the lowest across all datasets, followed by Tanh. An exception occurs on the fashionMNIST dataset, where Tanh achieved the highest accuracy with max pooling.
ReLU and Leaky ReLU outperformed sigmoid functions (Tanh and Logistic) on most datasets, as also observed by Maas et al. (2013)8.
We report the best-performing activation function on each dataset below:
| Dataset | Average Pooling | Maximum Pooling |
|---|---|---|
| MNIST | GELU | ReLU |
| FashionMNIST | Leaky ReLU | Tanh |
| KMNIST | GELU | Swish |
| EMNIST | Swish | Swish |
Validation Performance and Convergence
We observed no significant differences, except that the logistic sigmoid was by far the slowest to converge and had the lowest performance4.
Limitation
This experiment compares different pooling and activation functions for the hidden layers on Soft LeNet-5. Therefore, these findings may not translate to other CNN architectures or applications, especially since LeNet-5 isn’t very deep and some activation functions shine in deeper models 9. We also used the Adam optimizer, so different results might be obtained with other optimizers. For the experiments, we used three seeds, so we ran 3 runs for each pair of settings (pooling and activation function). Adding more runs would provide more reliable results and would likely increase the range gap of the different test results.
Conclusion
We found that max pooling is superior to average pooling in most of the cases we tested. For the activation function, the logistic sigmoid resulted in the lowest performance and slowest convergence. Besides that, there isn’t a significant difference in performance among the different activation functions. Even though Swish had the most wins (scoring highest in 3 out of the 8 settings), another function was often pretty close. This isn’t surprising, as many activation functions were developed to address issues in deeper convolutional neural networks. It would also be interesting to compare different CNN architectures to see whether a set of activation functions outperforms others, and whether the choice of pooling technique makes a difference in larger CNNs.
References
-
LeCun, Yann, et al. “Gradient-based learning applied to document recognition.” Proceedings of the IEEE 86.11 (1998): 2278-2324. ↩
-
Rumelhart, David E., Geoffrey E. Hinton, and Ronald J. Williams. “Learning representations by back-propagating errors.” nature 323.6088 (1986): 533-536. ↩
-
Glorot, Xavier, Antoine Bordes, and Yoshua Bengio. “Deep sparse rectifier neural networks.” Proceedings of the fourteenth international conference on artificial intelligence and statistics. JMLR Workshop and Conference Proceedings, 2011. ↩ ↩2 ↩3
-
LeCun, Yann, et al. “Efficient backprop.” Neural networks: Tricks of the trade. Berlin, Heidelberg: Springer Berlin Heidelberg, 2002. 9-50. ↩ ↩2
-
Nair, Vinod, and Geoffrey E. Hinton. “Rectified linear units improve restricted boltzmann machines.” Proceedings of the 27th international conference on machine learning (ICML-10). 2010. ↩
-
Fukushima, Kunihiko. “Visual feature extraction by a multilayered network of analog threshold elements.” IEEE Transactions on Systems Science and Cybernetics 5.4 (1969): 322-333. ↩
-
Ramachandran, Prajit, Barret Zoph, and Quoc V. Le. “Searching for activation functions.” arXiv preprint arXiv:1710.05941 (2017). ↩ ↩2 ↩3
-
Maas, Andrew L., Awni Y. Hannun, and Andrew Y. Ng. “Rectifier nonlinearities improve neural network acoustic models.” Proc. icml. Vol. 30. No. 1. 2013. ↩ ↩2 ↩3 ↩4
-
Clevert, Djork-Arné, Thomas Unterthiner, and Sepp Hochreiter. “Fast and accurate deep network learning by exponential linear units (elus).” arXiv preprint arXiv:1511.07289 (2015). ↩ ↩2 ↩3
-
Hendrycks, Dan, and Kevin Gimpel. “Gaussian error linear units (gelus).” arXiv preprint arXiv:1606.08415 (2016). ↩ ↩2 ↩3 ↩4
-
Elfwing, Stefan, Eiji Uchibe, and Kenji Doya. “Sigmoid-weighted linear units for neural network function approximation in reinforcement learning.” Neural networks 107 (2018): 3-11. ↩
-
Cohen, Gregory, et al. “EMNIST: Extending MNIST to handwritten letters.” 2017 international joint conference on neural networks (IJCNN). IEEE, 2017. ↩