成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

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

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

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

公式

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

pytorch中如何計算交叉熵損失

其中,其中yi表示真實的分類結(jié)果。這里只給出公式,關(guān)于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("網(wǎng)絡(luò)輸出為5類:")
print(output)
print("要計算label的類別:")
print(label)
print("計算loss的結(jié)果:")
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("自己的計算結(jié)果:")
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("網(wǎng)絡(luò)輸出為3個5類:")
print(output)
print("要計算loss的類別:")
print(label)
print("計算loss的結(jié)果:")
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("自己的計算結(jié)果:")
print(res/3)

pytorch中如何計算交叉熵損失

nn.CrossEntropyLoss()中的計算方法

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

在pytorch中,CrossEntropyLoss計算公式為:

pytorch中如何計算交叉熵損失

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

pytorch中如何計算交叉熵損失

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

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

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

廣告

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

外貿(mào)網(wǎng)站建設(shè)
行唐县| 元氏县| 永仁县| 海盐县| 汝城县| 临江市| 沅江市| 嵊州市| 安平县| 芦山县| 滨州市| 定远县| 禄劝| 永善县| 日土县| 宣威市| 梁山县| 红桥区| 平原县| 得荣县| 方正县| 独山县| 海晏县| 建平县| 溧阳市| 资源县| 江津市| 梁平县| 桂东县| 黄石市| 彭阳县| 盐亭县| 黔江区| 余姚市| 肃宁县| 日喀则市| 嘉义市| 左贡县| 汨罗市| 常山县| 武功县|