deep_residual_network

Deep_Residual_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
# Hyper-parameters setting.
num_epochs = 80
batch_size = 100
learning_rate = 0.001
# Image preprocessing modules.
transform = transforms.Compose([
    transforms.Pad(4),
    transforms.RandomHorizontalFlip(),
    transforms.RandomCrop(32),
    transforms.ToTensor()])
# Load The CIFAR-10 dataset.
train_dataset = torchvision.datasets.CIFAR10(root='../../data/',
                                             train=True,
                                             transform=transform,
                                            download=True)

test_dataset = torchvision.datasets.CIFAR10(root='../../data/',
                                            train=False,
                                            transform=transforms.ToTensor(),
                                            download=False)                                             
Files already downloaded and verified
# Define 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)
# 3x3 convolution.
def conv3x3(in_channels, out_channels, stride=1):
    return nn.Conv2d(in_channels, out_channels, kernel_size=3,
                     stride=stride, padding=1, bias=False)
# Residual block.
class ResidualBlock(nn.Module):
    def __init__(self, in_channels, out_channels, stride=1, downsample=None):
        super(ResidualBlock, self).__init__()
        self.conv1 = conv3x3(in_channels, out_channels, stride)
        self.bn1 = nn.BatchNorm2d(out_channels)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(out_channels, out_channels)
        self.bn2 = nn.BatchNorm2d(out_channels)
        self.downsample = downsample
    
    def forward(self, x):
        residual = x
        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)
        out = self.conv2(out)
        out = self.bn2(out)
        if self.downsample:
            residual = self.downsample(x)
        out += residual
        out = self.relu(out)
        return out
# ResNet.
class ResNet(nn.Module):
    def __init__(self, block, layers, num_classes=10):
        super(ResNet, self).__init__()
        self.in_channels = 16
        self.conv = conv3x3(3, 16)
        self.bn = nn.BatchNorm2d(16)
        self.relu = nn.ReLU(inplace=True)
        self.layer1 = self.make_layer(block, 16, layers[0])
        self.layer2 = self.make_layer(block, 32, layers[1], 2)
        self.layer3 = self.make_layer(block, 64, layers[2], 2)
        self.avg_pool = nn.AvgPool2d(8)
        self.fc = nn.Linear(64, num_classes)

    def make_layer(self, block, out_channels, blocks, stride=1):
        downsample = None
        if (stride != 1) or (self.in_channels != out_channels):
            downsample = nn.Sequential(
                conv3x3(self.in_channels, out_channels, stride=stride),
                nn.BatchNorm2d(out_channels))
        layers = []
        layers.append(block(self.in_channels, out_channels, stride, downsample))
        self.in_channels = out_channels
        for i in range(1, blocks):
            layers.append(block(out_channels, out_channels))
        return nn.Sequential(*layers)
    
    def forward(self, x):
        out = self.conv(x)
        out = self.bn(out)
        out = self.relu(out)
        out = self.layer1(out)
        out = self.layer2(out)
        out = self.layer3(out)
        out = self.avg_pool(out)
        out = out.view((out.size(0)), -1)
        out = self.fc(out)
        return out

model = ResNet(ResidualBlock, [2, 2, 2]).to(device)
# Loss and optimizer.
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# For updating learning rate.
def update_lr(optimizer, lr):
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr
# Train the model
total_step = len(train_loader)
curr_lr = learning_rate
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        images = images.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()))
        
        # Decay learning rate.
        if (epoch+1) % 20 ==0:
            curr_lr /= 3
            update_lr(optimizer, curr_lr)
