Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast sigmoid algorithm

The sigmoid function is defined as

enter image description here

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)?

like image 957
user416983 Avatar asked May 24 '12 06:05

user416983


People also ask

Does sigmoid ever reach 1?

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.

How do you write a sigmoid function in C++?

/* 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.

Why is sigmoid function used in MLP?

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.

What is sigmoid deep learning?

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.


2 Answers

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.

like image 93
Antti Huima Avatar answered Oct 04 '22 15:10

Antti Huima


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.

like image 21
sastanin Avatar answered Oct 04 '22 14:10

sastanin