The sigmoid function is defined as
I found that using the C built-in function exp()
to calculate the value of f(x)
is slow. Is there any faster algorithm to calculate the value of f(x)
?
At x = 1, we find a slightly larger value: and by x = 5, the value of the sigmoid function becomes very close to 1. In fact, in the limit of x tending towards infinity, the sigmoid function converges to 1, and towards -1 in the case of negative infinity, but the derivative of the function never reaches zero.
/* Returns the value of the sigmoid function f(x) = 1/(1 + e^-x). Input: m1, a vector. Output: 1/(1 + e^-x) for every element of the input matrix m1.
A common choice of activation function in MLP NNs is the Sigmoid function, since it takes a real-valued input and squashes it to range between 0 and 1, i.e., it produces a curve with an “S” shape similar to TLU. Sigmoid function is differentiable often used to introduce nonlinearity in the model.
The sigmoid function is the key to understanding how a neural network learns complex problems. This function also served as a basis for discovering other functions that lead to efficient and good solutions for supervised learning in deep learning architectures.
you don't have to use the actual, exact sigmoid function in a neural network algorithm but can replace it with an approximated version that has similar properties but is faster the compute.
For example, you can use the "fast sigmoid" function
f(x) = x / (1 + abs(x))
Using first terms of the series expansion for exp(x) won't help too much if the arguments to f(x) are not near zero, and you have the same problem with a series expansion of the sigmoid function if the arguments are "large".
An alternative is to use table lookup. That is, you precalculate the values of the sigmoid function for a given number of data points, and then do fast (linear) interpolation between them if you want.
It's best to measure on your hardware first. Just a quick benchmark script shows, that on my machine 1/(1+|x|)
is the fastest, and tanh(x)
is the close second. Error function erf
is pretty fast too.
% gcc -Wall -O2 -lm -o sigmoid-bench{,.c} -std=c99 && ./sigmoid-bench atan(pi*x/2)*2/pi 24.1 ns atan(x) 23.0 ns 1/(1+exp(-x)) 20.4 ns 1/sqrt(1+x^2) 13.4 ns erf(sqrt(pi)*x/2) 6.7 ns tanh(x) 5.5 ns x/(1+|x|) 5.5 ns
I expect that the results may vary depending on architecture and the compiler used, but erf(x)
(since C99), tanh(x)
and x/(1.0+fabs(x))
are likely to be the fast performers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With