国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

快速上手Pytorch實(shí)現(xiàn)BERT,以及BERT后接CNN/LSTM

這篇具有很好參考價(jià)值的文章主要介紹了快速上手Pytorch實(shí)現(xiàn)BERT,以及BERT后接CNN/LSTM。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

快速上手Pytorch實(shí)現(xiàn)BERT,以及BERT后接CNN/LSTM

本項(xiàng)目采用HuggingFace提供的工具實(shí)現(xiàn)BERT模型案例,并在BERT后接CNN、LSTM等
HuggingFace官網(wǎng)
快速上手Pytorch實(shí)現(xiàn)BERT,以及BERT后接CNN/LSTM

一、實(shí)現(xiàn)BERT(后接線性層)

1.引用案例源碼:
from transformers import BertTokenizer, BertModel
import torch
model_name = 'bert-base-uncased'

tokenizer = BertTokenizer.from_pretrained(model_name)
model = BertModel.from_pretrained(model_name)

inputs = tokenizer("Hello, my dog is cute", return_tensors="pt" , padding='max_length',max_length=10)
outputs = model(**inputs)
# print(inputs) # 字典類型的input_ids必選字段  101CLS  102SEP
last_hidden_states = outputs.last_hidden_state  # last_hidden_state 最后一層的輸出  pooler_output / hidden_states 

快速上手Pytorch實(shí)現(xiàn)BERT,以及BERT后接CNN/LSTM
程序會(huì)自行下載模型和配置文件,也可自行在官網(wǎng)上手動(dòng)下載
快速上手Pytorch實(shí)現(xiàn)BERT,以及BERT后接CNN/LSTM
模型返回的參數(shù)
快速上手Pytorch實(shí)現(xiàn)BERT,以及BERT后接CNN/LSTM

2. 自定義類調(diào)用數(shù)據(jù)集
class MyDataSet(Data.Dataset) :
    def __init__(self , data ,label):
        self.data = data # ['今天天氣很好', 'xxxx' , ……]
        self.label = label # [1 , 2 , 0]
        self.tokenizer = BertTokenizer.from_pretrained(model_name)
    def __getitem__(self , idx):
        text = self.data[idx] # str
        label = self.label[idx]
        inputs = self.tokenizer(text , return_tensors="pt" , padding='max_length',max_length=10 , truncation = True)  # truncation = True 是否進(jìn)行截?cái)嗖僮?/span>
        input_ids = inputs.input_ids.squeeze(0) # squeeze(0) 對張量進(jìn)行降維操作 為啥降維:輸入的data是一句話(一維)但生成的input_ids默認(rèn)是二維,因此需要降維
        token_type_ids = inputs.token_type_ids.squeeze(0)
        attention_mask = inputs.attention_mask.squeeze(0)
        return input_ids , token_type_ids , attention_mask,label
    def __len__(self):
        return len(self.data)

squeeze(0)的作用: 舉個(gè)栗子

input_ids: tensor([[ 101, 7592, 1010, 2026, 3899, 2003, 10140, 102, 0, 0]])
b = input_ids.squeeze(0)
b: tensor([ 101, 7592, 1010, 2026, 3899, 2003, 10140, 102, 0, 0])
當(dāng)張量是一個(gè)1 * n 維度的張量時(shí),input_ids的維度是 1 * 10,調(diào)用squeeze(0) 將張量降成1維。
若不是1 * n的這種2維張量,如本就是1維的,或者m * n(其中m和n都是大于1的),調(diào)用這個(gè)函數(shù)沒有效果。

squeeze(1)和squeeze(-1)作用相同 ,與squeeze(0)類似
將一個(gè)n*1維度的張量降成1維

3. 將數(shù)據(jù)集傳入模型
data , label = [] , []
with open('./dataset/data.txt') as f:
    for line in f.readlines():
        a, b = line.strip().split(' ')
        data.append(a)
        label.append(int(b))
dataset = MyDataSet(data,label)
dataloader = Data.DataLoader(dataset , batch_size = 2,shuffle =True)
4.自定義模型
class MyModel(nn.Module):
    def __init__(self):
        super(MyModel,self).__init__()
        self.bert = BertModel.from_pretrained(model_name)
        self.liner = nn.Linear(768, 3)  # "hidden_size": 768
    
    def forward(self , input_ids , token_type_ids , attention_mask) :
        output = self.bert(input_ids , token_type_ids ,attention_mask).pooler_output # [batch_size , hidden_size]
        # print(output.shape)
        output = self.linnear(output)
        return output
5.配置運(yùn)行環(huán)境
device = torch.device('cuda')
model = MyModel().to(device)
loss_fun = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters() , lr = 1e-5)
6.訓(xùn)練模型,計(jì)算損失
for epoch in range(10):
    for input_ids,token_type_ids,attention_mask ,label in dataloader:
        input_ids,token_type_ids,attention_mask ,label = input_ids.to(device),token_type_ids.to(device),attention_mask.to(device) ,label.to(device)
        pred = model(input_ids,token_type_ids,attention_mask)
        
        loss = loss_fn(pred , label)
        print(loss.item())
        
        loss.backward()
        optimizer.step()
        optimizer.zero_grad()

快速上手Pytorch實(shí)現(xiàn)BERT,以及BERT后接CNN/LSTM
易出現(xiàn)顯存不夠的錯(cuò)誤,可以在服務(wù)器控制臺中輸入nvidia-smi //查看所有進(jìn)程信息
快速上手Pytorch實(shí)現(xiàn)BERT,以及BERT后接CNN/LSTM
選擇需要?dú)⑺赖倪M(jìn)程taskkill /PID PTD號 /F //使用taskkill 進(jìn)程id,殺死進(jìn)程
快速上手Pytorch實(shí)現(xiàn)BERT,以及BERT后接CNN/LSTM

二、BERT+CNN

添加卷積層,查看需要的參數(shù)
快速上手Pytorch實(shí)現(xiàn)BERT,以及BERT后接CNN/LSTM
快速上手Pytorch實(shí)現(xiàn)BERT,以及BERT后接CNN/LSTM
輸入層和輸出層之間的參數(shù)關(guān)系為:Wout = (Win + 2p - w)/ s +1 ; Hout = (Hin + 2p - w)/ s +1
其中Win = maxlen,Hin = hidden_size,卷積核大小為 w * h ,p表示padding(默認(rèn)為0),s表示卷積步長(默認(rèn)為1)
因此輸出為 (10 + 2 * 0 - 2)/ 1 + 1 = 9 ,(768 + 2 * 0 - 768)/ 1 + 1 = 1

class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.bert = BertModel.from_pretrained(model_name)

        '''
        BERT后接CNN
        '''
        self.conv = nn.Conv2d(1 ,3 ,kernel_size=(2,768)) # in_channels 輸入的通道數(shù) out_channels 經(jīng)過卷積之后的通道數(shù) kernel_size 卷積核大小
        self.linear = nn.Linear(27, 3)  # "hidden_size": 768

    def forward(self, input_ids, token_type_ids, attention_mask):
        '''

        x : [batch , channel , width , height]

        '''
        batch = input_ids.size(0)
        output = self.bert(input_ids, token_type_ids, attention_mask).last_hidden_state  # [batch_size , seq , hidden_size]
        output = output.unsqueeze(1) # [batch , 1, seq , hidden_size] 三維擴(kuò)展成四維
        # print(output.shape)
        output = self.conv(output) # [batch , 3, 9 ,1]
        print(output.shape)
        # 為了進(jìn)行分類,希望將四維轉(zhuǎn)換成二維 # [batch , 3]
        output = output.view(batch , -1) # [batch , 3*9*1]

        output = self.linear(output)
        return output

輸出結(jié)果

torch.Size([2, 3, 9, 1])
1.0467640161514282
torch.Size([1, 3, 9, 1])
1.6651103496551514
torch.Size([2, 3, 9, 1])
1.1516715288162231
torch.Size([1, 3, 9, 1])
1.0645687580108643
torch.Size([2, 3, 9, 1])
1.0910512208938599
torch.Size([1, 3, 9, 1])
0.9897172451019287
torch.Size([2, 3, 9, 1])
1.0313527584075928
torch.Size([1, 3, 9, 1])
1.0067516565322876
torch.Size([2, 3, 9, 1])
0.9847115278244019
torch.Size([1, 3, 9, 1])
1.01240873336792
torch.Size([2, 3, 9, 1])
0.9597381353378296
torch.Size([1, 3, 9, 1])
0.9435619115829468
torch.Size([2, 3, 9, 1])
0.9591015577316284
torch.Size([1, 3, 9, 1])
0.8384571075439453
torch.Size([2, 3, 9, 1])
0.9722234010696411
torch.Size([1, 3, 9, 1])
0.7264331579208374
torch.Size([2, 3, 9, 1])
0.9841375350952148
torch.Size([1, 3, 9, 1])
0.6240622997283936
torch.Size([2, 3, 9, 1])
0.7659112811088562
torch.Size([1, 3, 9, 1])
1.0371975898742676

