1 模型描述
- 双向循环神经网络的特点是,当前时刻的输出不仅和之前的状态有关,还可能和未来的状态有关系,也就是同一层节点之间的信息是双向流动的
- 与一般的循环神经网络相比,在代码上改动不多,调用的函数仍是nn.LSTM,只是函数参数bidirectional设置为True,而隐层分成forward layer和backward layer,分别进行两个不同时间方向的信息传递,因此,隐层节点数翻倍
- 解决的问题是MNIST手写数字分类
2 具体代码
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
# Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Hyper-parameters
sequence_length = 28
input_size = 28
hidden_size = 128
num_layers = 2
num_classes = 10
batch_size = 100
num_epochs = 2
learning_rate = 0.003
# MNIST dataset
train_dataset = torchvision.datasets.MNIST(
root='../../data/',
train=True,
transform=transforms.ToTensor(),
download=True)
test_dataset = torchvision.datasets.MNIST(
root='../../data/',
train=False,
transform=transforms.ToTensor())
# Data loader
train_loader = torch.utils.data.DataLoader(
dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(
dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
# Bidirectional recurrent neural network (many-to-one)
class BiRNN(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, num_classes):
super(BiRNN, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True, bidirectional=True)
self.fc = nn.Linear(hidden_size*2, num_classes) # 2 for bidirection
def forward(self, x):
# Set initial states
h0 = torch.zeros(self.num_layers*2, x.size(0), self.hidden_size).to(device) # 2 for bidirection
c0 = torch.zeros(self.num_layers*2, x.size(0), self.hidden_size).to(device)
# Forward propagate LSTM
out, _ = self.lstm(x, (h0, c0)) # out: tensor of shape (batch_size, seq_length, hidden_size*2)
# Decode the hidden state of the last time step
out = self.fc(out[:, -1, :])
return out
model = BiRNN(input_size, hidden_size, num_layers, num_classes).to(device)
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Train the model
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images = images.reshape(-1, sequence_length, input_size).to(device)
labels = labels.to(device)
# Forward pass
outputs = model(images)
loss = criterion(outputs, labels)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
# Test the model
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images = images.reshape(-1, sequence_length, input_size).to(device)
labels = labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Test Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total))
# Save the model checkpoint
torch.save(model.state_dict(), 'model.ckpt')
3 模型输出
相比一般的RNN,双向RNN的效果还是提升了一点点的。
Epoch [1/2], Step [100/600], Loss: 0.5850
Epoch [1/2], Step [200/600], Loss: 0.1650
Epoch [1/2], Step [300/600], Loss: 0.2225
Epoch [1/2], Step [400/600], Loss: 0.1315
Epoch [1/2], Step [500/600], Loss: 0.1194
Epoch [1/2], Step [600/600], Loss: 0.0682
Epoch [2/2], Step [100/600], Loss: 0.0674
Epoch [2/2], Step [200/600], Loss: 0.0429
Epoch [2/2], Step [300/600], Loss: 0.0268
Epoch [2/2], Step [400/600], Loss: 0.1437
Epoch [2/2], Step [500/600], Loss: 0.0601
Epoch [2/2], Step [600/600], Loss: 0.1223
Test Accuracy of the model on the 10000 test images: 97.82 %