• PyTorch训练RNN, GRU, LSTM:手写数字识别


    pytorch 神经网络训练demo

    数据集:MNIST

    该数据集的内容是手写数字识别,其分为两部分,分别含有60000张训练图片和10000张测试图片

    在这里插入图片描述
    图片来源:https://tensornews.cn/mnist_intro/

    神经网络:RNN, GRU, LSTM

    # Imports
    import torch
    import torch.nn as nn
    import torch.optim as optim
    import torch.nn.functional as F
    from torch.utils.data import DataLoader
    import torchvision.datasets as datasets
    import torchvision.transforms as transforms
    
    # Set device
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    
    # Hyperparameters
    input_size = 28
    sequence_length = 28
    num_layers = 2
    hidden_size = 256
    num_classes = 10
    learning_rate = 0.001
    batch_size = 64
    num_epochs = 2
    
    # Create a RNN
    class RNN(nn.Module):
        def __init__(self, input_size, hidden_size, num_layers, num_classes):
            super(RNN, self).__init__()
            self.hidden_size = hidden_size
            self.num_layers = num_layers
            self.rnn = nn.RNN(input_size, hidden_size, num_layers, batch_first=True)
            self.fc = nn.Linear(hidden_size*sequence_length, num_classes) # fully connected
        
        def forward(self, x):
            h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
    
            # Forward Prop
            out, _ = self.rnn(x, h0)
            out = out.reshape(out.shape[0], -1)
            out = self.fc(out)
    
            return out
        
    # Create a GRU
    class RNN_GRU(nn.Module):
        def __init__(self, input_size, hidden_size, num_layers, num_classes):
            super(RNN_GRU, self).__init__()
            self.hidden_size = hidden_size
            self.num_layers = num_layers
            self.gru = nn.GRU(input_size, hidden_size, num_layers, batch_first=True)
            self.fc = nn.Linear(hidden_size*sequence_length, num_classes) # fully connected
        
        def forward(self, x):
            h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
    
            # Forward Prop
            out, _ = self.gru(x, h0)
            out = out.reshape(out.shape[0], -1)
            out = self.fc(out)
    
            return out
    
    
    # Create a LSTM
    class RNN_LSTM(nn.Module):
        def __init__(self, input_size, hidden_size, num_layers, num_classes):
            super(RNN_LSTM, 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)
            self.fc = nn.Linear(hidden_size*sequence_length, num_classes) # fully connected
        
        def forward(self, x):
            h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
            c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
    
            # Forward Prop
            out, _ = self.lstm(x, (h0, c0))
            out = out.reshape(out.shape[0], -1)
            out = self.fc(out)
            return out
        
    
    # Load data
    train_dataset = datasets.MNIST(root='dataset/', 
                                   train=True, 
                                   transform=transforms.ToTensor(),
                                   download=True)
    train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
    test_dataset = datasets.MNIST(root='dataset/', 
                                  train=False, 
                                   transform=transforms.ToTensor(),
                                   download=True)
    test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=True)
    
    # Initialize network 选择一个即可
    model = RNN(input_size, hidden_size, num_layers, num_classes).to(device)
    # model = RNN_GRU(input_size, hidden_size, num_layers, num_classes).to(device)
    # model = RNN_LSTM(input_size, hidden_size, num_layers, num_classes).to(device)
    
    # Loss and optimizer
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters(), lr=learning_rate)
    
    # Train network
    for epoch in range(num_epochs):
        # data: images, targets: labels
        for batch_idx, (data, targets) in enumerate(train_loader):
            # Get data to cuda if possible
            data = data.to(device).squeeze(1) # 删除一个张量中所有维数为1的维度 (N, 1, 28, 28) -> (N, 28, 28)
            targets = targets.to(device)
    
            # forward
            scores = model(data) # 64*10
            loss = criterion(scores, targets)
    
            # backward
            optimizer.zero_grad()
            loss.backward()
    
            # gradient descent or adam step
            optimizer.step()
    
    
    # Check accuracy on training & test to see how good our model
    def check_accuracy(loader, model):
        if loader.dataset.train:
            print("Checking accuracy on training data")
        else:
            print("Checking accuracy on test data")
        num_correct = 0
        num_samples = 0
        model.eval()
    
        with torch.no_grad(): # 不计算梯度
            for x, y in loader:
                x = x.to(device).squeeze(1)
                y = y.to(device)
                # x = x.reshape(x.shape[0], -1) # 64*784
    
                scores = model(x)# 64*10
                _, predictions = scores.max(dim=1) #dim=1,表示对每行取最大值,每行代表一个样本。
                num_correct += (predictions == y).sum()
                num_samples += predictions.size(0) # 64
    
            print(f'Got {num_correct} / {num_samples} with accuracy {float(num_correct)/float(num_samples)*100:.2f}%')
    
        model.train()
    
    check_accuracy(train_loader, model)
    check_accuracy(test_loader, model)
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151

    Result

    RNN Result
    Checking accuracy on training data
    Got 57926 / 60000 with accuracy 96.54%
    Checking accuracy on test data
    Got 9640 / 10000 with accuracy 96.40%
    
    
    GRU Result
    Checking accuracy on training data
    Got 59058 / 60000 with accuracy 98.43%
    Checking accuracy on test data
    Got 9841 / 10000 with accuracy 98.41%
    
    LSTM Result
    Checking accuracy on training data
    Got 59248 / 60000 with accuracy 98.75%
    Checking accuracy on test data
    Got 9849 / 10000 with accuracy 98.49%
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    参考来源

    【1】https://www.youtube.com/watch?v=Gl2WXLIMvKA&list=PLhhyoLH6IjfxeoooqP9rhU3HJIAVAJ3Vz&index=5

  • 相关阅读:
    (三)线性判别式分析LDA
    gunicorn 支持如下4种工作模式,Gunicorn“绿色独角兽”
    日均 6000+ 实例,TB 级数据流量,Apache DolphinScheduler 如何做联通医疗大数据平台的“顶梁柱”?...
    python调用subprocess模块实现命令行操作控制SVN
    Tyvj p1013 找啊找啊找GF
    css中新型的边框设置属性border-inline
    KWin、libdrm、DRM从上到下全过程 —— drmModeAddFBxxx(20)
    Kerberos认证
    生产线平衡分析常用工具大盘点!
    第五章第三节:数和二叉树的应用(二叉排序树和哈夫曼树及哈夫曼编码)
  • 原文地址:https://blog.csdn.net/shizheng_Li/article/details/131684837