Pytorch中Softmax與LogSigmoid的對比分析
Pytorch中Softmax與LogSigmoid的對比
torch.nn.Softmax
作用:
1、將Softmax函數(shù)應(yīng)用于輸入的n維Tensor,重新改變它們的規(guī)格,使n維輸出張量的元素位于[0,1]范圍內(nèi),并求和為1。
2、返回的Tensor與原Tensor大小相同,值在[0,1]之間。
3、不建議將其與NLLLoss一起使用,可以使用LogSoftmax代替之。
4、Softmax的公式:
參數(shù):
維度,待使用softmax計算的維度。
例子:
# 隨機初始化一個tensor a = torch.randn(2, 3) print(a) # 輸出tensor # 初始化一個Softmax計算對象,在輸入tensor的第2個維度上進行此操作 m = nn.Softmax(dim=1) # 將a進行softmax操作 output = m(a) print(output) # 輸出tensor tensor([[ 0.5283, 0.3922, -0.0484], [-1.6257, -0.4775, 0.5645]]) tensor([[0.4108, 0.3585, 0.2307], [0.0764, 0.2408, 0.6828]])
可以看見的是,無論輸入的tensor中的值為正或為負,輸出的tensor中的值均為正值,且加和為1。
當(dāng)m的參數(shù)dim=1時,輸出的tensor將原tensor按照行進行softmax操作;當(dāng)m的參數(shù)為dim=0時,輸出的tensor將原tensor按照列進行softmax操作。
深度學(xué)習(xí)拓展:
一般來說,Softmax函數(shù)會用于分類問題上。例如,在VGG等深度神經(jīng)網(wǎng)絡(luò)中,圖像經(jīng)過一系列卷積、池化操作后,我們可以得到它的特征向量,為了進一步判斷此圖像中的物體屬于哪個類別,我們會將該特征向量變?yōu)椋侯悇e數(shù) * 各類別得分 的形式,為了將得分轉(zhuǎn)換為概率值,我們會將該向量再經(jīng)過一層Softmax處理。
torch.nn.LogSigmoid
公式:
函數(shù)圖:
可以見得,函數(shù)值在[0, -]之間,輸入值越大函數(shù)值距離0越近,在一定程度上解決了梯度消失問題。
例子:
a = [[ 0.5283, 0.3922, -0.0484], [-1.6257, -0.4775, 0.5645]] a = torch.tensor(a) lg = nn.LogSigmoid() lgoutput = lg(a) print(lgoutput) tensor([[-0.4635, -0.5162, -0.7176], [-1.8053, -0.9601, -0.4502]])
二者比較:
import torch import torch.nn as nn # 設(shè)置a為 2*3 的tensor a = [[ 0.5283, 0.3922, -0.0484], [-1.6257, -0.4775, 0.5645]] a = torch.tensor(a) print(a) print('a.mean:', a.mean(1, True)) # 輸出a的 行平均值 m = nn.Softmax(dim=1) # 定義Softmax函數(shù),dim=1表示為按行計算 lg = nn.LogSigmoid() # 定義LogSigmoid函數(shù) output = m(a) print(output) # 輸出a經(jīng)過Softmax的結(jié)果的行平均值 print('output.mean:', output.mean(1, True)) lg_output = lg(a) print(lg_output) # 輸出a經(jīng)過LogSigmoid的結(jié)果的行平均值 print('lgouput.mean:', lg_output.mean(1, True)) # 結(jié)果: tensor([[ 0.5283, 0.3922, -0.0484], [-1.6257, -0.4775, 0.5645]]) a.mean: tensor(-0.1111) tensor([[0.4108, 0.3585, 0.2307], [0.0764, 0.2408, 0.6828]]) output.mean: tensor([[0.3333], [0.3333]]) # 經(jīng)過Softmax的結(jié)果的行平均值 tensor([[-0.4635, -0.5162, -0.7176], [-1.8053, -0.9601, -0.4502]]) lgouput.mean: tensor([[-0.5658], [-1.0719]]) # 經(jīng)過LogSigmoid的結(jié)果的行平均值
由上可知,繼續(xù)考慮分類問題,相同的數(shù)據(jù),經(jīng)過Softmax和LogSigmoid處理后,若取最大概率值對應(yīng)類別作為分類結(jié)果,那么:
1、第一行數(shù)據(jù)經(jīng)過Softmax后,會選擇第一個類別;經(jīng)過LogSigmoid后,會選擇第一個。
2、第二行數(shù)據(jù)經(jīng)過Softmax后,會選擇第三個類別;經(jīng)過LogSigmoid后,會選擇第三個。
3、一般來說,二者在一定程度上區(qū)別不是很大,由于sigmoid函數(shù)存在梯度消失問題,所以被使用的場景不多。
4、但是在多分類問題上,可以嘗試選擇Sigmoid函數(shù)來作為分類函數(shù),因為Softmax在處理多分類問題上,會更容易出現(xiàn)各項得分十分相近的情況。瓶頸值可以根據(jù)實際情況定。
nn.Softmax()與nn.LogSoftmax()
nn.Softmax()計算出來的值,其和為1,也就是輸出的是概率分布,具體公式如下:
這保證輸出值都大于0,在0,1范圍內(nèi)。
而nn.LogSoftmax()公式如下:
由于softmax輸出都是0-1之間的,因此logsofmax輸出的是小于0的數(shù),
softmax求導(dǎo):
logsofmax求導(dǎo):
例子:
import torch.nn as nn import torch import numpy as np
layer1=nn.Softmax() layer2=nn.LogSoftmax() input=np.asarray([2,3]) input=Variable(torch.Tensor(input)) output1=layer1(input) output2=layer2(input) print('output1:',output1) print('output2:',output2)
輸出:
output1: Variable containing:
0.2689
0.7311
[torch.FloatTensor of size 2]output2: Variable containing:
-1.3133
-0.3133
[torch.FloatTensor of size 2]
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實現(xiàn)批量上傳本地maven庫到nexus
這篇文章主要為大家詳細介紹了如何使用Python實現(xiàn)批量上傳本地maven庫到nexus,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的小伙伴可以參考下2024-01-01python爬取數(shù)據(jù)中的headers和代理IP問題分析
這篇文章主要為大家介紹了python爬取數(shù)據(jù)中的headers和代理IP問題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06