Mobile Artificial Intelligence Projects
上QQ阅读APP看书,第一时间看更新

Sigmoid function

The sigmoid function is a function in mathematics that outputs a value between 0 and 1 for any input:

Here,  and .

Let's understand sigmoid functions better with the help of some simple code. If you do not have Python installed, no problem: we will use an online alternative for now at https://www.jdoodle.com/python-programming-online. We will go through a complete setup from scratch in Chapter 2, Creating a Real-Estate Price Prediction Mobile App. Right now, let's quickly continue with the online alternative.

Once we have the page at https://www.jdoodle.com/python-programming-online loaded, we can go through the code step by step and understand sigmoid functions:

  1. First, let's import the math library so that we can use the exponential function:
from   math   import  e
  1. Next, let's define a function called sigmoid, based on the earlier formula:
def sigmoid ( x ):    
return
1 / ( 1 + e **- x )

  1. Let's take a scenario where our z is very small, -10. Therefore, the function outputs a number that is very small and close to 0:
sigmoid(-10) 
4.539786870243442e-05
  1. If z is very large, such as 10000, then the function will output the maximum possible value, 1:
sigmoid(10000)  
1.0

Therefore, the sigmoid function transforms any value, z, to a value between 0 and 1. When the sigmoid activation function is used on a neuron instead of the traditional perceptron algorithm, we get what is called a sigmoid neuron: