詳解基于Transformer實(shí)現(xiàn)電影評論星級分類任務(wù)
Transformer模型概述
Transformer是一種用于序列到序列學(xué)習(xí)的神經(jīng)網(wǎng)絡(luò)架構(gòu),專門用于處理輸入和輸出序列之間的依賴關(guān)系。該模型被廣泛應(yīng)用于機(jī)器翻譯、音頻轉(zhuǎn)錄、語言生成等多個自然語言處理領(lǐng)域。
Transformer基于attention機(jī)制來實(shí)現(xiàn)序列到序列的學(xué)習(xí)。 在RNN(循環(huán)神經(jīng)網(wǎng)絡(luò))中,網(wǎng)絡(luò)必須按順序遍歷每個單詞,并在每個時間步計(jì)算隱層表示。 這樣,在長段文本中,信息可能會從網(wǎng)絡(luò)的起點(diǎn)傳遞到終點(diǎn),這導(dǎo)致了難以捕捉遠(yuǎn)距離依賴關(guān)系的問題。而attention機(jī)制可以根據(jù)輸入序列中的詞與其它所有詞的相關(guān)性分配不同的權(quán)重,從而突破了序列到序列中的局限。
具體來說,一個Transformer模型由編碼器(encoder)和解碼器(decoder)兩部分組成。編碼器用于接收輸入序列,解碼器用于生成輸出序列。每個編碼器和解碼器均包含多頭attention機(jī)制、前饋網(wǎng)絡(luò)以及殘差連接等組件。
在一個典型的Transformer模型中,首先將輸入序列通過嵌入層進(jìn)行向量化,然后將向量表示作為Transformer的第一層輸入。處理完輸入向量之后,下一層就是多頭attention層,其中每個頭(head)都可以計(jì)算出不同的注意力權(quán)重向量(也稱為attention mask)。最后,利用殘差連接和skip connection機(jī)制使transformer更易于訓(xùn)練。
數(shù)據(jù)集準(zhǔn)備
在此任務(wù)中,我們將使用來自IMDB的電影評論數(shù)據(jù)集,該數(shù)據(jù)集包含50,000條有標(biāo)簽的電影評論,每個評論標(biāo)記為正面或負(fù)面情感。 其中25,000個用于訓(xùn)練,另外25,000個用于測試。
由于Transformer是對token進(jìn)行操作,所以我們需要對文本的每個單詞進(jìn)行編碼。一種常用的方法是使用Bert Tokenizer。GPT-2等預(yù)訓(xùn)練模型會使用特定的tokenizer。選擇最新版本的transformers包可以快速實(shí)現(xiàn)這些操作:
!pip install transformers
接著加載tokenizer:
from transformers import BertTokenizer tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
上述操作將下載并加載適用于bert的tokenizer。 下一步是讀取IMDB數(shù)據(jù)集的內(nèi)容。 在本文中,我們將使用此處的已處理好的CSV形式數(shù)據(jù):drive.google.com/file/d/1b_b…
import pandas as pd train_df = pd.read_csv('imdb_train.csv') test_df = pd.read_csv('imdb_test.csv')
由于Transformer模型需要固定長度的輸入序列,我們選擇了max_length為100并對所有評論進(jìn)行padding操作:
train_inputs = tokenizer(list(train_df['review']), padding=True, truncation=True, max_length=100) test_inputs = tokenizer(list(test_df['review']), padding=True, truncation=True, max_length=100)
現(xiàn)在我們可以將輸入和標(biāo)簽分別轉(zhuǎn)換成torch Tensor類型:
import torch train_labels = torch.tensor(list(train_df['sentiment'].replace({'pos': 1, 'neg':0}))) test_labels = torch.tensor(list(test_df['sentiment'].replace({'pos': 1, 'neg':0}))) train_encoded_dict = { 'input_ids': torch.tensor(train_inputs['input_ids']), 'token_type_ids': torch.tensor(train_inputs['token_type_ids']), 'attention_mask': torch.tensor(train_inputs['attention_mask']), 'labels': train_labels } test_encoded_dict = { 'input_ids': torch.tensor(test_inputs['input_ids']), 'token_type_ids': torch.tensor(test_inputs['token_type_ids']), 'attention_mask': torch.tensor(test_inputs['attention_mask']), 'labels': test_labels }
模型訓(xùn)練
在此任務(wù)中,我們將使用PyTorch庫實(shí)現(xiàn)Transformer模型。 PyTorch是一種基于Python的科學(xué)計(jì)算包,其靈活性和易用性使其成為深度學(xué)習(xí)領(lǐng)域最常用的庫之一。
可以使用Hugging Face的Transformers實(shí)現(xiàn)預(yù)先訓(xùn)練好的BERT模型:
from transformers import BertForSequenceClassification, AdamW, BertConfig model = BertForSequenceClassification.from_pretrained( "bert-base-uncased", num_labels = 2, output_attentions = False, output_hidden_states = False, )
然后,我們需要定義優(yōu)化器、損失函數(shù)和批大小等訓(xùn)練超參數(shù):
optimizer = AdamW(model.parameters(), lr = 2e-5, eps = 1e-8) from torch.utils.data import DataLoader, RandomSampler, SequentialSampler batch_size = 32 train_dataloader = DataLoader(train_encoded_dict, sampler = RandomSampler(train_encoded_dict), batch_size = batch_size) test_dataloader = DataLoader(test_encoded_dict, sampler = SequentialSampler(test_encoded_dict), batch_size = batch_size) from transformers import get_linear_schedule_with_warmup epochs = 4 total_steps = len(train_dataloader) * epochs scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps = 0, num_training_steps = total_steps) loss_fn = torch.nn.CrossEntropyLoss()
最后,我們可以定義模型的訓(xùn)練過程,并進(jìn)行模型訓(xùn)練:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) model.train() total_train_loss = 0 for epoch_i in range(epochs): print(f"{'':^5}Epoch:{epoch_i + 1:^3}") for step, batch in enumerate(train_dataloader): b_input_ids = batch['input_ids'].to(device) b_token_type_ids = batch['token_type_ids'].to(device) b_attention_mask = batch['attention_mask'].to(device) b_labels = batch['labels'].to(device) model.zero_grad() outputs = model(b_input_ids, token_type_ids=b_token_type_ids, attention_mask=b_attention_mask, labels=b_labels) loss = outputs.loss total_train_loss += loss.item() loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0) optimizer.step() scheduler.step() avg_train_loss = total_train_loss / len(train_dataloader) print(" Average training loss: {avg_train_loss:.2f}") def evaluate(model, test_dataloader): model.eval() total_eval_accuracy = 0 total_eval_loss = 0 nb_eval_steps = 0 for batch in test_dataloader: b_input_ids = batch['input_ids'].to(device) b_token_type_ids = batch['token_type_ids'].to(device) b_attention_mask = batch['attention_mask'].to(device) b_labels = batch['labels'].to(device) with torch.no_grad(): outputs = model(b_input_ids, token_type_ids=b_token_type_ids, attention_mask=b_attention_mask, labels=b_labels) loss = outputs.loss logits = outputs.logits total_eval_loss += loss.item() logits = logits.detach().cpu().numpy() label_ids = b_labels.to('cpu').numpy() total_eval_accuracy += flat_accuracy(logits, label_ids) avg_val_accuracy = total_eval_accuracy / len(test_dataloader) avg_val_loss = total_eval_loss / len(test_dataloader) return avg_val_accuracy, avg_val_loss accuracy, val_loss = evaluate(model, test_dataloader) print(f'Accuracy: {accuracy:.2f}%')
訓(xùn)練結(jié)束后,我們可以使用測試集對模型進(jìn)行評估。TensorFlow提供了非常好的評估函數(shù)可以在別人的工程稍微改下直接拿來用:
from sklearn.metrics import accuracy_score def flat_accuracy(preds, labels): pred_flat = np.argmax(preds, axis=1).flatten() labels_flat = labels.flatten() return accuracy_score(labels_flat, pred_flat)
模型調(diào)整和優(yōu)化
下面是一些可能有助于Transformer模型性能的調(diào)整和優(yōu)化方法。
(1)最大輸入序列長度: Transformer模型需要固定大小的輸入序列。在IMDB任務(wù)中,我們將max_length設(shè)置為100。調(diào)整這個參數(shù)會影響到模型的性能,長時間耗時與顯存限制等都會影響選擇。
(2)學(xué)習(xí)率、批大小、迭代次數(shù)等訓(xùn)練超參數(shù)的調(diào)整: 常用策略包括指數(shù)衰減學(xué)習(xí)率、增加批次大小、增加迭代次數(shù)等。
(3)使用預(yù)訓(xùn)練模型:隨著語言模型的發(fā)展,預(yù)訓(xùn)練語言模型在各種NLP任務(wù)中表現(xiàn)越來越好。因此,在這類任務(wù)中,可以通過使用預(yù)訓(xùn)練的模型來提高準(zhǔn)確性。適合使用這個方法的數(shù)據(jù)集規(guī)模越大,效果越明顯。
(4) 模型融合或集成: 許多競賽中,采用模型平均等方式提高模型的完整性和穩(wěn)健性。在結(jié)果更重要的大賽中尤為突出。
總結(jié)
首先簡要介紹了Transformer的基本結(jié)構(gòu)和工作原理,并解釋了為什么它適合于序列到序列的學(xué)習(xí)問題。然后,我們演示了如何獲取IMDB電影評論數(shù)據(jù),對其進(jìn)行標(biāo)記化處理,并將數(shù)據(jù)轉(zhuǎn)換為Tensor類型。最后,我們介紹了如何使用BertForSequenceClassification加載預(yù)處理的去停詞csv數(shù)據(jù),及PyTorch庫定義優(yōu)化器、損失函數(shù)和批大小等訓(xùn)練超參數(shù)來執(zhí)行模型訓(xùn)練和評估。
以上就是詳解基于Transformer實(shí)現(xiàn)電影評論星級分類任務(wù)的詳細(xì)內(nèi)容,更多關(guān)于Transformer電影評論星級分類的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Win11平臺安裝和配置NeoVim0.8.2編輯器搭建Python3開發(fā)環(huán)境詳細(xì)過程(2023最新攻略)
這篇文章主要介紹了Win11平臺安裝和配置NeoVim0.8.2編輯器搭建Python3開發(fā)環(huán)境(2023最新攻略),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01python利用遞歸方法實(shí)現(xiàn)求集合的冪集
這篇文章主要給大家介紹了關(guān)于python利用遞歸方法實(shí)現(xiàn)求集合的冪集的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Python要求O(n)復(fù)雜度求無序列表中第K的大元素實(shí)例
這篇文章主要介紹了Python要求O(n)復(fù)雜度求無序列表中第K的大元素實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04python pands實(shí)現(xiàn)execl轉(zhuǎn)csv 并修改csv指定列的方法
今天小編就為大家分享一篇python pands實(shí)現(xiàn)execl轉(zhuǎn)csv 并修改csv指定列的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12

解決keras+flask模型的重復(fù)調(diào)用出錯ValueError: Tensor is n

Python3.0與2.X版本的區(qū)別實(shí)例分析