#nn_loss_optim#
import torchvision.datasets from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential from torch import nn import torch from torch.utils.data import DataLoader from torch.utils.tensorboard import SummaryWriter import tqdm """ 损失函数: 1.计算实际输出和目标之间的差距 2.为我们更新输出提供了一定的依据(方向传播), grad optim 优化器:torch.optim.SGD(, lr) lr: 学习速率 """ dataset = torchvision.datasets.CIFAR10("D:/dev/python/pyWork/Season2/Stage1/data/myimg", train=False, transform=torchvision.transforms.ToTensor()) dataloader = DataLoader(dataset, batch_size=1) class Test(nn.Module): def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) # 利用Sequential使代码更简洁, 更易管理 self.module1 = Sequential( Conv2d(3, 32, 5, padding=2), MaxPool2d(2), Conv2d(32, 32, 5, padding=2), MaxPool2d(2), Conv2d(32, 64, 5, padding=2), MaxPool2d(2), Flatten(), Linear(1024, 64), Linear(64, 10) ) def forward(self, x): x = self.module1(x) return x loss = nn.CrossEntropyLoss() # 创建损失函数对象 test = Test() optim = torch.optim.SGD(test.parameters(), lr=0.01) for epoch in range(20): # 进行20轮的训练 running_loss = 0.0 for data in dataloader: imgs, targets = data outputs = test(imgs) result_loss = loss(outputs, targets) # 计算损失 optim.zero_grad() # 将梯度设置为零 result_loss.backward() # 反向传播 optim.step() # 调优 running_loss = running_loss + result_loss print(running_loss)
#nn_maxpool#
import torch import torchvision from torch import nn from torch.nn import MaxPool2d from torch.utils.data import DataLoader """ torch.nn.MaxPool2d(kernel_size, stride, padding, dilation=1, return_indices=False, ceil_mode=False) ceil_mode: 向上或向下取整, 默认为False 向下取整 """ test_set = torchvision.datasets.CIFAR10(root="D:/dev/python/pyWork/Season2/Stage1/data/myimg", train=False, transform=torchvision.transforms.ToTensor()) dataloader = DataLoader(test_set, batch_size=64) input = torch.tensor([[1, 2, 0, 3, 1], [0, 1, 2, 3, 1], [1, 2, 1, 0, 0], [5, 2, 3, 1, 1], [2, 1, 0, 1, 1]], dtype=torch.float32) input = torch.reshape(input, (-1, 1, 5, 5)) class Test(nn.Module): def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) self.maxpool1 = MaxPool2d(3, ceil_mode=True) def forward(self, input): output = self.maxpool1(input) return output test = Test() output = test(input) print(output) for data in dataloader: imgs, targets = data print(imgs.size)