Epoch [1/80], Step [100/500], Loss: 1.6813
Epoch [1/80], Step [200/500], Loss: 1.3309
Epoch [1/80], Step [300/500], Loss: 1.3600
Epoch [1/80], Step [400/500], Loss: 1.3821
Epoch [1/80], Step [500/500], Loss: 1.2320
Epoch [2/80], Step [100/500], Loss: 0.9961
Epoch [2/80], Step [200/500], Loss: 1.0894
Epoch [2/80], Step [300/500], Loss: 0.8659
Epoch [2/80], Step [400/500], Loss: 1.0653
Epoch [2/80], Step [500/500], Loss: 0.8834
Epoch [3/80], Step [100/500], Loss: 0.9407
Epoch [3/80], Step [200/500], Loss: 0.9502
Epoch [3/80], Step [300/500], Loss: 0.9662
Epoch [3/80], Step [400/500], Loss: 0.7733
Epoch [3/80], Step [500/500], Loss: 0.6249
Epoch [4/80], Step [100/500], Loss: 0.7448
Epoch [4/80], Step [200/500], Loss: 0.6193
Epoch [4/80], Step [300/500], Loss: 0.6481
Epoch [4/80], Step [400/500], Loss: 0.8609
Epoch [4/80], Step [500/500], Loss: 0.8430
Epoch [5/80], Step [100/500], Loss: 0.8937
Epoch [5/80], Step [200/500], Loss: 0.6558
Epoch [5/80], Step [300/500], Loss: 0.6467
Epoch [5/80], Step [400/500], Loss: 0.8017
Epoch [5/80], Step [500/500], Loss: 0.6142
Epoch [6/80], Step [100/500], Loss: 0.5732
Epoch [6/80], Step [200/500], Loss: 0.6271
Epoch [6/80], Step [300/500], Loss: 0.6341
Epoch [6/80], Step [400/500], Loss: 0.6480
Epoch [6/80], Step [500/500], Loss: 0.6894
Epoch [7/80], Step [100/500], Loss: 0.7035
Epoch [7/80], Step [200/500], Loss: 0.7022
Epoch [7/80], Step [300/500], Loss: 0.4513
Epoch [7/80], Step [400/500], Loss: 0.5571
Epoch [7/80], Step [500/500], Loss: 0.6292
Epoch [8/80], Step [100/500], Loss: 0.4555
Epoch [8/80], Step [200/500], Loss: 0.6679
Epoch [8/80], Step [300/500], Loss: 0.5793
Epoch [8/80], Step [400/500], Loss: 0.5330
Epoch [8/80], Step [500/500], Loss: 0.4378
Epoch [9/80], Step [100/500], Loss: 0.4883
Epoch [9/80], Step [200/500], Loss: 0.5861
Epoch [9/80], Step [300/500], Loss: 0.4735
Epoch [9/80], Step [400/500], Loss: 0.5172
Epoch [9/80], Step [500/500], Loss: 0.4156
Epoch [10/80], Step [100/500], Loss: 0.4031
Epoch [10/80], Step [200/500], Loss: 0.6231
Epoch [10/80], Step [300/500], Loss: 0.4010
Epoch [10/80], Step [400/500], Loss: 0.4843
Epoch [10/80], Step [500/500], Loss: 0.6768
Epoch [11/80], Step [100/500], Loss: 0.4757
Epoch [11/80], Step [200/500], Loss: 0.5781
Epoch [11/80], Step [300/500], Loss: 0.4803
Epoch [11/80], Step [400/500], Loss: 0.4234
Epoch [11/80], Step [500/500], Loss: 0.5142
Epoch [12/80], Step [100/500], Loss: 0.5684
Epoch [12/80], Step [200/500], Loss: 0.3083
Epoch [12/80], Step [300/500], Loss: 0.4764
Epoch [12/80], Step [400/500], Loss: 0.4499
Epoch [12/80], Step [500/500], Loss: 0.5892
Epoch [13/80], Step [100/500], Loss: 0.3176
Epoch [13/80], Step [200/500], Loss: 0.3424
Epoch [13/80], Step [300/500], Loss: 0.5316
Epoch [13/80], Step [400/500], Loss: 0.3902
Epoch [13/80], Step [500/500], Loss: 0.4355
Epoch [14/80], Step [100/500], Loss: 0.3610
Epoch [14/80], Step [200/500], Loss: 0.3949
Epoch [14/80], Step [300/500], Loss: 0.3674
Epoch [14/80], Step [400/500], Loss: 0.5186
Epoch [14/80], Step [500/500], Loss: 0.3936
Epoch [15/80], Step [100/500], Loss: 0.3408
Epoch [15/80], Step [200/500], Loss: 0.4585
Epoch [15/80], Step [300/500], Loss: 0.4356
Epoch [15/80], Step [400/500], Loss: 0.3548
Epoch [15/80], Step [500/500], Loss: 0.4019
Epoch [16/80], Step [100/500], Loss: 0.4717
Epoch [16/80], Step [200/500], Loss: 0.4037
Epoch [16/80], Step [300/500], Loss: 0.4970
Epoch [16/80], Step [400/500], Loss: 0.4643
Epoch [16/80], Step [500/500], Loss: 0.4482
Epoch [17/80], Step [100/500], Loss: 0.4007
Epoch [17/80], Step [200/500], Loss: 0.5095
Epoch [17/80], Step [300/500], Loss: 0.2736
Epoch [17/80], Step [400/500], Loss: 0.4718
Epoch [17/80], Step [500/500], Loss: 0.3577
Epoch [18/80], Step [100/500], Loss: 0.4523
Epoch [18/80], Step [200/500], Loss: 0.3902
Epoch [18/80], Step [300/500], Loss: 0.2361
Epoch [18/80], Step [400/500], Loss: 0.2933
Epoch [18/80], Step [500/500], Loss: 0.2130
Epoch [19/80], Step [100/500], Loss: 0.3324
Epoch [19/80], Step [200/500], Loss: 0.3762
Epoch [19/80], Step [300/500], Loss: 0.2952
Epoch [19/80], Step [400/500], Loss: 0.3768
Epoch [19/80], Step [500/500], Loss: 0.2550
Epoch [20/80], Step [100/500], Loss: 0.4499
Epoch [20/80], Step [200/500], Loss: 0.4516
Epoch [20/80], Step [300/500], Loss: 0.3245
Epoch [20/80], Step [400/500], Loss: 0.3915
Epoch [20/80], Step [500/500], Loss: 0.2645
Epoch [21/80], Step [100/500], Loss: 0.2207
Epoch [21/80], Step [200/500], Loss: 0.4940
Epoch [21/80], Step [300/500], Loss: 0.4244
Epoch [21/80], Step [400/500], Loss: 0.4214
Epoch [21/80], Step [500/500], Loss: 0.2739
Epoch [22/80], Step [100/500], Loss: 0.2975
Epoch [22/80], Step [200/500], Loss: 0.2993
Epoch [22/80], Step [300/500], Loss: 0.3349
Epoch [22/80], Step [400/500], Loss: 0.4027
Epoch [22/80], Step [500/500], Loss: 0.4266
Epoch [23/80], Step [100/500], Loss: 0.3436
Epoch [23/80], Step [200/500], Loss: 0.3375
Epoch [23/80], Step [300/500], Loss: 0.3030
Epoch [23/80], Step [400/500], Loss: 0.3841
Epoch [23/80], Step [500/500], Loss: 0.4496
Epoch [24/80], Step [100/500], Loss: 0.5373
Epoch [24/80], Step [200/500], Loss: 0.2922
Epoch [24/80], Step [300/500], Loss: 0.4524
Epoch [24/80], Step [400/500], Loss: 0.2701
Epoch [24/80], Step [500/500], Loss: 0.3375
Epoch [25/80], Step [100/500], Loss: 0.4769
Epoch [25/80], Step [200/500], Loss: 0.3759
Epoch [25/80], Step [300/500], Loss: 0.4362
Epoch [25/80], Step [400/500], Loss: 0.3946
Epoch [25/80], Step [500/500], Loss: 0.4517
Epoch [26/80], Step [100/500], Loss: 0.3163
Epoch [26/80], Step [200/500], Loss: 0.2814
Epoch [26/80], Step [300/500], Loss: 0.2830
Epoch [26/80], Step [400/500], Loss: 0.3459
Epoch [26/80], Step [500/500], Loss: 0.3376
Epoch [27/80], Step [100/500], Loss: 0.5180
Epoch [27/80], Step [200/500], Loss: 0.3052
Epoch [27/80], Step [300/500], Loss: 0.2578
Epoch [27/80], Step [400/500], Loss: 0.4357
Epoch [27/80], Step [500/500], Loss: 0.3561
Epoch [28/80], Step [100/500], Loss: 0.2215
Epoch [28/80], Step [200/500], Loss: 0.4192
Epoch [28/80], Step [300/500], Loss: 0.3859
Epoch [28/80], Step [400/500], Loss: 0.2494
Epoch [28/80], Step [500/500], Loss: 0.3063
Epoch [29/80], Step [100/500], Loss: 0.2807
Epoch [29/80], Step [200/500], Loss: 0.3785
Epoch [29/80], Step [300/500], Loss: 0.2530
Epoch [29/80], Step [400/500], Loss: 0.2523
Epoch [29/80], Step [500/500], Loss: 0.2863
Epoch [30/80], Step [100/500], Loss: 0.3900
Epoch [30/80], Step [200/500], Loss: 0.4359
Epoch [30/80], Step [300/500], Loss: 0.4164
Epoch [30/80], Step [400/500], Loss: 0.3966
Epoch [30/80], Step [500/500], Loss: 0.4372
Epoch [31/80], Step [100/500], Loss: 0.3702
Epoch [31/80], Step [200/500], Loss: 0.4615
Epoch [31/80], Step [300/500], Loss: 0.3059
Epoch [31/80], Step [400/500], Loss: 0.3323
Epoch [31/80], Step [500/500], Loss: 0.2572
Epoch [32/80], Step [100/500], Loss: 0.2867
Epoch [32/80], Step [200/500], Loss: 0.6127
Epoch [32/80], Step [300/500], Loss: 0.2086
Epoch [32/80], Step [400/500], Loss: 0.2994
Epoch [32/80], Step [500/500], Loss: 0.3686
Epoch [33/80], Step [100/500], Loss: 0.5084
Epoch [33/80], Step [200/500], Loss: 0.2910
Epoch [33/80], Step [300/500], Loss: 0.5251
Epoch [33/80], Step [400/500], Loss: 0.3583
Epoch [33/80], Step [500/500], Loss: 0.2018
Epoch [34/80], Step [100/500], Loss: 0.3070
Epoch [34/80], Step [200/500], Loss: 0.4379
Epoch [34/80], Step [300/500], Loss: 0.3893
Epoch [34/80], Step [400/500], Loss: 0.3778
Epoch [34/80], Step [500/500], Loss: 0.4583
Epoch [35/80], Step [100/500], Loss: 0.3852
Epoch [35/80], Step [200/500], Loss: 0.2713
Epoch [35/80], Step [300/500], Loss: 0.5001
Epoch [35/80], Step [400/500], Loss: 0.2437
Epoch [35/80], Step [500/500], Loss: 0.3665
Epoch [36/80], Step [100/500], Loss: 0.4438
Epoch [36/80], Step [200/500], Loss: 0.2112
Epoch [36/80], Step [300/500], Loss: 0.3104
Epoch [36/80], Step [400/500], Loss: 0.3022
Epoch [36/80], Step [500/500], Loss: 0.2445
Epoch [37/80], Step [100/500], Loss: 0.3935
Epoch [37/80], Step [200/500], Loss: 0.3391
Epoch [37/80], Step [300/500], Loss: 0.5324
Epoch [37/80], Step [400/500], Loss: 0.4255
Epoch [37/80], Step [500/500], Loss: 0.3942
Epoch [38/80], Step [100/500], Loss: 0.2929
Epoch [38/80], Step [200/500], Loss: 0.3554
Epoch [38/80], Step [300/500], Loss: 0.2800
Epoch [38/80], Step [400/500], Loss: 0.3467
Epoch [38/80], Step [500/500], Loss: 0.3648
Epoch [39/80], Step [100/500], Loss: 0.2709
Epoch [39/80], Step [200/500], Loss: 0.2959
Epoch [39/80], Step [300/500], Loss: 0.2654
Epoch [39/80], Step [400/500], Loss: 0.3282
Epoch [39/80], Step [500/500], Loss: 0.3584
Epoch [40/80], Step [100/500], Loss: 0.3200
Epoch [40/80], Step [200/500], Loss: 0.4697
Epoch [40/80], Step [300/500], Loss: 0.5168
Epoch [40/80], Step [400/500], Loss: 0.3246
Epoch [40/80], Step [500/500], Loss: 0.4181
Epoch [41/80], Step [100/500], Loss: 0.5672
Epoch [41/80], Step [200/500], Loss: 0.3359
Epoch [41/80], Step [300/500], Loss: 0.4046
Epoch [41/80], Step [400/500], Loss: 0.2741
Epoch [41/80], Step [500/500], Loss: 0.3885
Epoch [42/80], Step [100/500], Loss: 0.3990
Epoch [42/80], Step [200/500], Loss: 0.3618
Epoch [42/80], Step [300/500], Loss: 0.2712
Epoch [42/80], Step [400/500], Loss: 0.3771
Epoch [42/80], Step [500/500], Loss: 0.3654
Epoch [43/80], Step [100/500], Loss: 0.4265
Epoch [43/80], Step [200/500], Loss: 0.3484
Epoch [43/80], Step [300/500], Loss: 0.2833
Epoch [43/80], Step [400/500], Loss: 0.3779
Epoch [43/80], Step [500/500], Loss: 0.2916
Epoch [44/80], Step [100/500], Loss: 0.4146
Epoch [44/80], Step [200/500], Loss: 0.3840
Epoch [44/80], Step [300/500], Loss: 0.3005
Epoch [44/80], Step [400/500], Loss: 0.3404
Epoch [44/80], Step [500/500], Loss: 0.4090
Epoch [45/80], Step [100/500], Loss: 0.4705
Epoch [45/80], Step [200/500], Loss: 0.4325
Epoch [45/80], Step [300/500], Loss: 0.4477
Epoch [45/80], Step [400/500], Loss: 0.3278
Epoch [45/80], Step [500/500], Loss: 0.5257
Epoch [46/80], Step [100/500], Loss: 0.2231
Epoch [46/80], Step [200/500], Loss: 0.3204
Epoch [46/80], Step [300/500], Loss: 0.4188
Epoch [46/80], Step [400/500], Loss: 0.3421
Epoch [46/80], Step [500/500], Loss: 0.2937
Epoch [47/80], Step [100/500], Loss: 0.4077
Epoch [47/80], Step [200/500], Loss: 0.4088
Epoch [47/80], Step [300/500], Loss: 0.4501
Epoch [47/80], Step [400/500], Loss: 0.3707
Epoch [47/80], Step [500/500], Loss: 0.2656
Epoch [48/80], Step [100/500], Loss: 0.2942
Epoch [48/80], Step [200/500], Loss: 0.3595
Epoch [48/80], Step [300/500], Loss: 0.3710
Epoch [48/80], Step [400/500], Loss: 0.3372
Epoch [48/80], Step [500/500], Loss: 0.5289
Epoch [49/80], Step [100/500], Loss: 0.3020
Epoch [49/80], Step [200/500], Loss: 0.3487
Epoch [49/80], Step [300/500], Loss: 0.4004
Epoch [49/80], Step [400/500], Loss: 0.4134
Epoch [49/80], Step [500/500], Loss: 0.4896
Epoch [50/80], Step [100/500], Loss: 0.4403
Epoch [50/80], Step [200/500], Loss: 0.4179
Epoch [50/80], Step [300/500], Loss: 0.2329
Epoch [50/80], Step [400/500], Loss: 0.3027
Epoch [50/80], Step [500/500], Loss: 0.4491
Epoch [51/80], Step [100/500], Loss: 0.4268
Epoch [51/80], Step [200/500], Loss: 0.3180
Epoch [51/80], Step [300/500], Loss: 0.2959
Epoch [51/80], Step [400/500], Loss: 0.2462
Epoch [51/80], Step [500/500], Loss: 0.3458
Epoch [52/80], Step [100/500], Loss: 0.3443
Epoch [52/80], Step [200/500], Loss: 0.2158
Epoch [52/80], Step [300/500], Loss: 0.2663
Epoch [52/80], Step [400/500], Loss: 0.3248
Epoch [52/80], Step [500/500], Loss: 0.3340
Epoch [53/80], Step [100/500], Loss: 0.4008
Epoch [53/80], Step [200/500], Loss: 0.4801
Epoch [53/80], Step [300/500], Loss: 0.4733
Epoch [53/80], Step [400/500], Loss: 0.4176
Epoch [53/80], Step [500/500], Loss: 0.3105
Epoch [54/80], Step [100/500], Loss: 0.3254
Epoch [54/80], Step [200/500], Loss: 0.3082
Epoch [54/80], Step [300/500], Loss: 0.2672
Epoch [54/80], Step [400/500], Loss: 0.3272
Epoch [54/80], Step [500/500], Loss: 0.3505
Epoch [55/80], Step [100/500], Loss: 0.3498
Epoch [55/80], Step [200/500], Loss: 0.4057
Epoch [55/80], Step [300/500], Loss: 0.3522
Epoch [55/80], Step [400/500], Loss: 0.4080
Epoch [55/80], Step [500/500], Loss: 0.5481
Epoch [56/80], Step [100/500], Loss: 0.3302
Epoch [56/80], Step [200/500], Loss: 0.4734
Epoch [56/80], Step [300/500], Loss: 0.3435
Epoch [56/80], Step [400/500], Loss: 0.3337
Epoch [56/80], Step [500/500], Loss: 0.4469
Epoch [57/80], Step [100/500], Loss: 0.3973
Epoch [57/80], Step [200/500], Loss: 0.2889
Epoch [57/80], Step [300/500], Loss: 0.2850
Epoch [57/80], Step [400/500], Loss: 0.3346
Epoch [57/80], Step [500/500], Loss: 0.2428
Epoch [58/80], Step [100/500], Loss: 0.3499
Epoch [58/80], Step [200/500], Loss: 0.3708
Epoch [58/80], Step [300/500], Loss: 0.3457
Epoch [58/80], Step [400/500], Loss: 0.3043
Epoch [58/80], Step [500/500], Loss: 0.2127
Epoch [59/80], Step [100/500], Loss: 0.4164
Epoch [59/80], Step [200/500], Loss: 0.5447
Epoch [59/80], Step [300/500], Loss: 0.3270
Epoch [59/80], Step [400/500], Loss: 0.2835
Epoch [59/80], Step [500/500], Loss: 0.3678
Epoch [60/80], Step [100/500], Loss: 0.5183
Epoch [60/80], Step [200/500], Loss: 0.3077
Epoch [60/80], Step [300/500], Loss: 0.3788
Epoch [60/80], Step [400/500], Loss: 0.5352
Epoch [60/80], Step [500/500], Loss: 0.2007
Epoch [61/80], Step [100/500], Loss: 0.3592
Epoch [61/80], Step [200/500], Loss: 0.3335
Epoch [61/80], Step [300/500], Loss: 0.3052
Epoch [61/80], Step [400/500], Loss: 0.3914
Epoch [61/80], Step [500/500], Loss: 0.3509
Epoch [62/80], Step [100/500], Loss: 0.5136
Epoch [62/80], Step [200/500], Loss: 0.3592
Epoch [62/80], Step [300/500], Loss: 0.2378
Epoch [62/80], Step [400/500], Loss: 0.2732
Epoch [62/80], Step [500/500], Loss: 0.3470
Epoch [63/80], Step [100/500], Loss: 0.3089
Epoch [63/80], Step [200/500], Loss: 0.3117
Epoch [63/80], Step [300/500], Loss: 0.4159
Epoch [63/80], Step [400/500], Loss: 0.4553
Epoch [63/80], Step [500/500], Loss: 0.3710
Epoch [64/80], Step [100/500], Loss: 0.4569
Epoch [64/80], Step [200/500], Loss: 0.4581
Epoch [64/80], Step [300/500], Loss: 0.3520
Epoch [64/80], Step [400/500], Loss: 0.3495
Epoch [64/80], Step [500/500], Loss: 0.3255
Epoch [65/80], Step [100/500], Loss: 0.2694
Epoch [65/80], Step [200/500], Loss: 0.2800
Epoch [65/80], Step [300/500], Loss: 0.2462
Epoch [65/80], Step [400/500], Loss: 0.2442
Epoch [65/80], Step [500/500], Loss: 0.3629
Epoch [66/80], Step [100/500], Loss: 0.4210
Epoch [66/80], Step [200/500], Loss: 0.3035
Epoch [66/80], Step [300/500], Loss: 0.5971
Epoch [66/80], Step [400/500], Loss: 0.3247
Epoch [66/80], Step [500/500], Loss: 0.4722
Epoch [67/80], Step [100/500], Loss: 0.3194
Epoch [67/80], Step [200/500], Loss: 0.3479
Epoch [67/80], Step [300/500], Loss: 0.4012
Epoch [67/80], Step [400/500], Loss: 0.3244
Epoch [67/80], Step [500/500], Loss: 0.3968
Epoch [68/80], Step [100/500], Loss: 0.4607
Epoch [68/80], Step [200/500], Loss: 0.2946
Epoch [68/80], Step [300/500], Loss: 0.5424
Epoch [68/80], Step [400/500], Loss: 0.3642
Epoch [68/80], Step [500/500], Loss: 0.3861
Epoch [69/80], Step [100/500], Loss: 0.4021
Epoch [69/80], Step [200/500], Loss: 0.3440
Epoch [69/80], Step [300/500], Loss: 0.2665
Epoch [69/80], Step [400/500], Loss: 0.3696
Epoch [69/80], Step [500/500], Loss: 0.4682
Epoch [70/80], Step [100/500], Loss: 0.3457
Epoch [70/80], Step [200/500], Loss: 0.3573
Epoch [70/80], Step [300/500], Loss: 0.2991
Epoch [70/80], Step [400/500], Loss: 0.4386
Epoch [70/80], Step [500/500], Loss: 0.3190
Epoch [71/80], Step [100/500], Loss: 0.4371
Epoch [71/80], Step [200/500], Loss: 0.4293
Epoch [71/80], Step [300/500], Loss: 0.4279
Epoch [71/80], Step [400/500], Loss: 0.4478
Epoch [71/80], Step [500/500], Loss: 0.2983
Epoch [72/80], Step [100/500], Loss: 0.3917
Epoch [72/80], Step [200/500], Loss: 0.3143
Epoch [72/80], Step [300/500], Loss: 0.2839
Epoch [72/80], Step [400/500], Loss: 0.4507
Epoch [72/80], Step [500/500], Loss: 0.3409
Epoch [73/80], Step [100/500], Loss: 0.2883
Epoch [73/80], Step [200/500], Loss: 0.2914
Epoch [73/80], Step [300/500], Loss: 0.3873
Epoch [73/80], Step [400/500], Loss: 0.3090
Epoch [73/80], Step [500/500], Loss: 0.2304
Epoch [74/80], Step [100/500], Loss: 0.3744
Epoch [74/80], Step [200/500], Loss: 0.3707
Epoch [74/80], Step [300/500], Loss: 0.3504
Epoch [74/80], Step [400/500], Loss: 0.3842
Epoch [74/80], Step [500/500], Loss: 0.2762
Epoch [75/80], Step [100/500], Loss: 0.3845
Epoch [75/80], Step [200/500], Loss: 0.2869
Epoch [75/80], Step [300/500], Loss: 0.4739
Epoch [75/80], Step [400/500], Loss: 0.2564
Epoch [75/80], Step [500/500], Loss: 0.4151
Epoch [76/80], Step [100/500], Loss: 0.2882
Epoch [76/80], Step [200/500], Loss: 0.2850
Epoch [76/80], Step [300/500], Loss: 0.4033
Epoch [76/80], Step [400/500], Loss: 0.3802
Epoch [76/80], Step [500/500], Loss: 0.2550
Epoch [77/80], Step [100/500], Loss: 0.3178
Epoch [77/80], Step [200/500], Loss: 0.3310
Epoch [77/80], Step [300/500], Loss: 0.3325
Epoch [77/80], Step [400/500], Loss: 0.3344
Epoch [77/80], Step [500/500], Loss: 0.2777
Epoch [78/80], Step [100/500], Loss: 0.3008
Epoch [78/80], Step [200/500], Loss: 0.5229
Epoch [78/80], Step [300/500], Loss: 0.2749
Epoch [78/80], Step [400/500], Loss: 0.3293
Epoch [78/80], Step [500/500], Loss: 0.3426
Epoch [79/80], Step [100/500], Loss: 0.2453
Epoch [79/80], Step [200/500], Loss: 0.3240
Epoch [79/80], Step [300/500], Loss: 0.3560
Epoch [79/80], Step [400/500], Loss: 0.2577
Epoch [79/80], Step [500/500], Loss: 0.3159
Epoch [80/80], Step [100/500], Loss: 0.2661
Epoch [80/80], Step [200/500], Loss: 0.3118
Epoch [80/80], Step [300/500], Loss: 0.2826
Epoch [80/80], Step [400/500], Loss: 0.4284
Epoch [80/80], Step [500/500], Loss: 0.3187
# Test the model.
model.eval()
with torch.no_grad():
    correct = 0
    total = 0
    for images, labels in test_loader:
        images = images.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("Accuracy of the model no the test images: {} %".format(100 * correct / total))
Accuracy of the model no the test images: 84.61 %
# Save the model checkpoint.
torch.save(model.state_dict(), 'model_param.ckpt')

# Load the model checkpoint.
model.load_state_dict(torch.load('model_param.ckpt'))

你可能感兴趣的:(神经网络与深度学习,深度学习,pytorch,python,ResNet,神经网络)