国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - PyTorch的SoftMax交叉熵損失和梯度用法

PyTorch的SoftMax交叉熵損失和梯度用法

2020-04-20 12:03_icrazy_ Python

今天小編就為大家分享一篇PyTorch的SoftMax交叉熵損失和梯度用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

在PyTorch中可以方便的驗證SoftMax交叉熵損失和對輸入梯度的計算

關(guān)于softmax_cross_entropy求導的過程,可以參考HERE

示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# -*- coding: utf-8 -*-
import torch
import torch.autograd as autograd
from torch.autograd import Variable
import torch.nn.functional as F
import torch.nn as nn
import numpy as np
 
# 對data求梯度, 用于反向傳播
data = Variable(torch.FloatTensor([[1.0, 2.0, 3.0], [1.0, 2.0, 3.0], [1.0, 2.0, 3.0]]), requires_grad=True)
 
# 多分類標簽 one-hot格式
label = Variable(torch.zeros((3, 3)))
label[0, 2] = 1
label[1, 1] = 1
label[2, 0] = 1
print(label)
 
# for batch loss = mean( -sum(Pj*logSj) )
# for one : loss = -sum(Pj*logSj)
loss = torch.mean(-torch.sum(label * torch.log(F.softmax(data, dim=1)), dim=1))
 
loss.backward()
print(loss, data.grad)

輸出:

?
1
2
3
4
5
6
7
tensor([[ 0., 0., 1.],
    [ 0., 1., 0.],
    [ 1., 0., 0.]])
# loss:損失 和 input's grad:輸入的梯度
tensor(1.4076) tensor([[ 0.0300, 0.0816, -0.1116],
    [ 0.0300, -0.2518, 0.2217],
    [-0.3033, 0.0816, 0.2217]])

注意

對于單輸入的loss 和 grad

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
data = Variable(torch.FloatTensor([[1.0, 2.0, 3.0]]), requires_grad=True)
 
 
label = Variable(torch.zeros((1, 3)))
#分別令不同索引位置label為1
label[0, 0] = 1
# label[0, 1] = 1
# label[0, 2] = 1
print(label)
 
# for batch loss = mean( -sum(Pj*logSj) )
# for one : loss = -sum(Pj*logSj)
loss = torch.mean(-torch.sum(label * torch.log(F.softmax(data, dim=1)), dim=1))
 
loss.backward()
print(loss, data.grad)

其輸出:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 第一組:
lable: tensor([[ 1., 0., 0.]])
loss: tensor(2.4076)
grad: tensor([[-0.9100, 0.2447, 0.6652]])
 
# 第二組:
lable: tensor([[ 0., 1., 0.]])
loss: tensor(1.4076)
grad: tensor([[ 0.0900, -0.7553, 0.6652]])
 
# 第三組:
lable: tensor([[ 0., 0., 1.]])
loss: tensor(0.4076)
grad: tensor([[ 0.0900, 0.2447, -0.3348]])
 
"""
解釋:
對于輸入數(shù)據(jù) tensor([[ 1., 2., 3.]]) softmax之后的結(jié)果如下
tensor([[ 0.0900, 0.2447, 0.6652]])
交叉熵求解梯度推導公式可知 s[0, 0]-1, s[0, 1]-1, s[0, 2]-1 是上面三組label對應的輸入數(shù)據(jù)梯度
"""

pytorch提供的softmax, 和log_softmax 關(guān)系

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 官方提供的softmax實現(xiàn)
In[2]: import torch
 ...: import torch.autograd as autograd
 ...: from torch.autograd import Variable
 ...: import torch.nn.functional as F
 ...: import torch.nn as nn
 ...: import numpy as np
In[3]: data = Variable(torch.FloatTensor([[1.0, 2.0, 3.0]]), requires_grad=True)
In[4]: data
Out[4]: tensor([[ 1., 2., 3.]])
In[5]: e = torch.exp(data)
In[6]: e
Out[6]: tensor([[ 2.71837.3891, 20.0855]])
In[7]: s = torch.sum(e, dim=1)
In[8]: s
Out[8]: tensor([ 30.1929])
In[9]: softmax = e/s
In[10]: softmax
Out[10]: tensor([[ 0.0900, 0.2447, 0.6652]])
In[11]: # 等同于 pytorch 提供的 softmax
In[12]: org_softmax = F.softmax(data, dim=1)
In[13]: org_softmax
Out[13]: tensor([[ 0.0900, 0.2447, 0.6652]])
In[14]: org_softmax == softmax # 計算結(jié)果相同
Out[14]: tensor([[ 1, 1, 1]], dtype=torch.uint8)
 
