数据集
从二分类的情感分析进阶到多分类情感分析,数据集采用TREC数据集,这个数据包括6个不同的问题类型。
导入数据
import os
import time
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
from torch.units.data import Dataset, DataLoader
from sklearn.model_selection import train_test_split
from torchtext.legacy import data
from torchtext.legacy import datasets
SEED = 2021
torch.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
TEXT = data.Field(tokenize = 'spacy',tokenizer_language = 'en_core_web_sm')
LABEL = data.LabelField()
train_data, test_data = datasets.TREC.splits(TEXT, LABEL, fine_grained=False)
train_data, valid_data = train_data.split(random_state = random.seed(SEED))
接下来根据数据集构建词汇表,数据集约3800个训练样本,词汇约为7500。
MAX_VOCAB_SIZE = 25_000
TEXT.build_vocab(train_data,
max_size = MAX_VOCAB_SIZE,
vectors = "glove.6B.100d",
unk_init = torch.Tensor.normal_)
LABEL.build_vocab(train_data)
检查训练集中的标签类别,一共6个标签:
- HUM: 关于人类的问题
- ENTY:关于实体的问题的
- DESC:关于要求提供描述的问题
- NUM:关于答案为数字的问题
- LOC:关于答案是位置的问题
- ABBR:关于询问缩写的问题
设置迭代器
BATCH_SIZE = 64
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
train_iterator, valid_iterator, test_iterator = data.BucketIterator.splits(
(train_data, valid_data, test_data), batch_size = BATCH_SIZE, device = device)
构建CNN模型
class CNN(nn.Module):
def __init__(self, vocab_size, embedding_dim, n_filters, filter_sizes, output_dim,
dropout, pad_idx):
super().__init__()
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.convs = nn.ModuleList([
nn.Conv2d(in_channels = 1,
out_channels = n_filters,
kernel_size = (fs, embedding_dim))
for fs in filter_sizes
])
self.fc = nn.Linear(len(filter_sizes) * n_filters, output_dim)
self.dropout = nn.Dropout(dropout)
def forward(self, text):
text = text.permute(1, 0)
embedded = self.embedding(text)
embedded = embedded.unsqueeze(1)
conved = [F.relu(conv(embedded)).squeeze(3) for conv in self.convs]
pooled = [F.max_pool1d(conv, conv.shape[2]).squeeze(2) for conv in conved]
cat = self.dropout(torch.cat(pooled, dim = 1))
return self.fc(cat)
设置CNN参数
INPUT_DIM = len(TEXT.vocab) #6个类别
EMBEDDING_DIM = 100
N_FILTERS = 100
FILTER_SIZES = [2,3,4]
OUTPUT_DIM = len(LABEL.vocab)
DROPOUT = 0.5
PAD_IDX = TEXT.vocab.stoi[TEXT.pad_token]
model = CNN(INPUT_DIM, EMBEDDING_DIM, N_FILTERS, FILTER_SIZES, OUTPUT_DIM, DROPOUT, PAD_IDX)
计算使用参数的量
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f'The model has {count_parameters(model):,} trainable parameters')
加载预训练embedding
pretrained_embeddings = TEXT.vocab.vectors
model.embedding.weight.data.copy_(pretrained_embeddings)
用0来初始化位置的权重和padding参数
UNK_IDX = TEXT.vocab.stoi[TEXT.unk_token]
model.embedding.weight.data[UNK_IDX] = torch.zeros(EMBEDDING_DIM)
model.embedding.weight.data[PAD_IDX] = torch.zeros(EMBEDDING_DIM)
构建优化器和损失函数
optimizer = optim.Adam(model.parameters())
criterion = nn.CrossEntropyLoss()
model = model.to(device)
criterion = criterion.to(device)
损失函数采用交叉熵损失 通常二分类采用BCEWithLogitsLoss,多分类采用CrossEntropyLoss
之前,我们有一个函数可以计算二进制标签情况下的准确度,我们说如果值超过 0.5,那么我们会假设它是正的。 在我们有C 个类(C > 2)的情况下,我们的模型输出一个C 维向量,其中每个元素的值是示例属于该类的置信度。
例如,在我们的标签中,我们有:‘HUM’ = 0、‘ENTY’ = 1、‘DESC’ = 2、‘NUM’ = 3、‘LOC’ = 4 和 ‘ABBR’ = 5。如果我们的输出 模型是这样的:[5.1, 0.3, 0.1, 2.1, 0.2, 0.6] 这意味着该模型确信该示例属于第 0 类:这是一个关于人类的问题,并且略微相信该示例属于该第3类:关于数字的问题。
我们通过执行 argmax 来获取批次中每个元素的预测最大值的索引,然后计算它与实际标签相等的次数来计算准确度。 然后我们对整个批次进行平均。
def categorical_accuracy(preds, y):
"""
Returns accuracy per batch, i.e. if you get 8/10 right, this returns 0.8, NOT 8
"""
top_pred = preds.argmax(1, keepdim = True)
correct = top_pred.eq(y.view_as(top_pred)).sum()
acc = correct.float() / y.shape[0]
return acc
训练循环与之前类似,CrossEntropyLoss期望输入数据为 [batch size, n classes] ,标签为 [batch size] 。 标签默认需要是一个 LongTensor类型的数据,因为我们没有像以前那样将 dtype 设置为 FloatTensor。
def train(model, iterator, optimizer, criterion):
epoch_loss = 0
epoch_acc = 0
model.train()
for batch in iterator:
optimizer.zero_grad()
predictions = model(batch.text)
loss = criterion(predictions, batch.label)
acc = categorical_accuracy(predictions, batch.label)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_acc += acc.item()
return epoch_loss / len(iterator), epoch_acc / len(iterator)
def evaluate(model, iterator, criterion):
epoch_loss = 0
epoch_acc = 0
model.eval()
with torch.no_grad():
for batch in iterator:
predictions = model(batch.text)
loss = criterion(predictions, batch.label)
acc = categorical_accuracy(predictions, batch.label)
epoch_loss += loss.item()
epoch_acc += acc.item()
return epoch_loss / len(iterator), epoch_acc / len(iterator)
import time
def epoch_time(start_time, end_time):
elapsed_time = end_time - start_time
elapsed_mins = int(elapsed_time / 60)
elapsed_secs = int(elapsed_time - (elapsed_mins * 60))
return elapsed_mins, elapsed_secs
main
EPOCHS = 5
best_valid_loss = float('inf')
for epoch in range(EPOCHS):
start_time = time.time()
train_loss, train_acc = train(model, train_iterator, optimizer, criterion)
valid_loss, valid_acc = evaluate(model, valid_iterator, criterion)
end_time = time.time()
epoch_mins, epoch_secs = epoch_time(start_time, end_time)
if valid_loss < best_valid_loss:
best_valid_loss = valid_loss
torch.save(model.state_dict(), 'tut5-model.pt')
print(f'Epoch: {epoch+1:02} | Epoch Time: {epoch_mins}m {epoch_secs}s')
print(f'\tTrain Loss: {train_loss:.3f} | Train Acc: {train_acc*100:.2f}%')
print(f'\t Val. Loss: {valid_loss:.3f} | Val. Acc: {valid_acc*100:.2f}%')
在测试集上运行
model.load_state_dict(torch.load('tut5-model.pt'))
test_loss, test_acc = evaluate(model, test_iterator, criterion)
print(f'Test Loss: {test_loss:.3f} | Test Acc: {test_acc*100:.2f}%')
创建一个函数来预测给定问题的类别 使用argmax获得最高的预测类索引,使用这个索引和标签vocab来获得可读的标签。
import spacy
nlp = spacy.load('en_core_web_sm')
def predict_class(model, sentence, min_len = 4):
model.eval()
tokenized = [tok.text for tok in nlp.tokenizer(sentence)]
if len(tokenized) < min_len:
tokenized += ['<pad>'] * (min_len - len(tokenized))
indexed = [TEXT.vocab.stoi[t] for t in tokenized]
tensor = torch.LongTensor(indexed).to(device)
tensor = tensor.unsqueeze(1)
preds = model(tensor)
max_preds = preds.argmax(dim = 1)
return max_preds.item()
在随机的问题上进行尝试
参考链接: https://github.com/datawhalechina/team-learning-nlp/blob/master/EmotionalAnalysis
|