用pytorch写一个情感分析的代码
时间: 2024-10-29 08:17:35 浏览: 5
在PyTorch中实现情感分析通常涉及几个步骤:数据预处理、构建神经网络模型、训练和评估。这里是一个简单的例子,展示如何使用预训练的BERT模型进行情感分析:
```python
import torch
from transformers import BertTokenizerFast, BertForSequenceClassification
# 加载预处理好的数据集(假设已经包含文本和对应的情感标签)
from dataset import SentimentDataset # 自定义的数据集类
tokenizer = BertTokenizerFast.from_pretrained('bert-base-chinese')
train_dataset = SentimentDataset(tokenizer, 'train.csv', label_column='sentiment')
test_dataset = SentimentDataset(tokenizer, 'test.csv', label_column='sentiment')
# 准备数据加载器
batch_size = 16
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size)
# 初始化BertForSequenceClassification模型
num_labels = len(train_dataset.label2id) # 确定情感类别数
model = BertForSequenceClassification.from_pretrained('bert-base-chinese', num_labels=num_labels)
# 定义优化器和损失函数
optimizer = torch.optim.AdamW(model.parameters(), lr=2e-5)
loss_fn = torch.nn.CrossEntropyLoss()
# 训练循环
epochs = 4
for epoch in range(epochs):
for batch in train_loader:
input_ids, attention_mask, labels = batch
outputs = model(input_ids=input_ids, attention_mask=attention_mask, labels=labels)
loss = outputs.loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
# 评估模型
model.eval()
total_accuracy = 0.
with torch.no_grad():
for batch in test_loader:
*_, preds = model(**batch)
total_accuracy += (preds.argmax(dim=-1) == batch[-1]).float().mean()
print(f"Test accuracy after {epochs} epochs: {total_accuracy / len(test_loader)}")
# 相关问题--
1. 想了解更复杂的模型结构可以怎么做?
2. 怎样调整超参数提升情感分析精度?
3. 这段代码中的CrossEntropyLoss是什么含义?
阅读全文