• LeNet(pytorch实现


    LeNet

    本文编写了一个简单易懂的LeNet网络,并在F-MNIST数据集上进行测试,允许使用GPU计算

    在这里插入代码片
    import torch
    from torch import nn, optim 
    import d2lzh_pytorch as d2l
    
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    
    # f-mnist 数据集是28*28的
    class LeNet(nn.Module):
        def __init__(self):
            super(LeNet, self).__init__()
            self.conv = nn.Sequential(
                nn.Conv2d(1, 6, 5),  # 输出通道,输出通道,核大小
                nn.Sigmoid(),
                nn.MaxPool2d(2, 2),  # 高宽减半
                nn.Conv2d(6, 16, 5),
                nn.Sigmoid(),
                nn.MaxPool2d(2, 2)
            )
    
            self.fc = nn.Sequential(
                d2l.FlattenLayer(),
                nn.Linear(16*4*4, 120),
                nn.Sigmoid(),
                nn.Linear(120, 84),
                nn.Sigmoid(),
                nn.Linear(84, 10)
            )
    
        def forward(self, img):
            feature = self.conv(img)
            output = self.fc(feature)
            return output
    net = LeNet()
    
    # 数据集
    batch_size = 256
    train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
    
    # 评估测试集,支持GPU
    def evaluate_acc(data_iter, net, device = None):
        if device is None and isinstance(net, nn.Module):
            device = list(net.parameters())[0].device  # 看参数的gpu还是cpu
        acc_sum, n = 0.0, 0
        with torch.no_grad():
            for X,y in data_iter:
                if isinstance(net, nn.Module):  # 这个可加可不加
                    net.eval()  # 评估模式
                    acc_sum += (net(X.to(device)).argmax(dim=1) == y.to(device)).float().sum().cpu().item()
                    net.train()  # 转回训练模式
                    n += y.shape[0]
                    
        return acc_sum / n
    
    def train(net, train_iter, test_iter, optimizer, device, epochs):
        net = net.to(device)
        loss = nn.CrossEntropyLoss()
        for epoch in range(epochs):
            train_l_sum, train_acc_sum, n = 0.0, 0.0, 0
            for X,y in train_iter:
                X = X.to(device)
                y = y.to(device)
                y_hat = net(X)
                l = loss(y_hat, y)
                optimizer.zero_grad()
                l.backward()
                optimizer.step()
                train_l_sum += l.cpu().item()
                train_acc_sum += (y_hat.argmax(dim=1) == y).sum().cpu().item()
                n += y.shape[0]
            test_acc = evaluate_acc(test_iter, net)
            print('epoch %d, loss %.4f, train_acc %.4f, test_acc %.4f'%(epoch + 1, train_l_sum, train_acc_sum/n, test_acc))
    
    lr, epochs = 0.001, 5
    optimizer = torch.optim.Adam(net.parameters(), lr=lr)
    train(net, train_iter, test_iter, optimizer, device, epochs)
    
    
    
    • 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
  • 相关阅读:
    TCP的三次握手与四次挥手
    以为很熟悉CountDownLatch的使用了,居然在生产环境翻车了
    office办公软件太贵了 Microsoft的Word为什么要买 Microsoft365家庭版多少钱 Microsoft365密钥
    曝光量近400万,小红书入秋玩出九件套,数据解析如何运用仪式感?
    React之Hooks基础
    2023年显著性检测论文及代码汇总(4)
    GBase 8s是否支持同城复制
    Impala常用时间转换函数
    AI-新手玩转RKNN
    BWO白鲸优化算法
  • 原文地址:https://blog.csdn.net/abc1234564546/article/details/133979171