使用pytorch的lstm对数据集aclImdb进行预测
时间: 2024-01-21 09:04:05 浏览: 225
基于pytorch LSTM 的股票预测
以下是使用PyTorch的LSTM对数据集aclImdb进行预测的示例代码:
```
import torch
import torch.nn as nn
import torch.optim as optim
from torchtext.datasets import IMDB
from torchtext.data import Field, LabelField, BucketIterator
# 定义字段
text_field = Field(tokenize='spacy', lower=True)
label_field = LabelField(dtype=torch.float)
# 加载数据集
train_data, test_data = IMDB.splits(text_field, label_field)
# 构建词汇表
text_field.build_vocab(train_data, max_size=10000, vectors='glove.6B.100d')
label_field.build_vocab(train_data)
# 定义模型
class LSTMClassifier(nn.Module):
def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim, num_layers, bidirectional, dropout):
super().__init__()
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers=num_layers, bidirectional=bidirectional, dropout=dropout)
self.fc = nn.Linear(hidden_dim * 2 if bidirectional else hidden_dim, output_dim)
self.dropout = nn.Dropout(dropout)
def forward(self, text):
embedded = self.dropout(self.embedding(text))
output, (hidden, cell) = self.lstm(embedded)
hidden = self.dropout(torch.cat((hidden[-2,:,:], hidden[-1,:,:]), dim=1))
return self.fc(hidden)
# 定义超参数
vocab_size = len(text_field.vocab)
embedding_dim = 100
hidden_dim = 256
output_dim = 1
num_layers = 2
bidirectional = True
dropout = 0.5
# 初始化模型
model = LSTMClassifier(vocab_size, embedding_dim, hidden_dim, output_dim, num_layers, bidirectional, dropout)
# 定义损失函数和优化器
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters())
# 将数据集分成batch
BATCH_SIZE = 64
train_iterator, test_iterator = BucketIterator.splits((train_data, test_data), batch_size=BATCH_SIZE)
# 训练模型
NUM_EPOCHS = 10
for epoch in range(NUM_EPOCHS):
for batch in train_iterator:
text = batch.text
labels = batch.label
optimizer.zero_grad()
predictions = model(text).squeeze(1)
loss = criterion(predictions, labels)
loss.backward()
optimizer.step()
# 在测试集上测试模型
with torch.no_grad():
correct = 0
total = 0
for batch in test_iterator:
text = batch.text
labels = batch.label
predictions = torch.round(torch.sigmoid(model(text))).squeeze(1)
correct += (predictions == labels).sum().item()
total += labels.size(0)
accuracy = correct / total
print('Epoch: %d, Test Accuracy: %.3f' % (epoch+1, accuracy))
```
在这个例子中,我们首先定义了两个字段,一个用于文本数据,另一个用于标签数据。然后,我们加载IMDB数据集并构建词汇表。接下来,我们定义了一个LSTM分类器模型,并初始化了超参数、损失函数和优化器。最后,我们将数据集分成batch并训练模型。
在每个epoch结束后,我们在测试集上测试模型,并输出测试精度。在这个例子中,我们使用了BCEWithLogitsLoss作为损失函数,并使用Adam作为优化器。我们也使用了dropout来避免过拟合。
阅读全文