閱讀79 返回首頁    go 阿裏雲 go 技術社區[雲棲]


神經網絡常用激活函數對比:sigmoid VS sofmax(附python源碼)

首發地址:https://yq.aliyun.com/articles/73661

https://yq.aliyun.com/cloud

Softmax

作者介紹:

f4fc34dbd7db4870aaad8adbe6801f02712e0bff

Saimadhu Polamuripython

領英:https://www.linkedin.com/in/saimadhu/

https://dataaspirant.com/author/saimadhu/

0a68f8e4007c67a30d310d506bd56c8951d7539b

Softmax
在學習邏輯回歸概念時,主要的困惑於計算概率的函數由於在邏輯回歸模型會使用計算的概率預測目標類經常用到的兩個函數Softmaxd
函數水平(幫助預測目標類別)上來看,這兩個函數是相同的,但存在許多明顯的數學差異應用在深度學習和其他領域中,發揮至關重要的作用。
所以在這篇文章中將進一步了解這兩個函數及其應用之間的根本區別。
在開始之前,介紹本文的目錄

  • 什麼是S型函數
  • S形函數性質
  • Sigmoid函數使用
  • Python
  • 創建Sigmoid函數圖像 
  • 什麼是Softmax函數
  • Softmax函數的性質
  • Softmax函數使用
  • Python
  • 創建Softmax函數圖像 
  • Sigmoid
  • 結論 

S

數學定義上來看,Sigmoid實數,返回0曲線這些曲線也用於統計0
S

  • sigmoid
  • S

  1. 非負數:如果數字大於或等於零。
  2. 非正數:如果數字小於或等於零。

Sigmoid

  • Sigmoid
  • 創建人造神經元時,Sigmoid用作激活函數
  • S函數圖像是常見的累積分布函數。
Python

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]

圖像

成功運行上述代碼後,屏幕上將顯示以下圖像。如果上述代碼在你的係統中運行失敗。檢查機器學習包設置。 

2af24904fb9e1b80b569e854d65561ab3e0f697e

從上圖可以看出,隨著輸入值的增加,sigmoid1表示在0.9
Softmax

ca81779093824ed50a586b53a6d102e83eb97d27

Softmaxn'在所有可能的目標類中的概率。計算的概率將有助於確定給定輸入的目標類
Softmax範圍0softmax,它會返回每個類的概率,並且目標類別的概率值會很大指數公式softmax 

Softmax

softmax函數的幾個性質

  • 0
  • 1
Softmax使用

  • 用於多重分類邏輯回歸模型。
  • 中,在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]

6softmax 

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]

 

c1385c479881cefd367307909716f29cbbae380a

softmax值越大,其概率越高
Sigmoid
Sigmoid差異表格:

56a1f13aeac2e59c5c725f9ec93c4f5bd7ea801d                        

結論
在本文中,詳細了解確定邏輯回歸模型的兩個函數。

  • Softmax
  • Sigmoid

pdf

@

Difference Between Softmax Function and Sigmoid FunctionSaimadhu Polamuri

最後更新:2017-07-12 22:11:35

  上一篇:go  有監督相似性學習:基於相似問題數據的對稱關係學習
  下一篇:go  論文導讀:深度神經網絡中的對抗樣本與學習(附原文)