Python

NumPy Sigmoid

Today, we learn what the sigmoid function is and how to implement the sigmoid function in NumPy Python. The sigmoid function is used to activate the functions of the neural network in Python using one of the advanced libraries of the Python language which is NumPy.

The sigmoid function is used to forecast the statistical likelihood outputs and may be found in the output layers of deep learning architectures and in machine learning. This function accepts inputs of any range of real numbers and produces results with real values. Let’s look at the formulae of the sigmoid function:

Let’s start implementing the sigmoid function in Python using the NumPy library.

Syntax:

We implement the sigmoid_function in Python. The keyword “def” means that we define the function in Python. Then, we write the function name that we want to implement. In the function brackets, we pass the argument that can be a variable or array in the function. In the body of the function, we write the sigmoid function formula so that we get the output of the sigmoid function.

As you see in the formula, we use the exp() function to calculate the exponential of the inverse of x. The x is the input value or input array of the sigmoid function.

Parameter:

x: The single input value or input array of the sigmoid function.

Return Value:

The return value of the sigmoid function depends on the input value of the sigmoid function. If we pass the real number in the sigmoid function, we get the real number in return. But if we pass the array in the sigmoid function, it returns the NumPy array. The resultant array is element-wise and has the same shape as the shape of the input array.

Example 1:

Let’s start our very first and simple example of the sigmoid function in Python using one of the important libraries of Python which is used to compute the numerical value in the Python programming language. To implement this, we have to install first the NumPy library. After this, we import the library so that we can perform the numerical functions in Python. We first write the keyword “import” so that it tells the compiler that we are going to import the library. Then, we write the library name that we use in the program which is “numpy”. We can also write the alias of NumPy which is “np”. Now, we start writing the actual line of code of the sigmoid function that we want to perform.

After importing the NumPy library, we call a print() method so that we can display the message that we are implementing the sigmoid function. This is optional; we only display this so that the users can understand easily what we are doing in the example. Then, we create the input array using the array() function of NumPy. Then, we display this input array in the shell using the print() method again. The print method is the pre-defined method of Python language which is used to display the data in the output after the compilation process.

import numpy as np

print("Implementation of sigmoid Function in NumPy Python: ")

array = np.array([-0.1, 2.1, 1.1, -3.3, 0.1])
print("\nThe Input Array is: ", array)

def sigmoid(x):
    return 1.0 / (1.0 + np.exp(-x))
print("\nThe sigmoid array is: ", sigmoid(array))

As you see, we define the sigmoid function because NumPy doesn’t provide any function to calculate the value of the sigmoid so we have to make a custom sigmoid function. First, we write the keyword “def” which means that we tell the compiler that we are defining the function. Then, we write the function name that we perform which is “sigmoid”. Then, we pass the parameter in the sigmoid function which is “x”. In the body of the sigmoid function, we first write the “return” keyword. Then, we implement the sigmoid formulae which we use in mathematics to calculate the sigmoid. As you have noticed, we use the exp() function in the formula because we calculate the exponential of the inverse of “x” which is why we use the exp() function and then return the calculated formula to the sigmoid function. Then, we display the sigmoid array using the print() method.

Now, let us look at the output of the sigmoid function that we have to calculate using the NumPy package:

Example 2:

Let’s move on to another example of the sigmoid function. First, we import the library which is NumPy because we are doing the mathematical operations in the Python language. We write “import numpy as np” where import is the keyword, NumPy is the name of the library, and np is the alias of NumPy.

Then, we create an input array using the linspace() function. It is one of the functions of NumPy which is used to evenly space the array that contains 10 elements from -100 to 100. And then, we store the whole function in another array named “array” and display it using the print() method. Then, we define the sigmoid function because it is not a predefined function of NumPy. Then, we return the formula of the sigmoid to the sigmoid function.

import numpy as np

print("Implementation of sigmoid Function in NumPy: \n")

array = np.linspace(start = -100, stop = 100, num = 10)
print("The Input Array is: \n", array)

def sigmoid(x):
    return 1.0 / (1.0 + np.exp(-x))

sigmoid_values = sigmoid(array)
print("\nThe Sigmoid Values are: \n", sigmoid_values)

After defining the sigmoid function, we use the sigmoid function and pass the input array into it. Then, we store the function in another array which is named “sigmoid_values”. Then, we display the new array by calling the print() method and passing the sigmoid_values in it.

Let’s see the output of the sigmoid function’s second example and check whether we get the desired output or not:

As you can see, we get the desired output of the previously-explained example as we gave the input of 10 elements in the array.

Conclusion

In this article, we learned about the sigmoid function and how we define the sigmoid function in NumPy because it is not a predefined function of NumPy Python. Then, we implemented multiple examples of the sigmoid function and explained these examples.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.