以下是PyTorch如何使用batch训练LSTM网络的完整攻略,包含两个示例说明。
环境要求
在开始实战操作之前,需要确保您的系统满足以下要求:
- Python 3.6或更高版本
- PyTorch 1.0或更高版本
示例1:使用batch训练LSTM网络进行文本分类
在这个示例中,我们将使用batch训练LSTM网络进行文本分类。
首先,我们需要准备数据。我们将使用torchtext库来加载IMDB电影评论数据集。您可以使用以下代码来加载数据集:
import torchtext
from torchtext.datasets import IMDB
from torchtext.data import Field, LabelField, BucketIterator
TEXT = Field(tokenize='spacy', batch_first=True)
LABEL = LabelField(dtype=torch.float)
train_data, test_data = IMDB.splits(TEXT, LABEL)
TEXT.build_vocab(train_data, max_size=10000, vectors="glove.6B.100d")
LABEL.build_vocab(train_data)
train_loader, test_loader = BucketIterator.splits(
(train_data, test_data),
batch_size=32,
device='cuda',
sort_within_batch=True,
sort_key=lambda x: len(x.text),
repeat=False
)
然后,我们可以使用以下代码来定义一个LSTM网络:
import torch.nn as nn
class LSTMClassifier(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.dropout(self.embedding(text))
packed_embedded = nn.utils.rnn.pack_padded_sequence(embedded, text_lengths.cpu(), batch_first=True)
packed_output, (hidden, cell) = self.lstm(packed_embedded)
output, output_lengths = nn.utils.rnn.pad_packed_sequence(packed_output, batch_first=True)
hidden = self.dropout(torch.cat((hidden[-2,:,:], hidden[-1,:,:]), dim=1))
return self.fc(hidden)
接下来,我们可以使用以下代码来训练LSTM网络:
import torch.optim as optim
model = LSTMClassifier(len(TEXT.vocab), 100, 256, 1, 2, True, 0.5)
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters())
num_epochs = 10
for epoch in range(num_epochs):
for i, batch in enumerate(train_loader):
text, text_lengths = batch.text
labels = batch.label
optimizer.zero_grad()
predictions = model(text, text_lengths).squeeze(1)
loss = criterion(predictions, labels)
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, i+1, len(train_data)//32, loss.item()))
correct = 0
total = 0
with torch.no_grad():
for batch in test_loader:
text, text_lengths = batch.text
labels = batch.label
predictions = model(text, text_lengths).squeeze(1)
predicted = torch.round(torch.sigmoid(predictions))
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the model on the test set: {} %'.format(100 * correct / total))
在这个示例中,我们首先加载IMDB电影评论数据集,并使用torchtext库来进行数据预处理。然后,我们定义了一个LSTM网络,并使用该网络对IMDB电影评论数据集进行训练和测试。
示例2:使用batch训练LSTM网络进行时间序列预测
在这个示例中,我们将使用batch训练LSTM网络进行时间序列预测。
首先,我们需要准备数据。我们将使用sin函数生成时间序列数据。您可以使用以下代码来生成数据:
import numpy as np
def generate_data(n_samples, seq_length):
X = np.zeros((n_samples, seq_length, 1))
y = np.zeros((n_samples, 1))
for i in range(n_samples):
start = np.random.uniform(0, 2*np.pi)
seq = np.sin(np.linspace(start, start+10*np.pi, seq_length+1))[:-1, np.newaxis]
X[i,:,:] = seq
y[i,:] = np.sin(start+10*np.pi)
return X, y
X_train, y_train = generate_data(1000, 50)
X_test, y_test = generate_data(100, 50)
然后,我们可以使用以下代码来定义一个LSTM网络:
class LSTMRegressor(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, n_layers, dropout):
super().__init__()
self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers=n_layers, dropout=dropout)
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
output, (hidden, cell) = self.lstm(x)
return self.fc(hidden[-1,:,:])
接下来,我们可以使用以下代码来训练LSTM网络:
model = LSTMRegressor(1, 64, 1, 2, 0.5)
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters())
num_epochs = 10
for epoch in range(num_epochs):
for i in range(0, len(X_train), 32):
batch_X = torch.tensor(X_train[i:i+32], dtype=torch.float32)
batch_y = torch.tensor(y_train[i:i+32], dtype=torch.float32)
optimizer.zero_grad()
predictions = model(batch_X)
loss = criterion(predictions, batch_y)
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, i+1, len(X_train), loss.item()))
with torch.no_grad():
test_X = torch.tensor(X_test, dtype=torch.float32)
test_y = torch.tensor(y_test, dtype=torch.float32)
predictions = model(test_X)
loss = criterion(predictions, test_y)
print('Epoch [{}/{}], Test Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))
在这个示例中,我们首先生成了sin函数的时间序列数据。然后,我们定义了一个LSTM网络,并使用该网络对时间序列数据进行训练和测试。
总之,通过本文提供的攻略,您可以轻松地使用batch训练LSTM网络进行文本分类和时间序列预测。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:pytorch 如何使用batch训练lstm网络 - Python技术站