Python softmax實(shí)現(xiàn)及數(shù)值穩(wěn)定性詳解
Softmax
softmax函數(shù)將任意n維的實(shí)值向量轉(zhuǎn)換為取值范圍在(0,1)之間的n維實(shí)值向量,并且總和為1。
例如:向量softmax([1.0, 2.0, 3.0]) ------> [0.09003057, 0.24472847, 0.66524096]
性質(zhì):
- 因?yàn)閟oftmax是單調(diào)遞增函數(shù),因此不改變?cè)紨?shù)據(jù)的大小順序。
- 將原始輸入映射到(0,1)區(qū)間,并且總和為1,常用于表征概率。
- softmax(x) = softmax(x+c), 這個(gè)性質(zhì)用于保證數(shù)值的穩(wěn)定性。
softmax的實(shí)現(xiàn)及數(shù)值穩(wěn)定性
一個(gè)最簡單的計(jì)算給定向量的softmax的實(shí)現(xiàn)如下:
import numpy as np
def softmax(x):
"""Compute the softmax of vector x."""
exp_x = np.exp(x)
softmax_x = exp_x / np.sum(exp_x)
return softmax_x讓我們來測試一下上面的代碼:
softmax([1, 2, 3]) array([0.09003057, 0.24472847, 0.66524096])
但是,當(dāng)我們嘗試輸入一個(gè)比較大的數(shù)值向量時(shí),就會(huì)出錯(cuò):
softmax([1000, 2000, 3000]) array([nan, nan, nan])
這是由numpy中的浮點(diǎn)型數(shù)值范圍限制所導(dǎo)致的。當(dāng)輸入一個(gè)較大的數(shù)值時(shí),sofmax函數(shù)將會(huì)超出限制,導(dǎo)致出錯(cuò)。
為了解決這一問題,這時(shí)我們就能用到sofmax的第三個(gè)性質(zhì),即:softmax(x) = softmax(x+c),
一般在實(shí)際運(yùn)用中,通常設(shè)定c = - max(x)。
接下來,我們重新定義softmax函數(shù):
import numpy as np
def softmax(x):
"""Compute the softmax in a numerically stable way."""
x = x - np.max(x)
exp_x = np.exp(x)
softmax_x = exp_x / np.sum(exp_x)
return softmax_x然后再次測試一下:
softmax([1000, 2000, 3000]) array([ 0., 0., 1.])
Done!
以上都是基于向量上的softmax實(shí)現(xiàn),下面提供了基于向量以及矩陣的softmax實(shí)現(xiàn),代碼如下:
import numpy as np
def softmax(x):
"""
Compute the softmax function for each row of the input x.
Arguments:
x -- A N dimensional vector or M x N dimensional numpy matrix.
Return:
x -- You are allowed to modify x in-place
"""
orig_shape = x.shape
if len(x.shape) > 1:
# Matrix
exp_minmax = lambda x: np.exp(x - np.max(x))
denom = lambda x: 1.0 / np.sum(x)
x = np.apply_along_axis(exp_minmax,1,x)
denominator = np.apply_along_axis(denom,1,x)
if len(denominator.shape) == 1:
denominator = denominator.reshape((denominator.shape[0],1))
x = x * denominator
else:
# Vector
x_max = np.max(x)
x = x - x_max
numerator = np.exp(x)
denominator = 1.0 / np.sum(numerator)
x = numerator.dot(denominator)
assert x.shape == orig_shape
return x以上就是Python softmax實(shí)現(xiàn)及數(shù)值穩(wěn)定性詳解的詳細(xì)內(nèi)容,更多關(guān)于Python softmax數(shù)值穩(wěn)定性的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
對(duì)pandas通過索引提取dataframe的行方法詳解
今天小編就為大家分享一篇對(duì)pandas通過索引提取dataframe的行方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-02-02
簡要講解Python編程中線程的創(chuàng)建與鎖的使用
這篇文章主要介紹了簡要講解Python編程中線程的創(chuàng)建與鎖的使用,Python中雖然有GIL的存在,但依然是能夠創(chuàng)建多個(gè)線程來交替使用的,需要的朋友可以參考下2016-02-02
Python批量刪除mysql中千萬級(jí)大量數(shù)據(jù)的腳本分享
這篇文章主要介紹了Python批量刪除mysql中千萬級(jí)大量數(shù)據(jù)的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12
VSCode配置python.analysis.extraPaths作用詳解
本文主要介紹了VSCode配置python.analysis.extraPaths作用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-12-12
Python3實(shí)現(xiàn)爬取指定百度貼吧頁面并保存頁面數(shù)據(jù)生成本地文檔的方法
這篇文章主要介紹了Python3實(shí)現(xiàn)爬取指定百度貼吧頁面并保存頁面數(shù)據(jù)生成本地文檔的方法,涉及Python基于urllib模塊的頁面爬取與文件讀寫相關(guān)操作技巧,需要的朋友可以參考下2018-04-04
基于python實(shí)現(xiàn)模擬數(shù)據(jù)結(jié)構(gòu)模型
這篇文章主要介紹了基于python實(shí)現(xiàn)模擬數(shù)據(jù)結(jié)構(gòu)模型,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

