P27.完整的模型训练套路(一)
import torchvision
# prepare dataset
train_data = torchvision.datasets.CIFAR10(root="dataset", train=True, transform=torchvision.transforms.ToTensor(),
download=True)
test_data = torchvision.datasets.CIFAR10(root="dataset", train=True, transform=torchvision.transforms.ToTensor(),
download=True)
# length
train_data_size = len(train_data)
test_data_size = len(test_data)
# if train_data_size=10,The length of train dataset is 10
print("The length of train dataset is:{}".format(train_data_size))
print("The length of test dataset is:{}".format(test_data_size))
Files already downloaded and verified
Files already downloaded and verified
The length of train dataset is:50000
The length of test dataset is:50000
import torch
from torch import nn
# build a network model
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.model = nn.Sequential(
nn.Conv2d(3, 32, 5, 1, 2),
nn.MaxPool2d(2),
nn.Conv2d(32, 32, 5, 1, 2),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 5, 1, 2),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(64*4*4, 64),
nn.Linear(64, 10)
)
def forward(self, x):
x = self.model(x)
return x
# to test if the neural network is right
if __name__ == '__main__':
tudui = Tudui()
input = torch.ones((64, 3, 32, 32))
output = tudui(input)
print(output.shape)
torch.Size([64, 10])
import torchvision
from torch.utils.data import DataLoader
from P27_model import *
# prepare dataset
train_data = torchvision.datasets.CIFAR10(root="dataset", train=True, transform=torchvision.transforms.ToTensor(),
download=True)
test_data = torchvision.datasets.CIFAR10(root="dataset", train=True, transform=torchvision.transforms.ToTensor(),
download=True)
# length
train_data_size = len(train_data)
test_data_size = len(test_data)
# if train_data_size=10,The length of train dataset is 10
print("The length of train dataset is:{}".format(train_data_size))
print("The length of test dataset is:{}".format(test_data_size))
# load dataset with DataLoder
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)
# build a network model
tudui = Tudui()
# loss function
loss_fn = nn.CrossEntropyLoss()
# optimizer
# learning_rate = 0.01
# 1e-2 = 1 x (10)^(-2) = 1/100 = 0.01
learning_rate = 1e-2
optimizer = torch.optim.SGD(tudui.parameters(), lr=learning_rate)
# set some parameters of training network
# record the number of training
total_train_step = 0
# record the number of testing
total_test_step = 0
# number of training rounds
epoch = 10
for i in range(epoch):
print("------The {} round training start------".format(i+1))
# training start
for data in train_dataloader:
imgs, targets = data
outputs = tudui(imgs)
loss = loss_fn(outputs, targets)
# use optimizer to optimize the model
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_train_step = total_train_step + 1
print("training round:{}, Loss:{}".format(total_train_step, loss.item()))
Files already downloaded and verified
Files already downloaded and verified
The length of train dataset is:50000
The length of test dataset is:50000
------The 1 round training start------
training round:1, Loss:2.3091909885406494
training round:2, Loss:2.3123345375061035
training round:3, Loss:2.3105111122131348
training round:4, Loss:2.2946245670318604
training round:5, Loss:2.3073222637176514
training round:6, Loss:2.303025245666504
training round:7, Loss:2.3019297122955322...