. Advertisement .
. Advertisement .
This article will provide you topnotch manners to calculate Sigmoid function Python the most effectively. Wait no longer but read on and grasp further!
What Is The Sigmoid Function Python?
So, what is Sigmoid Function in Python anyway?
To put it simply, a sigmoid function, so-called a mathematical feature, once plotted will shape as an S-line curve.
With the help of such a programing trait, it shall aid greatly in reducing the loss as things are on the training track. This is all thanks to its function to eliminate the gradient issue in the program learning sample.
The logistic sigmoid function is the most commonly presented instance of a sigmoid function. Its calculation is shown as:
F(x) = 1 / (1 + e-x)
How To Calculate The Sigmoid Function Python?
Method #1: Utilizing The SciPy Library.
The most approachable technique to implement a sigmoid function is to employ the SciPy library‘s expit() function. Let’s check out the fundamental syntax as follows:
from scipy.special import expit # Calculate value of sigmoid function for x = 5 expit(5)
This method is not very speedy compared to others. However, there is indeed something to say about such a manner.
Using it, you will be able to handle the different kinds of inputs automatically, no matter if it is a list, an array, or so on.
Method #2: Utilizing The Math.exp() Method
Aside from the above technique, we can also calculate our Python sigmoid function with the use of the math module. As such, the math.exp() method of the math module is in demand to execute the sigmoid feature.
Have a look at the following example code to acquire this insight regarding employing the sigmoid function.
import math def sigmoid(x): sig = 1 / (1 + math.exp(-x)) return sig
This implementation has only one issue which is it is unable to be stable numerically, yet leads to many chances for overflow to happen.
The illustrating code formed from the numerically stable implementation is shown as follows.
import math def example_sigmoid(x): if x >= 0: a = math.exp(-x) sig = 1 / (1 + a) return sig else: a = math.exp(x) sig = a / (1 + a) return sig print(example_sigmoid(5))
Method #3: Utilizing the numpy.exp() Feature
There is an exponential term in the sigmoid function. The sigmoid function may be calculated with numpy.exp().
For an interval of x-values between -10 and 10, let’s determine the sigmoid function and its derivative. The condensed derivative phrase from the preceding section can be used.
That way, the sigmoid function and its derivative in the range [-10, 10] will both be plotted using the same plotting function as in the SciPy example.
import numpy as np import matplotlib.pyplot as plt def numpy_sigmoid(x): z = np.exp(-x) sig = 1 / (1 + z) return sig def plot_function(x, y, dy, name): ticks = [2, 4, 6, 8, 10] ax = plt.gca() ax.spines['top'].set_color('blue') ax.spines['left'].set_position('zero') ax.spines['right'].set_color('blue') ax.spines['bottom'].set_position('zero') plt.plot(x, y, color='k', label='$\sigma(x)$') plt.plot(x, dy, color='r', linestyle='dashed', label='Deriviative of $\sigma(x)$') plt.grid(True) plt.legend() plt.xlabel('x') plt.ylabel('F(x)') plt.savefig('Sigmoid Function Python.jpg') plt.close() if __name__ == '__main__': # Define x values x = np.linspace(-10, 10, 100) # Calculate sigmoid function for x values y = numpy_sigmoid(x) # Calculate derivate of sigmoid function dy = y * (1 - y) # Plot function and its derivative plot_function(x, y, dy, 'numpy')
The Bottom Line
The above article regarding sigmoid function Python implementation’s methods hopefully can assist you somehow in conquering this code characteristic.
Now, let’s get on the way and conduct the programming yourself! Good luck to you then.