How does the back-propagation algorithm deal with non-differentiable activation functions?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Back-propagation is a fundamental algorithm used in training neural networks. It iteratively updates weights in a multi-layer network to minimize the loss function. A crucial part of this process is the calculation of the gradient of the loss function with respect to the weights. To compute this gradient, the algorithm relies heavily on activation functions, which need to be differentiable to facilitate gradient calculations. However, what happens when we encounter non-differentiable activation functions? This article delves into how back-propagation handles such scenarios and explores strategies to overcome potential challenges.
Basics of Back-Propagation
Back-propagation operates through the following steps:
- Forward Pass: The input is propagated through the network layers to compute the output.
- Loss Computation: The difference between the actual and predicted output, measured by a defined loss function, is calculated.
- Backward Pass: The gradient of the loss function with respect to each weight is computed using the chain rule. This requires computing the derivative of the activation function.
- Weight Update: Weights are updated in the direction that minimally reduces the loss.
The derivative's computation requires that all functions involved, particularly the activation functions, are differentiable.
Non-Differentiable Activation Functions
Activation functions such as the ReLU (Rectified Linear Unit) function have regions where they are non-differentiable. The ReLU is defined as:
At , the ReLU function is non-differentiable. Here's how back-propagation handles such situations:
Handling Non-Differentiability
Subgradient Method
In cases where the activation function is non-differentiable at a specific point, a common approach is to use a subgradient. A subgradient is a generalization of a derivative for non-differentiable functions. For ReLU, at , any value between 0 and 1 can be used as the subgradient. Common practice is to set the derivative at to 0 during back-propagation.
Alternative: Smoothed Approximations
Another method is to approximate the non-differentiable function using a smooth, differentiable function. For ReLU, one such approximation is the softplus function:
The softplus function is differentiable everywhere and provides a smooth transition around .
Example: Handling ReLU in Back-Propagation
When implementing back-propagation with ReLU, the gradient update at a hidden layer can be summarized as:
Where represents the error term at layer , and is the weight matrix connecting layers and .
Key Points Summary
| Aspect | Details |
| Differentiability | Essential for gradient computation in back-propagation. |
| Non-Differentiable Points | For functions like ReLU, occur at specific points, typically at . |
| Subgradient | A generalized idea of a derivative; for ReLU at , often set to 0 during back-propagation. |
| Smooth Approximations | Functions like softplus provide a differentiable alternative to non-differentiable functions. |
Conclusion
The challenge of non-differentiable activation functions in back-propagation is mitigated through the use of subgradients and smoothed approximations. These strategies enable the continuation of gradient calculations and allow neural networks to be effectively trained even in the presence of non-differentiable functions. Understanding and implementing these methods is crucial for developing robust, efficient neural network models.
By being flexible with the non-differentiability constraints, we ensure that back-propagation remains a versatile tool in machine learning, able to adapt to various function forms and conditions.
Related reading
- How does the epsilon hyperparameter affect tf.train.AdamOptimizer?
- How does the Flatten layer work in Keras?
- How does the unpooling and deconvolution work in DeConvNet
- How exactly does LSTMCell from TensorFlow operates?
- How does the predict_proba function in LightGBM work internally?
- how does theano.scan's updates work?
- How does the Google Did you mean? Algorithm work?
- How does the Google Did you mean? Algorithm work?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.