Back-propagation
Non-differentiable activation functions
Neural networks
Deep learning
Machine learning algorithms

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.

Practice ML system design

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:

  1. Forward Pass: The input is propagated through the network layers to compute the output.
  2. Loss Computation: The difference between the actual and predicted output, measured by a defined loss function, is calculated.
  3. 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.
  4. 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:

ReLU(x)={xif x>00if x0ReLU(x) = \begin{cases} x & \text{if } x > 0 \\ 0 & \text{if } x \leq 0 \end{cases}

At x=0x = 0, 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 x=0x = 0, any value between 0 and 1 can be used as the subgradient. Common practice is to set the derivative at x=0x = 0 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:

Softplus(x)=log(1+ex)\text{Softplus}(x) = \log(1 + e^x)

The softplus function is differentiable everywhere and provides a smooth transition around x=0x = 0.

Example: Handling ReLU in Back-Propagation

When implementing back-propagation with ReLU, the gradient update at a hidden layer can be summarized as:

δ_i={δ_i+1W_iif x_i>00if x_i0\delta\_i = \begin{cases} \delta\_{i+1} \cdot W\_i & \text{if } x\_i > 0 \\ 0 & \text{if } x\_i \leq 0 \end{cases}

Where δi\delta_i represents the error term at layer ii, and WiW_i is the weight matrix connecting layers ii and i+1i+1.

Key Points Summary

AspectDetails
DifferentiabilityEssential for gradient computation in back-propagation.
Non-Differentiable PointsFor functions like ReLU, occur at specific points, typically at x=0x = 0.
SubgradientA generalized idea of a derivative; for ReLU at x=0x = 0, often set to 0 during back-propagation.
Smooth ApproximationsFunctions 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice ML system design

All Rights Reserved.