踩坑筆記(pytorch-bert,dataframe,交叉熵)

20210121 note

1、pytorch bert輸出的問題。

#model是加載的pytorch transformer里的bert模型
loss,logits = model(input_ids, attention_mask=masks)#會(huì)提示是str類型

tmpx = model(input_ids, attention_mask=masks)
loss=tmpx[0]
logits=tmpx[1]#有時(shí)候loss=None會(huì)出問題。
#比較合適的是用tmpx.loss,tmpx.logits來得到以下輸出值。
SequenceClassifierOutput(loss=tensor(1.2209, device='cuda:0', grad_fn=<NllLossBackward>), logits=tensor([[-0.0275, -0.2090, -0.1251, -0.2942],
        [ 0.0310, -0.2028, -0.1399, -0.3605],
        [-0.0671, -0.3543, -0.1225, -0.4625],
        [ 0.1389, -0.1244, -0.2310, -0.3664]], device='cuda:0',
       grad_fn=<AddmmBackward>), hidden_states=None, attentions=None)

2、dataframe的篩選問題。

import numpy as np
s = np.asarray([True] * 12)
s[3]=False
s[6]=False
import pandas as pd
x=pd.read_csv("boold.csv")
print(x[:12][s])
print(len(x[:12][s]))

輸出:值為False的列沒有被選中。

 Unnamed: 0                                              train  answer
0            0  <start> does ethanol take more energy make tha...       0
1            1  <start> is house tax and property tax are same...       1
2            2  <start> is pain experienced in a missing body ...       1
4            4  <start> is there a difference between hydroxyz...       1
5            5  <start> is barq s root beer a pepsi product <e...       0
7            7  <start> is there a word with q without u <end>...       1
8            8  <start> can u drive in canada with us license ...       1
9            9  <start> is there a play off for third place in...       1
10          10  <start> can minors drink with parents in new y...       1
11          11  <start> is the show bloodline based on a 1 sto...       0
10

3、交叉熵?fù)p失函數(shù)

\mathcal{L} = - \frac{1}{N}\sum_{n=1}^Nlogp(y^{(n)}|s^{(n)})

可以直接 認(rèn)為是這樣的一個(gè)概率和,

如果是多分類,假設(shè)真實(shí)標(biāo)簽[0,1,2,1,1,2,1]

那就是-(logp(0|x)+logp(1|x)+log p(2|x)+log p(1|x)+log p(1|x)+log p(2|x)+log p(1|x))

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容