当前位置:   article > 正文

PyTorch基础完整模型训练套路(土堆老师版)详细注释及讲解!小白学习必看!_python训练模型

python训练模型

目录

1、准备数据集

2、利用dataloader加载数据集

3、创建网络模型

model.py

4、损失函数

5、优化器

6、设置训练网络的参数

7 、添加tensorboard

8 、训练过程并保存模型结构及参数

①  cyx.train()和cyx.eval()

② loss.item()

③ with torch.no_grad():

④ argmax()

 9、关闭writer

完整代码


1、准备数据集

  1. # 准备数据集
  2. from torch.utils.data import DataLoader
  3. train_data = torchvision.datasets.CIFAR10(root="datasets", train=True,
  4. transform=torchvision.transforms.ToTensor(),
  5. download=False)
  6. test_data = torchvision.datasets.CIFAR10(root="datasets", train=False,
  7. transform=torchvision.transforms.ToTensor(),
  8. download=False)
  9. train_data_size = len(train_data)
  10. test_data_size = len(test_data)
  11. print("训练数据集的长度:{}".format(train_data_size))
  12. print("测试数据集的长度:{}".format(test_data_size))

2、利用dataloader加载数据集

  1. train_dataloader = DataLoader(train_data, batch_size=64)
  2. test_dataloader = DataLoader(test_data, batch_size=64)

3、创建网络模型

  1. from model import *
  2. cyx = CYX()

model.py

  1. # 搭建神经网络
  2. import torch
  3. from torch import nn
  4. from torch.nn import Sequential, Conv2d, MaxPool2d, Flatten, Linear
  5. class CYX(nn.Module):
  6. def __init__(self):
  7. super(CYX, self).__init__()
  8. self.model1 = Sequential(
  9. Conv2d(in_channels=3, out_channels=32, kernel_size=5,
  10. padding=2, stride=1),
  11. MaxPool2d(2),
  12. Conv2d(32, 32, 5, padding=2),
  13. MaxPool2d(2),
  14. Conv2d(32, 64, 5, padding=2),
  15. MaxPool2d(2),
  16. Flatten(),
  17. Linear(1024, 64),
  18. Linear(64, 10)
  19. )
  20. def forward(self, x):
  21. x = self.model1(x)
  22. return x
  23. # 验证模型是否成功搭建
  24. # if __name__ == '__main__':
  25. # cyx = CYX()
  26. # input = torch.ones((64, 3, 32, 32))
  27. # output = cyx(input)
  28. # print(output.shape)

4、损失函数

loss_fn = nn.CrossEntropyLoss()

5、优化器

  1. learning_rate = 0.01
  2. optimizer = torch.optim.SGD(cyx.parameters(), lr=learning_rate)

6、设置训练网络的参数

  1. # 记录训练的次数
  2. total_train_step = 0
  3. # 记录测试的次数
  4. total_test_step = 0
  5. # 记录训练的轮数
  6. epoch = 10

7 、添加tensorboard

writer = SummaryWriter("logs_train")

8 、训练过程并保存模型结构及参数

  1. for i in range(epoch):
  2. print("---------第{}轮训练开始---------".format(i+1))
  3. # 训练步骤开始
  4. # 只对特定层起作用
  5. cyx.train()
  6. for data in train_dataloader:
  7. imgs, targets = data
  8. outputs = cyx(imgs)
  9. loss = loss_fn(outputs, targets)
  10. # 优化器优化
  11. optimizer.zero_grad()
  12. loss.backward()
  13. optimizer.step()
  14. total_train_step = total_train_step + 1
  15. # 每逢100的时候再打印(好看)
  16. if total_train_step % 100 == 0:
  17. # .item()会把tensor里面的变成数字,比如tensor(5)->5
  18. print("训练次数:{},Loss:{}".format(total_train_step, loss.item()))
  19. writer.add_scalar("train_loss", loss.item(), total_train_step)
  20. # 测试步骤开始
  21. # 只对特定层起作用
  22. cyx.eval()
  23. # 看的是整个数据集的损失
  24. total_test_loss = 0
  25. total_accuracy = 0
  26. # 不累计梯度
  27. with torch.no_grad():
  28. for data in test_dataloader:
  29. imgs, targets = data
  30. outputs = cyx(imgs)
  31. loss = loss_fn(outputs, targets)
  32. total_test_loss = total_test_loss + loss.item()
  33. # 见截图
  34. accuracy = (outputs.argmax(1) == targets).sum()
  35. total_accuracy = total_accuracy + accuracy
  36. print("整体测试集上的Loss:{}".format(total_test_loss))
  37. print("整体测试集上的正确率:{}".format(total_accuracy/test_data_size))
  38. writer.add_scalar("test_loss", total_test_loss, total_test_step)
  39. writer.add_scalar("test_accuracy", total_accuracy/test_data_size, total_train_step)
  40. total_test_step = total_test_step + 1
  41. torch.save(cyx, "cyx_cpu_{}.pth".format(i))
  42. # torch.save(cyx.state_dict(), "cyx_{}.pth".format(i))
  43. print("模型已保存")

