1.階躍函數的實現
def step_function(x):
if x>0:
return 1
else:
return 0
#支持numpy數組的實現:
def step_function(x):
y = x>0 #數組X中大於0的元素被轉換爲True,小於等於0的元素被轉換爲False,從而生成一個新的數組y
return y.astype(np.int) #將數組y的元素類型從布爾型轉換爲int型
#繪製階躍函數的圖形
import numpy as np
import matplotlib.pyplot as plt
def step_function():
return np.array(x>0, dtype=np.int)
x = np.arange(-5, 5 ,0.1)
y = step_function(x)
plt.plot(x,y)
plt.ylim(-0.1, 1.1)
plt.show()
2.sigmoid函數
h(x)=1/(1+exp(-x))
實現:
def sigmoid(x):
return 1/(1+np.exp(-x))
#numpy的廣播功能,如果在標量和numpy數組之間進行運算,則標量會和numpy數組的各個元素進行運算。
#繪製sigmode函數圖形
x = np.arange(-5, 5 ,0.1)
y = sigmode(x)
plt.plot(x,y)
plt.ylim(-0.1, 1.1)
plt.show()
3.Relu函數
:Relu函數在輸入大於0時,直接輸出該值; 在輸出小於等於0時,輸出0。
def relu(x):
return np.maximum(0, x)