Pytorch实现bidirectional_recurrent_neural_network

Bidirectional_recurrent_neural_network

# Import necessary packages.
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')
# print(device)   # cuda
# print(torch.cuda.get_device_name()) # NVIDIA GeForce GTX 1650 
# print(torch.cuda.get_device_capability())   # (7, 5)
# Hyper-parameters.
sequence_length = 28
input_size = 28
hidden_size = 128
num_layers = 2
num_classes = 10
batch_size = 100
num_epochs = 5
learning_rate = 0.003
# Load the 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())
# Define the dataloader.
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 status.
        h0 = torch.zeros(self.num_layers*2, x.size(0), self.hidden_size).to(device)
        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)
# Define Loss and optimizer.
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Train the model.
total_step = len(train_dataset)
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()

        # Set an output conunter.
        if (i+1) % 100 == 0:
            print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
                  .format(epoch+1, num_epochs, i+1, total_step, loss.item()))
Epoch [1/5], Step [100/60000], Loss: 0.7600
Epoch [1/5], Step [200/60000], Loss: 0.3808
Epoch [1/5], Step [300/60000], Loss: 0.1809
Epoch [1/5], Step [400/60000], Loss: 0.2003
Epoch [1/5], Step [500/60000], Loss: 0.1400
Epoch [1/5], Step [600/60000], Loss: 0.0573
Epoch [2/5], Step [100/60000], Loss: 0.1896
Epoch [2/5], Step [200/60000], Loss: 0.0918
Epoch [2/5], Step [300/60000], Loss: 0.0184
Epoch [2/5], Step [400/60000], Loss: 0.0292
Epoch [2/5], Step [500/60000], Loss: 0.1486
Epoch [2/5], Step [600/60000], Loss: 0.0494
Epoch [3/5], Step [100/60000], Loss: 0.0355
Epoch [3/5], Step [200/60000], Loss: 0.0233
Epoch [3/5], Step [300/60000], Loss: 0.0608
Epoch [3/5], Step [400/60000], Loss: 0.0590
Epoch [3/5], Step [500/60000], Loss: 0.0765
Epoch [3/5], Step [600/60000], Loss: 0.0127
Epoch [4/5], Step [100/60000], Loss: 0.0712
Epoch [4/5], Step [200/60000], Loss: 0.0620
Epoch [4/5], Step [300/60000], Loss: 0.0265
Epoch [4/5], Step [400/60000], Loss: 0.0269
Epoch [4/5], Step [500/60000], Loss: 0.0236
Epoch [4/5], Step [600/60000], Loss: 0.0022
Epoch [5/5], Step [100/60000], Loss: 0.0162
Epoch [5/5], Step [200/60000], Loss: 0.0763
Epoch [5/5], Step [300/60000], Loss: 0.0087
Epoch [5/5], Step [400/60000], Loss: 0.0111
Epoch [5/5], Step [500/60000], Loss: 0.0914
Epoch [5/5], Step [600/60000], Loss: 0.0205
# Test the model.
model.eval()
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))
Test Accuracy of the model on the 10000 test images: 98.59
# Save the model checkpoint.
torch.save(model.state_dict(), 'model_param.ckpt')
# Alternative method to save the hole model.
torch.save(model, 'model.ckpt')

# Load the model checkpoint.
model.load_state_dict(torch.load('model_param.ckpt'))
# Alternative method to load the hole model.
torch.load('model.ckpt')
BiRNN(
  (lstm): LSTM(28, 128, num_layers=2, batch_first=True, bidirectional=True)
  (fc): Linear(in_features=256, out_features=10, bias=True)
)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值