pytorch搭建网络模型实战-LeNet5

神经网络模型训练步骤

1、数据

2、模型

3、损失函数

4、优化器

5、迭代训练

6、保存模型

基本上所有的网络训练过程都可以按这种过程来实现
这里以简单的LeNet-5识别手写体数字为例,让大家看到训练的完整流程:

import torch
from torchvision import datasets
from torchvision import transforms
from torch.utils.data import DataLoader
import torch.nn as nn
import torch.optim as optim


# 设置超参数
batch_size = 64
epochs = 40
lr = 0.001

# 1、数据
train_dataset = datasets.MNIST(root='./data',train=True,transform=transforms.ToTensor(),download=True)
test_dataset = datasets.MNIST(root='./data',train=False,transform=transforms.ToTensor())

train_loader = DataLoader(train_dataset,batch_size=batch_size,shuffle=True)
train_loader = DataLoader(test_dataset,batch_size=batch_size,shuffle=False)

# 2、模型
class LeNet5(nn.Module):
    def __init__(self, in_dim, n_class):
        super(LeNet5, self).__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(in_dim, 6, 5, stride=1, padding=2),
            nn.ReLU(True),
            nn.MaxPool2d(2, 2),

            nn.Conv2d(6, 16, 5, stride=1, padding=0),
            nn.ReLU(True),
            nn.MaxPool2d(2, 2))

        self.fc = nn.Sequential(
            nn.Linear(400, 120),
            nn.Linear(120, 84),
            nn.Linear(84, n_class))

    def forward(self, x):
        out = self.conv(x)
        out = out.view(out.size(0), -1)
        out = self.fc(out)
        return out
# 实例化模型
model = LeNet5(1,10)

# 3、损失函数
criterion = nn.CrossEntropyLoss()

# 4、优化器
optimizer = optim.SGD(model.parameters(),lr=lr)

# 5、迭代训练
def train(model,optimizer,criterion,train_loader,num_epochs):
    model.train()  # 声明这里的model是作为训练
    for epoch in range(num_epochs):
        train_loss = 0.0
        train_acc= 0.0
        train_len = 0.0
        total = 0.0

        for i,data in enumerate(train_loader):

            # 前向传播
            img,label = data
            output = model(img)

            # 反向传播
            optimizer.zero_grad()
            loss = criterion(output,label)
            loss.backward()
            
			# 更新参数
            optimizer.step()

            train_loss += loss.item()
            train_len += output.shape[0]

            # 准确率
            pred = torch.argmax(output,1)
            num_correct = pred.eq(label.data.view_as(pred)).sum()
            train_acc += num_correct.item()
            total += label.size(0)

        print('Finish {} epoch, Loss: {:.6f}, Acc: {:.6f}'.format(
            epoch + 1, train_loss / (train_len), train_acc / (total)))

if __name__ == '__main__':
    train(model,optimizer,criterion,train_loader,num_epochs=epochs)

    # 6、保存模型
    torch.save(model.state_dict(), './lenet5.pth')

后续将会实现其他经典网络实战案例,如果对你有帮助的话可以点赞收藏关注

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