1. 程式人生 > >使用python計算softmax函數

使用python計算softmax函數

inf n) nbsp 表示 ray png 矩陣 alt 簡單的

softmax計算公式:

Softmax是機器學習中一個非常重要的工具,他可以兼容 logistics 算法、可以獨立作為機器學習的模型進行建模訓練、還可以作為深度 學習的激勵函數。

     softmax的作用簡單的說就計算一組數值中每個值的占比,公式一般性描述為:
設一共有技術分享圖片個用數值表示的分類技術分享圖片,其中技術分享圖片表示分類的個數。那麽softmax

技術分享圖片

公式softmax的代碼實現:

    

import numpy as np
import
math def softmax(inMatrix): """ softmax計算公式函數 :param inMatrix: 矩陣數據 :return: """ m,n = np.shape(inMatrix) #得到m,n(行,列) outMatrix = np.mat(np.zeros((m,n))) #mat生成數組 soft_sum = 0 for idx in range(0,n): outMatrix[0,idx] = math.exp(inMatrix[0,idx]) #
求冪運算,取e為底的指數計算變成非負 soft_sum +=outMatrix[0,idx] #求和運算 for idx in range(0,n): outMatrix[0,idx] = outMatrix[0,idx] /soft_sum #然後除以所有項之後進行歸一化 return outMatrix a = np.array([[1,2,1,2,1,1,3]]) print(softmax(a))

輸出結果如下:

[[0.05943317 0.16155612 0.05943317 0.16155612 0.05943317 0.05943317

0.43915506]]

使用python計算softmax函數