给我一份使用pytorch搭建分类模型实现情感分析的代码
以下是一个简单的使用PyTorch搭建分类模型,实现情感分析的代码:
import torch
import torch.nn as nn
import torch.optim as optim
from torchtext.datasets import IMDB
from torchtext.data import Field, LabelField, BucketIterator
# 定义文本处理的Field
TEXT = Field(sequential=True, lower=True, tokenize='spacy')
LABEL = LabelField()
# 加载IMDB数据集
train_data, test_data = IMDB.splits(TEXT, LABEL)
# 构建词汇表
TEXT.build_vocab(train_data, max_size=10000)
LABEL.build_vocab(train_data)
# 定义模型
class SentimentClassifier(nn.Module):
def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim):
super().__init__()
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.rnn = nn.RNN(embedding_dim, hidden_dim, batch_first=True)
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, text):
embedded = self.embedding(text)
output, hidden = self.rnn(embedded)
last_hidden = hidden[-1]
return self.fc(last_hidden)
# 设置模型参数
vocab_size = len(TEXT.vocab)
embedding_dim = 100
hidden_dim = 256
output_dim = 1
# 初始化模型和优化器
model = SentimentClassifier(vocab_size, embedding_dim, hidden_dim, output_dim)
optimizer = optim.Adam(model.parameters())
# 定义损失函数
criterion = nn.BCEWithLogitsLoss()
# 将数据集划分为小批量进行训练
train_iterator, test_iterator = BucketIterator.splits(
datasets=(train_data, test_data),
batch_size=64,
device=torch.device('cuda' if torch.cuda.is_available() else 'cpu'))
# 训练模型
model.train()
for epoch in range(10):
epoch_loss = 0
for batch in train_iterator:
optimizer.zero_grad()
text, label = batch.text, batch.label
predictions = model(text).squeeze(1)
loss = criterion(predictions, label.float())
loss.backward()
optimizer.step()
epoch_loss += loss.item()
print(f'Epoch {epoch+1} Loss {epoch_loss/len(train_iterator):.4f}')
# 测试模型
model.eval()
correct = 0
total = 0
with torch.no_grad():
for batch in test_iterator:
text, label = batch.text, batch.label
predictions = model(text).squeeze(1)
rounded_predictions = torch.round(torch.sigmoid(predictions))
correct += (rounded_predictions == label.float()).sum().item()
total += len(label)
print(f'Test Accuracy: {correct/total:.4f}')
这个代码使用了PyTorch的RNN模块,对输入的句子进行情感分析。在训练过程中,使用Adam优化器进行参数更新,使用BCEWithLogitsLoss作为损失函数。在测试过程中,使用准确率作为评估指标
原文地址: https://www.cveoy.top/t/topic/hpBH 著作权归作者所有。请勿转载和采集!