# 與log_softmax關(guān)系
# log_softmax = log(softmax)
In[15]: _log_softmax = torch.log(org_softmax)
In[16]: _log_softmax
Out[16]: tensor([[-2.4076, -1.4076, -0.4076]])
In[17]: log_softmax = F.log_softmax(data, dim=1)
In[18]: log_softmax
Out[18]: tensor([[-2.4076, -1.4076, -0.4076]])

官方提供的softmax交叉熵求解結(jié)果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-
import torch
import torch.autograd as autograd
from torch.autograd import Variable
import torch.nn.functional as F
import torch.nn as nn
import numpy as np
 
data = Variable(torch.FloatTensor([[1.0, 2.0, 3.0], [1.0, 2.0, 3.0], [1.0, 2.0, 3.0]]), requires_grad=True)
log_softmax = F.log_softmax(data, dim=1)
 
label = Variable(torch.zeros((3, 3)))
label[0, 2] = 1
label[1, 1] = 1
label[2, 0] = 1
print("lable: ", label)
 
# 交叉熵的計算方式之一
loss_fn = torch.nn.NLLLoss() # reduce=True loss.sum/batch & grad/batch
# NLLLoss輸入是log_softmax, target是非one-hot格式的label
loss = loss_fn(log_softmax, torch.argmax(label, dim=1))
loss.backward()
print("loss: ", loss, "\ngrad: ", data.grad)
 
"""
# 交叉熵計算方式二
loss_fn = torch.nn.CrossEntropyLoss() # the target label is NOT an one-hotted
#CrossEntropyLoss適用于分類問題的損失函數(shù)
#input:沒有softmax過的nn.output, target是非one-hot格式label
loss = loss_fn(data, torch.argmax(label, dim=1))
loss.backward()
print("loss: ", loss, "\ngrad: ", data.grad)
"""
"""

輸出

?
1
2
3
4
5
6
7
lable: tensor([[ 0., 0., 1.],
    [ 0., 1., 0.],
    [ 1., 0., 0.]])
loss: tensor(1.4076)
grad: tensor([[ 0.0300, 0.0816, -0.1116],
    [ 0.0300, -0.2518, 0.2217],
    [-0.3033, 0.0816, 0.2217]])

通過和示例的輸出對比, 發(fā)現(xiàn)兩者是一樣的

以上這篇PyTorch的SoftMax交叉熵損失和梯度用法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/u010472607/article/details/82705567

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 亚洲人视频 | 日韩色综合 | 欧洲精品久久久久毛片完整版 | 亚洲精品久久久久久一区二区 | 日韩精品一区二区三区在线播放 | 国产欧美综合一区二区三区 | 一级全黄少妇性色生活片免费 | 人一级毛片 | 国产精品久久久久久久久久久久冷 | 一区二区视频 | 亚洲人成网站b2k3cm | 欧美精品日韩精品 | 久久性色 | 网站av| 午夜av网站 | 欧州一区二区三区 | 日韩久久久久久 | 亚洲精品男人的天堂 | 欧美视频网站 | 免费伊人网 | 欧美激情视频一区二区三区在线播放 | 亚洲国产精品尤物yw在线观看 | 99免费视频 | 精品视频一区二区三区 | 欧美精品在欧美一区二区少妇 | 欧美国产91| 亚洲午夜精品视频 | 欧美片网站免费 | 一级电影在线观看 | 欧美日韩精品久久久 | 欧美日韩视频 | 黄色资源网站 | 久久久精品一区二区 | 人人操天天射 | 久草热线 | 色人久久 | 亚洲成人福利网 | 国产精品久久久av | 国产精品成人一区二区三区夜夜夜 | 日本a视频 | 一区二区国产精品 |