基于MSELoss()與CrossEntropyLoss()的區(qū)別詳解
基于pytorch來講
MSELoss()多用于回歸問題,也可以用于one_hotted編碼形式,
CrossEntropyLoss()名字為交叉熵?fù)p失函數(shù),不用于one_hotted編碼形式
MSELoss()要求batch_x與batch_y的tensor都是FloatTensor類型
CrossEntropyLoss()要求batch_x為Float,batch_y為L(zhǎng)ongTensor類型
(1)CrossEntropyLoss() 舉例說明:
比如二分類問題,最后一層輸出的為2個(gè)值,比如下面的代碼:
class CNN (nn.Module ) : def __init__ ( self , hidden_size1 , output_size , dropout_p) : super ( CNN , self ).__init__ ( ) self.hidden_size1 = hidden_size1 self.output_size = output_size self.dropout_p = dropout_p self.conv1 = nn.Conv1d ( 1,8,3,padding =1) self.fc1 = nn.Linear (8*500, self.hidden_size1 ) self.out = nn.Linear (self.hidden_size1,self.output_size ) def forward ( self , encoder_outputs ) : cnn_out = F.max_pool1d ( F.relu (self.conv1(encoder_outputs)),2) cnn_out = F.dropout ( cnn_out ,self.dropout_p) #加一個(gè)dropout cnn_out = cnn_out.view (-1,8*500) output_1 = torch.tanh ( self.fc1 ( cnn_out ) ) output = self.out ( ouput_1) return output
最后的輸出結(jié)果為:
上面一個(gè)tensor為output結(jié)果,下面為target,沒有使用one_hotted編碼。
訓(xùn)練過程如下:
cnn_optimizer = torch.optim.SGD(cnn.parameters(),learning_rate,momentum=0.9,\ weight_decay=1e-5) criterion = nn.CrossEntropyLoss() def train ( input_variable , target_variable , cnn , cnn_optimizer , criterion ) : cnn_output = cnn( input_variable ) print(cnn_output) print(target_variable) loss = criterion ( cnn_output , target_variable) cnn_optimizer.zero_grad () loss.backward( ) cnn_optimizer.step( ) #print('loss: ',loss.item()) return loss.item() #返回?fù)p失
說明CrossEntropyLoss()是output兩位為one_hotted編碼形式,但target不是one_hotted編碼形式。
(2)MSELoss() 舉例說明:
網(wǎng)絡(luò)結(jié)構(gòu)不變,但是標(biāo)簽是one_hotted編碼形式。下面的圖僅做說明,網(wǎng)絡(luò)結(jié)構(gòu)不太對(duì),出來的預(yù)測(cè)也不太對(duì)。
如果target不是one_hotted編碼形式會(huì)報(bào)錯(cuò),報(bào)的錯(cuò)誤如下。
目前自己理解的兩者的區(qū)別,就是這樣的,至于多分類問題是不是也是樣的有待考察。
以上這篇基于MSELoss()與CrossEntropyLoss()的區(qū)別詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python 3.6 中使用pdfminer解析pdf文件的實(shí)現(xiàn)
這篇文章主要介紹了Python 3.6 中使用pdfminer解析pdf文件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09python實(shí)現(xiàn)圖像識(shí)別功能
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)圖像識(shí)別功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01python中利用Future對(duì)象異步返回結(jié)果示例代碼
future是一種對(duì)象,表示異步執(zhí)行的操作。下面這篇文章主要給大家介紹了關(guān)于python中利用Future對(duì)象異步返回結(jié)果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09python創(chuàng)建ArcGIS shape文件的實(shí)現(xiàn)
今天小編就為大家分享一篇python創(chuàng)建ArcGIS shape文件的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12使用 Python 實(shí)現(xiàn)微信公眾號(hào)粉絲遷移流程
近日,因公司業(yè)務(wù)需要,需將原兩個(gè)公眾號(hào)合并為一個(gè),即要將其中一個(gè)公眾號(hào)(主要是粉絲)遷移到另一個(gè)公眾號(hào)。這篇文章主要介紹了使用 Python 實(shí)現(xiàn)微信公眾號(hào)粉絲遷移,需要的朋友可以參考下2018-01-01PyQt5的PyQtGraph實(shí)踐系列3之實(shí)時(shí)數(shù)據(jù)更新繪制圖形
這篇文章主要介紹了PyQt5的PyQtGraph實(shí)踐系列3之實(shí)時(shí)數(shù)據(jù)更新繪制圖形,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05