Python 语言 用 PyTorch 实现情感分析 BERT 模型微调中文语料

Python阿木 发布于 1 天前 2 次阅读


使用 PyTorch 和 BERT 模型实现中文情感分析

情感分析是自然语言处理(NLP)领域的一个重要任务,它旨在识别和分类文本中的情感倾向。随着深度学习技术的快速发展,基于深度学习的情感分析模型在准确性和效率上取得了显著的成果。BERT(Bidirectional Encoder Representations from Transformers)模型作为一种预训练语言表示模型,在多种NLP任务中表现出色。本文将使用PyTorch框架,结合BERT模型,实现中文情感分析。

环境准备

在开始之前,请确保您的环境中已安装以下库:

- PyTorch
- Transformers
- torchtext
- torch
- pandas
- sklearn

您可以使用以下命令安装所需的库:

bash
pip install torch transformers torchtext pandas sklearn

数据准备

为了进行情感分析,我们需要一个中文情感分析数据集。这里我们以“ChnSentiCorp”数据集为例,该数据集包含电影评论,每条评论都标注了正面、负面或中性情感。

python
import pandas as pd

读取数据集
data = pd.read_csv('ChnSentiCorp.csv', encoding='gbk')

分离文本和标签
texts = data['content']
labels = data['label']

数据预处理

在训练模型之前,需要对数据进行预处理,包括分词、编码等。

python
from transformers import BertTokenizer

初始化分词器
tokenizer = BertTokenizer.from_pretrained('bert-base-chinese')

编码文本
encoded_inputs = tokenizer(texts.tolist(), padding=True, truncation=True, return_tensors='pt')
input_ids = encoded_inputs['input_ids']
attention_masks = encoded_inputs['attention_mask']

模型构建

接下来,我们将使用PyTorch和Transformers库构建一个基于BERT的情感分析模型。

python
import torch
from torch import nn
from transformers import BertModel

定义模型
class BertForSentimentAnalysis(nn.Module):
def __init__(self, num_labels):
super(BertForSentimentAnalysis, self).__init__()
self.bert = BertModel.from_pretrained('bert-base-chinese')
self.dropout = nn.Dropout(0.1)
self.classifier = nn.Linear(768, num_labels)

def forward(self, input_ids, attention_mask):
outputs = self.bert(input_ids, attention_mask=attention_mask)
sequence_output = outputs[0]
logits = self.classifier(sequence_output[:, 0, :])
return logits

初始化模型
num_labels = 3 正面、负面、中性
model = BertForSentimentAnalysis(num_labels)

训练模型

现在,我们可以使用训练数据来训练我们的模型。

python
from torch.optim import Adam
from torch.utils.data import DataLoader, TensorDataset

创建数据加载器
train_data = TensorDataset(input_ids, attention_masks, labels)
train_loader = DataLoader(train_data, batch_size=32, shuffle=True)

定义优化器和损失函数
optimizer = Adam(model.parameters(), lr=2e-5)
loss_fn = nn.CrossEntropyLoss()

训练模型
epochs = 4
for epoch in range(epochs):
model.train()
for batch in train_loader:
optimizer.zero_grad()
input_ids, attention_mask, labels = batch
outputs = model(input_ids, attention_mask)
loss = loss_fn(outputs, labels)
loss.backward()
optimizer.step()
print(f'Epoch {epoch+1}/{epochs}, Loss: {loss.item()}')

评估模型

在训练完成后,我们可以使用测试数据来评估模型的性能。

python
from sklearn.metrics import accuracy_score

创建测试数据加载器
test_data = TensorDataset(input_ids_test, attention_masks_test, labels_test)
test_loader = DataLoader(test_data, batch_size=32)

评估模型
model.eval()
predictions, true_labels = [], []
with torch.no_grad():
for batch in test_loader:
input_ids, attention_mask, labels = batch
outputs = model(input_ids, attention_mask)
_, predicted = torch.max(outputs, 1)
predictions.extend(predicted.numpy())
true_labels.extend(labels.numpy())

计算准确率
accuracy = accuracy_score(true_labels, predictions)
print(f'Test Accuracy: {accuracy}')

总结

本文介绍了如何使用PyTorch和BERT模型实现中文情感分析。通过数据预处理、模型构建、训练和评估等步骤,我们成功构建了一个基于BERT的情感分析模型。在实际应用中,您可以根据需要调整模型参数、优化训练过程,以提高模型的性能。