三、BERT+LSTM

添加LSTM,查看需要哪些參數(shù)
快速上手Pytorch實(shí)現(xiàn)BERT,以及BERT后接CNN/LSTM
快速上手Pytorch實(shí)現(xiàn)BERT,以及BERT后接CNN/LSTM

class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.bert = BertModel.from_pretrained(model_name)
        '''
        BERT后接LSTM
        '''
        self.lstm = nn.LSTM(input_size=768, hidden_size= 512 ,num_layers= 1 , batch_first= True , bidirectional=True) # batch_first = True 表示輸入輸出順序(batch,seq,feature) LSTM默認(rèn)(seq,batch,feature)
        self.linear = nn.Linear(1024, 3)  # "hidden_size": 768

    def forward(self, input_ids, token_type_ids, attention_mask):
        '''
        x : [batch , seq]
        '''
        batch = input_ids.size(0)
        output = self.bert(input_ids, token_type_ids, attention_mask).last_hidden_state  # [batch_size , seq , hidden_size]
        output , _ = self.lstm(output)
        print(output.shape) # [2 , seq ,2*hidden_size]

        # 使用LSTM最后一層的輸出做分類
        output = output[: ,-1,:] # [batch , 1024]
        print('最后一層' ,output.shape)
        output = self.linear(output)
        return output

輸出結(jié)果

{‘input_ids’: tensor([[ 101, 7592, 1010, 2026, 3899, 2003,
10140, 102, 0, 0]]), ‘token_type_ids’: tensor([[0, 0, 0, 0,
0, 0, 0, 0, 0, 0]]), ‘a(chǎn)ttention_mask’: tensor([[1, 1, 1, 1, 1, 1, 1,
1, 0, 0]])} [‘今天天氣很好’, ‘今天天氣很不好’, ‘明天天氣非常好’] [1, 0, 2]

torch.Size([2, 10, 1024]) 最后一層 torch.Size([2, 1024])
1.0788244009017944 torch.Size([1, 10, 1024]) 最后一層 torch.Size([1, 1024])
1.3834939002990723 torch.Size([2, 10, 1024]) 最后一層 torch.Size([2, 1024])
1.155088186264038 torch.Size([1, 10, 1024]) 最后一層 torch.Size([1, 1024])
1.0809415578842163 torch.Size([2, 10, 1024]) 最后一層 torch.Size([2, 1024])
1.061639666557312 torch.Size([1, 10, 1024]) 最后一層 torch.Size([1, 1024])
1.1302376985549927 torch.Size([2, 10, 1024]) 最后一層 torch.Size([2, 1024])
1.0572789907455444 torch.Size([1, 10, 1024]) 最后一層 torch.Size([1, 1024])
1.086378812789917 torch.Size([2, 10, 1024]) 最后一層 torch.Size([2, 1024])
1.0700803995132446 torch.Size([1, 10, 1024]) 最后一層 torch.Size([1, 1024])
1.0184061527252197 torch.Size([2, 10, 1024]) 最后一層 torch.Size([2, 1024])
0.9948051571846008 torch.Size([1, 10, 1024]) 最后一層 torch.Size([1, 1024])
1.203598976135254 torch.Size([2, 10, 1024]) 最后一層 torch.Size([2, 1024])
1.1068116426467896 torch.Size([1, 10, 1024]) 最后一層 torch.Size([1, 1024])
0.9117098450660706 torch.Size([2, 10, 1024]) 最后一層 torch.Size([2, 1024])
0.9891176223754883 torch.Size([1, 10, 1024]) 最后一層 torch.Size([1, 1024])
1.1974778175354004 torch.Size([2, 10, 1024]) 最后一層 torch.Size([2, 1024])
1.0810655355453491 torch.Size([1, 10, 1024]) 最后一層 torch.Size([1, 1024])
0.8861477375030518 torch.Size([2, 10, 1024]) 最后一層 torch.Size([2, 1024])
0.9180283546447754 torch.Size([1, 10, 1024]) 最后一層 torch.Size([1, 1024])
1.2292695045471191

要實(shí)現(xiàn)BERT后接各種模型,最重要的是需要知道模型經(jīng)過每一層后的維度是多少,最粗暴的方式可以通過print輸出維度。文章來源地址http://www.zghlxwxcb.cn/news/detail-454831.html

到了這里,關(guān)于快速上手Pytorch實(shí)現(xiàn)BERT,以及BERT后接CNN/LSTM的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包