跟着李沐学AI代码复现-03BN-LeNet,04ResNet

 

import torch
from torch import nn
from torch.utils import data

import torchvision
from torchvision import transforms

'''
'''

# 超参数定义
batch_size = 256
epoch_num = 10
lr = 0.03
if torch.cuda.device_count() >= 1:
    device = torch.device(f'cuda:0')
else:
    device = torch.device('cpu')


def load():
    # 加载数据集
    # 这里的root指的是本地目录。
    mnist_train = torchvision.datasets.FashionMNIST(
        root="data", train=True, transform=transforms.ToTensor(), download=True)
    mnist_test = torchvision.datasets.FashionMNIST(
        root="data", train=False, transform=transforms.ToTensor(), download=True)
    # 将数据集转为特定格式 
    train_iter = data.DataLoader(mnist_train, batch_size, shuffle=True, num_workers=8)
    test_iter = data.DataLoader(mnist_test, batch_size, shuffle=False, num_workers=8)

    return train_iter, test_iter


def netdefine():
    # 网络定义
    net = nn.Sequential(
        nn.Conv2d(1, 6, kernel_size=5, padding=2), nn.BatchNorm2d(6),
        nn.Sigmoid(), nn.AvgPool2d(kernel_size=2, stride=2),
        nn.Conv2d(6, 16, kernel_size=5), nn.BatchNorm2d(16),
        nn.Sigmoid(),
        nn.AvgPool2d(kernel_size=2, stride=2),
        nn.Flatten(),
        nn.Linear(16 * 5 * 5, 120), nn.BatchNorm1d(120),
        nn.Sigmoid(),
        nn.Linear(120, 84), nn.BatchNorm1d(84),
        nn.Sigmoid(),
        nn.Linear(84, 10))

    # 初始化参数
    def init_weights(m):
        if type(m) == nn.Linear or type(m) == nn.Conv2d:
            nn.init.xavier_uniform_(m.weight)

    net.apply(init_weights)
    # 模型迁移
    net = net.to(device)
    return net


def train(net, train_iter):
    # optimizer会根据梯度对所有参数进行更新
    optimizer = torch.optim.SGD(net.parameters(), lr)
    loss = nn.CrossEntropyLoss()
    for epoch in range(0, epoch_num):
        net.train()
        total = 0
        correct = 0
        for X, y in train_iter:
            # 梯度置零
            X, y = X.to(device), y.to(device)
            y_hat = net(X)
            l = loss(y_hat, y)
            # 梯度反向传播,参数更新
            optimizer.zero_grad()
            l.backward()
            optimizer.step()
            # 计算准确率
            _, pred = torch.max(y_hat, 1)
            total += y.size(0)
            correct += (pred == y).sum().item()
        print(f'epoch = {epoch}, total = {total}, acurrcy = {100 * correct / total}%')


def test(net, test_iter):
    net.eval()
    with torch.no_grad():
        correct = 0
        total = 0
        for X, y in test_iter:
            X = X.to(device)
            y = y.to(device)
            output = net(X)
            _, pred = torch.max(output, 1)
            total += y.size(0)
            correct += (pred == y).sum().item()
        print(f'test,total = {total}, acurrcy = {100 * correct / total}%')


if __name__ == '__main__':
    train_iter, test_iter = load()
    net = netdefine()
    train(net, train_iter)
    test(net, test_iter)
import torch
from torch import nn
from torch.utils import data

import torchvision
from torchvision import transforms

'''
'''

# 超参数定义
batch_size = 256
epoch_num = 10
lr = 0.03
if torch.cuda.device_count() >= 1:
    device = torch.device(f'cuda:0')
else:
    device = torch.device('cpu')


def load():
    # 加载数据集
    # 这里的root指的是本地目录。
    mnist_train = torchvision.datasets.FashionMNIST(
        root="data", train=True, transform=transforms.ToTensor(), download=True)
    mnist_test = torchvision.datasets.FashionMNIST(
        root="data", train=False, transform=transforms.ToTensor(), download=True)
    # 将数据集转为特定格式 
    train_iter = data.DataLoader(mnist_train, batch_size, shuffle=True, num_workers=8)
    test_iter = data.DataLoader(mnist_test, batch_size, shuffle=False, num_workers=8)

    return train_iter, test_iter


def netdefine():
    # 网络定义
    net = nn.Sequential(
        nn.Conv2d(1, 6, kernel_size=5, padding=2), nn.BatchNorm2d(6),
        nn.Sigmoid(), nn.AvgPool2d(kernel_size=2, stride=2),
        nn.Conv2d(6, 16, kernel_size=5), nn.BatchNorm2d(16),
        nn.Sigmoid(),
        nn.AvgPool2d(kernel_size=2, stride=2),
        nn.Flatten(),
        nn.Linear(16 * 5 * 5, 120), nn.BatchNorm1d(120),
        nn.Sigmoid(),
        nn.Linear(120, 84), nn.BatchNorm1d(84),
        nn.Sigmoid(),
        nn.Linear(84, 10))

    # 初始化参数
    def init_weights(m):
        if type(m) == nn.Linear or type(m) == nn.Conv2d:
            nn.init.xavier_uniform_(m.weight)

    net.apply(init_weights)
    # 模型迁移
    net = net.to(device)
    return net


def train(net, train_iter):
    # optimizer会根据梯度对所有参数进行更新
    optimizer = torch.optim.SGD(net.parameters(), lr)
    loss = nn.CrossEntropyLoss()
    for epoch in range(0, epoch_num):
        net.train()
        total = 0
        correct = 0
        for X, y in train_iter:
            # 梯度置零
            X, y = X.to(device), y.to(device)
            y_hat = net(X)
            l = loss(y_hat, y)
            # 梯度反向传播,参数更新
            optimizer.zero_grad()
            l.backward()
            optimizer.step()
            # 计算准确率
            _, pred = torch.max(y_hat, 1)
            total += y.size(0)
            correct += (pred == y).sum().item()
        print(f'epoch = {epoch}, total = {total}, acurrcy = {100 * correct / total}%')


def test(net, test_iter):
    net.eval()
    with torch.no_grad():
        correct = 0
        total = 0
        for X, y in test_iter:
            X = X.to(device)
            y = y.to(device)
            output = net(X)
            _, pred = torch.max(output, 1)
            total += y.size(0)
            correct += (pred == y).sum().item()
        print(f'test,total = {total}, acurrcy = {100 * correct / total}%')


if __name__ == '__main__':
    train_iter, test_iter = load()
    net = netdefine()
    train(net, train_iter)
    test(net, test_iter)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值