注意细节:

①  cyx.train()和cyx.eval()

是否启用 Batch Normalization 和 Dropout,分别在训练时和测试时添加。

参考文章:

【Pytorch】model.train() 和 model.eval() 原理与用法

② loss.item()

把tensor里面的变成数字,比如tensor(5)->5

参考文章:

【Pytorch】.item() 方法介绍

③ with torch.no_grad():

with 语句适用于对资源进行访问的场合,确保不管使用过程中是否发生异常都会执行必要的“清理”操作,释放资源,比如文件使用后自动关闭/线程中锁的自动获取和释放等。

因为是测试,不累计梯度

参考文章:

【pytorch】 with torch.no_grad():用法详解

④ argmax()

arg = 1时,沿 axis=1 比较
arg = 1时,沿 axis=1 比较
arg = 0时,沿 axis=0 比较
arg = 0时,沿 axis=0 比较

 9、关闭writer

writer.close()

完整代码

  1. import torch
  2. import torchvision
  3. from torch import nn
  4. from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
  5. from torch.utils.tensorboard import SummaryWriter
  6. from model import *
  7. # 准备数据集
  8. from torch.utils.data import DataLoader
  9. train_data = torchvision.datasets.CIFAR10(root="datasets", train=True,
  10. transform=torchvision.transforms.ToTensor(),
  11. download=False)
  12. test_data = torchvision.datasets.CIFAR10(root="datasets", train=False,
  13. transform=torchvision.transforms.ToTensor(),
  14. download=False)
  15. train_data_size = len(train_data)
  16. test_data_size = len(test_data)
  17. print("训练数据集的长度:{}".format(train_data_size))
  18. print("测试数据集的长度:{}".format(test_data_size))
  19. # 利用dataloader加载数据集
  20. train_dataloader = DataLoader(train_data, batch_size=64)
  21. test_dataloader = DataLoader(test_data, batch_size=64)
  22. # 创建网络模型
  23. cyx = CYX()
  24. # 损失函数
  25. loss_fn = nn.CrossEntropyLoss()
  26. # 优化器
  27. learning_rate = 0.01
  28. optimizer = torch.optim.SGD(cyx.parameters(), lr=learning_rate)
  29. # 设置训练网络的参数
  30. # 记录训练的次数
  31. total_train_step = 0
  32. # 记录测试的次数
  33. total_test_step = 0
  34. # 记录训练的轮数
  35. epoch = 10
  36. # 添加tensorboard
  37. writer = SummaryWriter("logs_train")
  38. for i in range(epoch):
  39. print("---------第{}轮训练开始---------".format(i+1))
  40. # 训练步骤开始
  41. # 只对特定层起作用
  42. cyx.train()
  43. for data in train_dataloader:
  44. imgs, targets = data
  45. outputs = cyx(imgs)
  46. loss = loss_fn(outputs, targets)
  47. # 优化器优化
  48. optimizer.zero_grad()
  49. loss.backward()
  50. optimizer.step()
  51. total_train_step = total_train_step + 1
  52. # 每逢100的时候再打印(好看)
  53. if total_train_step % 100 == 0:
  54. # .item()会把tensor里面的变成数字,比如tensor(5)->5
  55. print("训练次数:{},Loss:{}".format(total_train_step, loss.item()))
  56. writer.add_scalar("train_loss", loss.item(), total_train_step)
  57. # 测试步骤开始
  58. # 只对特定层起作用
  59. cyx.eval()
  60. # 看的是整个数据集的损失
  61. total_test_loss = 0
  62. total_accuracy = 0
  63. # 不累计梯度
  64. with torch.no_grad():
  65. for data in test_dataloader:
  66. imgs, targets = data
  67. outputs = cyx(imgs)
  68. loss = loss_fn(outputs, targets)
  69. total_test_loss = total_test_loss + loss.item()
  70. # 见截图
  71. accuracy = (outputs.argmax(1) == targets).sum()
  72. total_accuracy = total_accuracy + accuracy
  73. print("整体测试集上的Loss:{}".format(total_test_loss))
  74. print("整体测试集上的正确率:{}".format(total_accuracy/test_data_size))
  75. writer.add_scalar("test_loss", total_test_loss, total_test_step)
  76. writer.add_scalar("test_accuracy", total_accuracy/test_data_size, total_train_step)
  77. total_test_step = total_test_step + 1
  78. torch.save(cyx, "cyx_cpu_{}.pth".format(i))
  79. # torch.save(cyx.state_dict(), "cyx_{}.pth".format(i))
  80. print("模型已保存")
  81. writer.close()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/277395
推荐阅读
相关标签
  

闽ICP备14008679号