machine learning
backpropagation
smooth approximation
floor function
neural networks

Smooth approximation to the floor function for use with backpropagation

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

In machine learning, especially in the context of neural networks and backpropagation, differentiability is of utmost importance. Many functions that we would like to employ are not differentiable everywhere, or even anywhere, which complicates their use in training models. One such function is the floor function, which maps a real number to the greatest integer less than or equal to the number. While essential in discrete mathematics and various computer science applications, its discontinuous nature poses challenges for gradient-based optimization methods. In this article, we delve into smooth approximations of the floor function that enable their use within neural networks.

The Floor Function

The floor function, denoted as x\lfloor x \rfloor, is formally defined by:

x=maxnZnx\lfloor x \rfloor = \max{ n \in \mathbb{Z} \mid n \leq x}

While this is precise for direct computational purposes, the derivative of the floor function is zero almost everywhere, making it unusable in gradient descent, which relies on differentiability.

Need for Smooth Approximations

Neural networks are typically optimized using techniques like gradient descent, which require the computation of gradients. As the floor function is not continuous, and hence not differentiable, we seek a smooth approximation that maintains continuity and is differentiable. The goal is to approximate the floor function in a way that is close enough for practical purposes while allowing for the computation of useful gradients.

Possible Approximations

Several techniques can craft smooth approximations of the floor function. Here we explore a few approaches:

Sigmoid-Based Approximation

A common method to smooth step-like functions is to utilize the sigmoid function. Given by:

σ(x)=11+ekx\sigma(x) = \frac{1}{1 + e^{-kx}}

A smooth approximation to x\lfloor x \rfloor can then be constructed as:

S(x)=x_kZσ(xk)S(x) = x - \sum\_{k \in \mathbb{Z}} \sigma(x - k)

Here, the approximation involves subtracting the sigmoid transitions to reduce jump discontinuities to smooth transitions. The parameter kk controls the steepness of the approximation. However, practical implementation would usually truncate kk to finite bounds such as k...2,1,0,1,2...k \in { ... -2, -1, 0, 1, 2 ... }.

Tanh-Based Approximation

Another approach leverages the hyperbolic tangent function:

T(x)=12(x_kZtanh(xk))T(x) = \frac{1}{2} \left( x - \sum\_{k \in \mathbb{Z}} \tanh(x - k) \right)

This can similarly create a smoothened version by using the accumulative property of tanh\tanh, which smoothly transitions between -1 and 1.

Fourier Series Approximation

By leveraging Fourier series, we can express periodic functions in terms of sines and cosines, potentially offering smooth approximations:

F(x)=x12π_n=1sin(2πnx)nF(x) = x - \frac{1}{2\pi} \sum\_{n=1}^\infty \frac{\sin(2 \pi n x)}{n}

Fourier series methods are powerful and often result in elegant analytical expressions, but they can be computationally intensive for large sums.

Comparison and Evaluation

The following table outlines key properties of each approximation method:

Approximation MethodExpressionKey Characteristics
Sigmoid-basedS(x)=xkσ(xk)S(x) = x - \sum_{k} \sigma(x-k)Simple, controlled smoothness with sigmoid function
Tanh-basedT(x)=12(xktanh(xk))T(x) = \frac{1}{2}(x - \sum_{k} \tanh(x-k))Gradual transition, typically wider transitions
Fourier SeriesF(x)=x12πnsin(2πnx)nF(x) = x - \frac{1}{2\pi} \sum_{n} \frac{\sin(2\pi nx)}{n}Periodic, more complex computation

Implementational Considerations

When implementing any of these approximations:

Computational Complexity: It's essential to balance the smoothness of the approximation with the computational resources. Truncating infinite series and choosing practical bounds for kk is crucial. • Gradient Impact: The choice of approximation affects the gradient flow. Always verify that the approximated gradients align with the intended learning dynamics. • Hyperparameters: `Parameters` like kk in the sigmoid and truncation limits must be optimized per application.

Conclusion

Employing smooth approximations to the floor function is a practical necessity when leveraging non-differentiable components within neural networks. By using sigmoid, tanh, or Fourier-based techniques, we can maintain differentiability, enabling the employment of efficient gradient-based optimization methods. Each method comes with trade-offs between computational cost and approximation accuracy, and the choice depends on specific application needs. Exploring these approximations opens new possibilities for integrating discrete operations into the continuous domain of neural network training.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free 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.