神經網絡常用激活函數對比:sigmoid VS sofmax(附python源碼)
首發地址:https://yq.aliyun.com/articles/73661
Softmax
作者介紹:
Saimadhu Polamuripython
領英:https://www.linkedin.com/in/saimadhu/
https://dataaspirant.com/author/saimadhu/
Softmax
在學習邏輯回歸概念時,主要的困惑在於計算概率的函數,由於在邏輯回歸模型中會使用計算出的概率來預測目標類別,經常用到的兩個函數Softmaxd。
從函數水平(幫助預測目標類別)上來看,這兩個函數是相同的,但存在許多明顯的數學差異,應用在深度學習和其他領域中,發揮了至關重要的作用。
所以在這篇文章中將進一步了解這兩個函數及其應用之間的根本區別。
在開始之前,介紹本文的目錄:
- 什麼是S型函數?
- S形函數的性質
- Sigmoid函數的使用
- Python
- 創建Sigmoid函數圖像形
- 什麼是Softmax函數?
- Softmax函數的性質
- Softmax函數的使用
- Python
- 創建Softmax函數圖像形
- Sigmoid
-
結論
S?
從數學定義上來看,Sigmoid的實數,返回的0型曲線,這些曲線也用於統計,0。
S
- sigmoid
- S
- 非負數:如果數字大於或等於零。
- 非正數:如果數字小於或等於零。
Sigmoid
- Sigmoid
-
創建人造神經元時,Sigmoid用作激活函數。
-
S函數圖像是常見的累積分布函數。
Python
# Required Python Package
import numpy as np
def sigmoid(inputs):
"""
Calculate the sigmoid for the give inputs (array)
:param inputs:
:return:
"""
sigmoid_scores = [1 / float(1 + np.exp(- x)) for x in inputs]
return sigmoid_scores
sigmoid_inputs = [2, 3, 5, 6]
print "Sigmoid Function Output :: {}".format(sigmoid(sigmoid_inputs))
Sigmoid代碼。
- 該函數將以列表形式的值作為輸入參數。
- /
- 代碼 1 / float(1 + np.exp(-x))是用於計算sigmoid分數的函數。
-
接下來,我們將一個列表sigmiod_inputs作為函數的輸入,列表值為2,3,5,6,經過sigmoid函數計算後獲得Sigmoid分數。
腳本輸出
Sigmoid Function Output :: [0.8807970779778823, 0.9525741268224334, 0.9933071490757153, 0.9975273768433653]
Sigmoid
現在使用上麵的函數來創建圖像,以方便Sigmoid
-
0
-
輸入sigmoid
- 圖像。
# Required Python Packages
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(inputs):
"""
Calculate the sigmoid for the give inputs (array)
:param inputs:
:return:
"""
sigmoid_scores = [1 / float(1 + np.exp(- x)) for x in inputs]
return sigmoid_scores
def line_graph(x, y, x_title, y_title):
"""
Draw line graph with x and y values
:param x:
:param y:
:param x_title:
:param y_title:
:return:
"""
plt.plot(x, y)
plt.xlabel(x_title)
plt.ylabel(y_title)
plt.show()
graph_x = range(0, 21)
graph_y = sigmoid(graph_x)
print "Graph X readings: {}".format(graph_x)
print "Graph Y readings: {}".format(graph_y)
line_graph(graph_x, graph_y, "Inputs", "Sigmoid Scores")
-
0
-
graph_ysigmoid
-
line_graph圖像x
腳本輸出
Graph X readings: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Graph Y readings: [0.5, 0.7310585786300049, 0.8807970779778823, 0.9525741268224334, 0.9820137900379085, 0.9933071490757153, 0.9975273768433653, 0.9990889488055994, 0.9996646498695336, 0.9998766054240137, 0.9999546021312976, 0.999983298578152, 0.9999938558253978, 0.999997739675702, 0.9999991684719722, 0.999999694097773, 0.9999998874648379, 0.9999999586006244, 0.9999999847700205, 0.9999999943972036, 0.9999999979388463]
圖像
成功運行上述代碼後,屏幕上將顯示以下圖像。如果上述代碼在你的係統中運行失敗。檢查機器學習包的設置。
從上圖可以看出,隨著輸入值的增加,sigmoid1表示在0.9的值。
Softmax?
Softmaxn'別在所有可能的目標類中的概率。計算出的概率將有助於確定給定輸入的目標類別。
Softmax的範圍,0將softmax,它會返回每個類別的概率,並且目標類別的概率值會很大。指數公式softmax
Softmax
softmax函數的幾個性質。
-
0
-
1
- 用於多重分類邏輯回歸模型。
- 中,在softmax
Python
Python
# Required Python Package
import numpy as np
def softmax(inputs):
"""
Calculate the softmax for the give inputs (array)
:param inputs:
:return:
"""
return np.exp(inputs) / float(sum(np.exp(inputs)))
softmax_inputs = [2, 3, 5, 6]
print "Softmax Function Output :: {}".format(softmax(softmax_inputs))
腳本輸出
Softmax Function Output :: [ 0.01275478 0.03467109 0.25618664 0.69638749]
Softmax
Softmax圖像來了解這個函數的表現。
-
0
-
將通過此列表來計算已實現函數的分數。
- 創建圖像。
# Required Python Packages
import numpy as np
import matplotlib.pyplot as plt
def softmax(inputs):
"""
Calculate the softmax for the give inputs (array)
:param inputs:
:return:
"""
return np.exp(inputs) / float(sum(np.exp(inputs)))
def line_graph(x, y, x_title, y_title):
"""
Draw line graph with x and y values
:param x:
:param y:
:param x_title:
:param y_title:
:return:
"""
plt.plot(x, y)
plt.xlabel(x_title)
plt.ylabel(y_title)
plt.show()
graph_x = range(0, 21)
graph_y = softmax(graph_x)
print "Graph X readings: {}".format(graph_x)
print "Graph Y readings: {}".format(graph_y)
line_graph(graph_x, graph_y, "Inputs", "Softmax Scores")
腳本輸出
Graph X readings: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Graph Y readings: [ 1.30289758e-09 3.54164282e-09 9.62718331e-09 2.61693975e-08 7.11357976e-08 1.93367146e-07 5.25626399e-07 1.42880069e-06 3.88388295e-06 1.05574884e-05 2.86982290e-05 7.80098744e-05 2.12052824e-04 5.76419338e-04 1.56687021e-03 4.25919483e-03 1.15776919e-02 3.14714295e-02 8.55482149e-02 2.32544158e-01 6.32120559e-01]
softmax,值越大,其概率越高。
Sigmoid
Sigmoid差異表格:
結論
在本文中,詳細了解確定邏輯回歸模型的兩個函數。
- Softmax
-
Sigmoid
Difference Between Softmax Function and Sigmoid FunctionSaimadhu Polamuri
最後更新:2017-07-12 22:11:35