请给我一段Pytorch的LSTM模型对自然语言处理的实际代码
时间: 2023-05-27 15:06:57 浏览: 150
以下是一个使用Pytorch实现LSTM模型进行情感分析的代码示例:
```
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torchtext.data import Field, TabularDataset, BucketIterator
# 定义Field
TEXT = Field(tokenize='spacy', tokenizer_language='en_core_web_sm', include_lengths=True)
LABEL = Field(sequential=False, use_vocab=False)
# 加载数据集
train, test = TabularDataset.splits(path='./data', train='train.csv', test='test.csv', format='csv', fields=[('text', TEXT), ('label', LABEL)], skip_header=True)
# 构建词汇表
TEXT.build_vocab(train)
vocab_size = len(TEXT.vocab)
# 定义LSTM模型
class LSTM(nn.Module):
def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim, n_layers, bidirectional, dropout):
super().__init__()
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers=n_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, text_lengths):
embedded = self.embedding(text)
packed_embedded = nn.utils.rnn.pack_padded_sequence(embedded, text_lengths)
packed_output, (hidden, cell) = self.lstm(packed_embedded)
hidden = self.dropout(torch.cat((hidden[-2,:,:], hidden[-1,:,:]), dim=1))
output = self.fc(hidden)
return output
# 初始化模型
EMBEDDING_DIM = 100
HIDDEN_DIM = 256
OUTPUT_DIM = 1
N_LAYERS = 2
BIDIRECTIONAL = True
DROPOUT = 0.5
model = LSTM(vocab_size, EMBEDDING_DIM, HIDDEN_DIM, OUTPUT_DIM, N_LAYERS, BIDIRECTIONAL, DROPOUT)
# 定义优化器和损失函数
optimizer = optim.Adam(model.parameters())
criterion = nn.BCEWithLogitsLoss()
# 将数据集划分为batch并进行训练
BATCH_SIZE = 64
train_iterator, test_iterator = BucketIterator.splits((train, test), batch_size=BATCH_SIZE, sort_within_batch=True, sort_key=lambda x: len(x.text), device=torch.device('cuda' if torch.cuda.is_available() else 'cpu'))
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)
criterion = criterion.to(device)
def train(model, iterator, optimizer, criterion):
model.train()
epoch_loss = 0
epoch_acc = 0
for batch in iterator:
text, text_lengths = batch.text
text = text.to(device)
text_lengths = text_lengths.to(device)
labels = batch.label.to(device)
optimizer.zero_grad()
predictions = model(text, text_lengths).squeeze(1)
loss = criterion(predictions, labels.float())
acc = binary_accuracy(predictions, labels)
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):
model.eval()
epoch_loss = 0
epoch_acc = 0
with torch.no_grad():
for batch in iterator:
text, text_lengths = batch.text
text = text.to(device)
text_lengths = text_lengths.to(device)
labels = batch.label.to(device)
predictions = model(text, text_lengths).squeeze(1)
loss = criterion(predictions, labels.float())
acc = binary_accuracy(predictions, labels)
epoch_loss += loss.item()
epoch_acc += acc.item()
return epoch_loss / len(iterator), epoch_acc / len(iterator)
def binary_accuracy(preds, y):
rounded_preds = torch.round(torch.sigmoid(preds))
correct = (rounded_preds == y).float()
acc = correct.sum() / len(correct)
return acc
# 训练模型
N_EPOCHS = 10
for epoch in range(N_EPOCHS):
train_loss, train_acc = train(model, train_iterator, optimizer, criterion)
test_loss, test_acc = evaluate(model, test_iterator, criterion)
print(f'Epoch: {epoch+1:02}')
print(f'\tTrain Loss: {train_loss:.3f} | Train Acc: {train_acc*100:.2f}%')
print(f'\tTest Loss: {test_loss:.3f} | Test Acc: {test_acc*100:.2f}%')
```
此代码实现了一个使用LSTM模型对情感分析数据集进行训练和测试的过程。在代码中,首先定义了Field来指定数据集的处理方式,然后使用TabularDataset加载数据集并构建词汇表。接着定义了LSTM模型,包括嵌入层、LSTM层、全连接层和dropout层。然后定义了优化器和损失函数,并将数据集划分为batch进行训练。在训练过程中,使用train函数来训练模型并计算损失和准确率,并使用evaluate函数来测试模型并计算损失和准确率。最后,训练模型并输出结果。
阅读全文