久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

pytorch中如何計算交叉熵損失-創(chuàng)新互聯

這篇文章將為大家詳細講解有關pytorch中如何計算交叉熵損失,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

麻江網站制作公司哪家好,找創(chuàng)新互聯建站!從網頁設計、網站建設、微信開發(fā)、APP開發(fā)、成都響應式網站建設公司等網站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯建站2013年開創(chuàng)至今到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選創(chuàng)新互聯建站。

公式

首先需要了解CrossEntropyLoss的計算過程,交叉熵的函數是這樣的:

pytorch中如何計算交叉熵損失

其中,其中yi表示真實的分類結果。這里只給出公式,關于CrossEntropyLoss的其他詳細細節(jié)請參照其他博文。

測試代碼(一維)

import torch
import torch.nn as nn
import math

criterion = nn.CrossEntropyLoss()
output = torch.randn(1, 5, requires_grad=True)
label = torch.empty(1, dtype=torch.long).random_(5)
loss = criterion(output, label)

print("網絡輸出為5類:")
print(output)
print("要計算label的類別:")
print(label)
print("計算loss的結果:")
print(loss)

first = 0
for i in range(1):
  first = -output[i][label[i]]
second = 0
for i in range(1):
  for j in range(5):
    second += math.exp(output[i][j])
res = 0
res = (first + math.log(second))
print("自己的計算結果:")
print(res)

pytorch中如何計算交叉熵損失

測試代碼(多維)

import torch
import torch.nn as nn
import math
criterion = nn.CrossEntropyLoss()
output = torch.randn(3, 5, requires_grad=True)
label = torch.empty(3, dtype=torch.long).random_(5)
loss = criterion(output, label)

print("網絡輸出為3個5類:")
print(output)
print("要計算loss的類別:")
print(label)
print("計算loss的結果:")
print(loss)

first = [0, 0, 0]
for i in range(3):
  first[i] = -output[i][label[i]]
second = [0, 0, 0]
for i in range(3):
  for j in range(5):
    second[i] += math.exp(output[i][j])
res = 0
for i in range(3):
  res += (first[i] + math.log(second[i]))
print("自己的計算結果:")
print(res/3)

pytorch中如何計算交叉熵損失

nn.CrossEntropyLoss()中的計算方法

注意:在計算CrossEntropyLosss時,真實的label(一個標量)被處理成onehot編碼的形式。

在pytorch中,CrossEntropyLoss計算公式為:

pytorch中如何計算交叉熵損失

CrossEntropyLoss帶權重的計算公式為(默認weight=None):

pytorch中如何計算交叉熵損失

關于“pytorch中如何計算交叉熵損失”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

網頁名稱:pytorch中如何計算交叉熵損失-創(chuàng)新互聯
分享鏈接:http://sd-ha.com/article42/decchc.html

成都網站建設公司_創(chuàng)新互聯,為您提供云服務器搜索引擎優(yōu)化、企業(yè)網站制作網站設計公司、外貿網站建設ChatGPT

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

外貿網站建設