import torch import torch.nn as nn from torchvision import datasets from torchvision import transforms import torchvision.utils import torch.utils.data as data # 对数据进行batch的划分,在训练模型时用到此函数,用来把训练数据分成多个小组,此函数每次抛出一组数据,直至把所有数据都抛出 from matplotlib import pyplot as plt import numpy as np import torch.nn.functional as F class Reshape(torch.nn.Module): def forward(self, x): # 将图像转为(X,1,28,28)的形式,其中X为图像的数量,1*28*28为图像格式,1为通道数 return x.view(-1, 1, 28, 28) net = nn.Sequential( # 定义LeNet-5网络结构 Reshape(), nn.Conv2d(1, 6, kernel_size=5, padding=2), nn.Sigmoid(), nn.AvgPool2d(kernel_size=2, stride=2), nn.Conv2d(6, 16, kernel_size=5), nn.Sigmoid(), nn.AvgPool2d(kernel_size=2, stride=2), nn.Flatten(), nn.Linear(16 * 5 * 5, 120), nn.Sigmoid(), nn.Linear(120, 84), nn.Sigmoid(), nn.Linear(84, 10) ) """ x = torch.rand(size=(1, 1, 28, 28), dtype=torch.float32) #初始化一个1*1*28*28的torch张量对模型进行检查,查看其相应层的输出 for layer in net: x=layer(x) print(layer.__class__.__name__,'output shape:\t',x.shape) """ def load_data_fashion_mnist(batch_seze): # 下载MNIST数据集然后加载到内存中 train_dataset = datasets.MNIST(root='../data', train=True, transform=transforms.ToTensor(), download=True) test_dataset = datasets.MNIST(root='../data', train=False, transform=transforms.ToTensor(), download=True) return (data.DataLoader(train_dataset, batch_size, shuffle=True), data.DataLoader(test_dataset, batch_size, shuffle=False)) batch_size = 64 train_iter, test_iter = load_data_fashion_mnist(batch_size) def imshow(img): img = img / 2 + 0.5 npimg = img.numpy() plt.imshow(np.transpose(npimg, (1, 2, 0))) plt.show() # print(iter(train_iter).next()[0].size()) # imshow(torchvision.utils.make_grid(iter(train_iter).next()[0])) #利用torchvision.utils.make_grid输出图像,再进行可视化展示 # 损失函数 loss_function = nn.CrossEntropyLoss() # 优化器 optimizer = torch.optim.Adam(net.parameters()) # 开始训练 num_epochs = 10 train_loss = [] for epoch in range(num_epochs): for batch_idx, (x, y) in enumerate(train_iter): out = net(x) y_onehot = F.one_hot(y, num_classes=10).float() # 转为one-hot编码 loss = loss_function(out, y_onehot) # 均方差 # 清零梯度 optimizer.zero_grad() loss.backward() optimizer.step() train_loss.append(loss.item()) if batch_idx % 10 == 0: print(epoch, batch_idx, loss.item()) # 绘制损失曲线 plt.figure(figsize=(16, 8)) plt.grid(True, linestyle='--', alpha=0.5) plt.plot(train_loss, label='loss') plt.legend(loc="best") plt.show() # 测试准确率 total_correct = 0 for batch_idx, (x, y) in enumerate(test_iter): out = net(x) pred = out.argmax(dim=1) correct = pred.eq(y).sum().float().item() total_correct += correct total_num = len(test_iter.dataset) test_acc = total_correct / total_num print(total_correct, total_num) print("test acc:", test_acc)帮我加图片
理解问题import torch import torch.nn as nn from torchvision import datasets from torchvision import transforms import torchvision.utils import torch.utils.data as data # 对数据进行batch的划分,在训练模型时用到此函数,用来把训练数据分成多个小组,此函数每次抛出一组数据,直至把所有数据都抛出 from matplotlib import pyplot as plt import numpy as np import torch.nn.functional as F class Reshape(torch.nn.Module): def forward(self, x): # 将图像转为(X,1,28,28)的形式,其中X为图像的数量,1*28*28为图像格式,1为通道数 return x.view(-1, 1, 28, 28) net = nn.Sequential( # 定义LeNet-5网络结构 Reshape(), nn.Conv2d(1, 6, kernel_size=5, padding=2), nn.Sigmoid(), nn.AvgPool2d(kernel_size=2, stride=2), nn.Conv2d(6, 16, kernel_size=5), nn.Sigmoid(), nn.AvgPool2d(kernel_size=2, stride=2), nn.Flatten(), nn.Linear(16 * 5 * 5, 120), nn.Sigmoid(), nn.Linear(120, 84), nn.Sigmoid(), nn.Linear(84, 10) ) """ x = torch.rand(size=(1, 1, 28, 28), dtype=torch.float32) #初始化一个1*1*28*28的torch张量对模型进行检查,查看其相应层的输出 for layer in net: x=layer(x) print(layer.__class__.__name__,'output shape:\t',x.shape) """ def load_data_fashion_mnist(batch_seze): # 下载MNIST数据集然后加载到内存中 train_dataset = datasets.MNIST(root='../data', train=True, transform=transforms.ToTensor(), download=True) test_dataset = datasets.MNIST(root='../data', train=False, transform=transforms.ToTensor(), download=True) return (data.DataLoader(train_dataset, batch_size, shuffle=True), data.DataLoader(test_dataset, batch_size, shuffle=False)) batch_size = 64 train_iter, test_iter = load_data_fashion_mnist(batch_size) def imshow(img): img = img / 2 + 0.5 npimg = img.numpy() plt.imshow(np.transpose(npimg, (1, 2, 0))) plt.show() # print(iter(train_iter).next()[0].size()) # imshow(torchvision.utils.make_grid(iter(train_iter).next()[0])) #利用torchvision.utils.make_grid输出图像,再进行可视化展示 # 损失函数 loss_function = nn.CrossEntropyLoss() # 优化器 optimizer = torch.optim.Adam(net.parameters()) # 开始训练 num_epochs = 10 train_loss = [] for epoch in range(num_epochs): for batch_idx, (x, y) in enumerate(train_iter): out = net(x) y_onehot = F.one_hot(y, num_classes=10).float() # 转为one-hot编码 loss = loss_function(out, y_onehot) # 均方差 # 清零梯度 optimizer.zero_grad() loss.backward() optimizer.step() train_loss.append(loss.item()) if batch_idx % 10 == 0: print(epoch, batch_idx, loss.item()) # 绘制损失曲线 plt.figure(figsize=(16, 8)) plt.grid(True, linestyle='--', alpha=0.5) plt.plot(train_loss, label='loss') plt.legend(loc="best") plt.show() # 测试准确率 total_correct = 0 for batch_idx, (x, y) in enumerate(test_iter): out = net(x) pred = out.argmax(dim=1) correct = pred.eq(y).sum().float().item() total_correct += correct total_num = len(test_iter.dataset) test_acc = total_correct / total_num print(total_correct, total_num) print("test acc:", test_acc)帮我加图片
已完成理解「import torch import torch.nn as nn from torchvision import datasets from torchvision import transforms import torchvision.utils import torch.utils.data as data # 对数据进行batch的划分,在训练模型时用到此函数,用来把训练数据分成多个小组,此函数每次抛出一组数据,直至把所有数据都抛出 from matplotlib import pyplot as plt import numpy as np import torch.nn.functional as F class Reshape(torch.nn.Module): def forward(self, x): # 将图像转为(X,1,28,28)的形式,其中X为图像的数量,1*28*28为图像格式,1为通道数 return x.view(-1, 1, 28, 28) net = nn.Sequential( # 定义LeNet-5网络结构 Reshape(), nn.Conv2d(1, 6, kernel_size=5, padding=2), nn.Sigmoid(), nn.AvgPool2d(kernel_size=2, stride=2), nn.Conv2d(6, 16, kernel_size=5), nn.Sigmoid(), nn.AvgPool2d(kernel_size=2, stride=2), nn.Flatten(), nn.Linear(16 * 5 * 5, 120), nn.Sigmoid(), nn.Linear(120, 84), nn.Sigmoid(), nn.Linear(84, 10) ) """ x = torch.rand(size=(1, 1, 28, 28), dtype=torch.float32) #初始化一个1*1*28*28的torch张量对模型进行检查,查看其相应层的输出 for layer in net: x=layer(x) print(layer.__class__.__name__,'output shape:\t',x.shape) """ def load_data_fashion_mnist(batch_seze): # 下载MNIST数据集然后加载到内存中 train_dataset = datasets.MNIST(root='../data', train=True, transform=transforms.ToTensor(), download=True) test_dataset = datasets.MNIST(root='../data', train=False, transform=transforms.ToTensor(), download=True) return (data.DataLoader(train_dataset, batch_size, shuffle=True), data.DataLoader(test_dataset, batch_size, shuffle=False)) batch_size = 64 train_iter, test_iter = load_data_fashion_mnist(batch_size) def imshow(img): img = img / 2 + 0.5 npimg = img.numpy() plt.imshow(np.transpose(npimg, (1, 2, 0))) plt.show() # print(iter(train_iter).next()[0].size()) # imshow(torchvision.utils.make_grid(iter(train_iter).next()[0])) #利用torchvision.utils.make_grid输出图像,再进行可视化展示 # 损失函数 loss_function = nn.CrossEntropyLoss() # 优化器 optimizer = torch.optim.Adam(net.parameters()) # 开始训练 num_epochs = 10 train_loss = [] for epoch in range(num_epochs): for batch_idx, (x, y) in enumerate(train_iter): out = net(x) y_onehot = F.one_hot(y, num_classes=10).float() # 转为one-hot编码 loss = loss_function(out, y_onehot) # 均方差 # 清零梯度 optimizer.zero_grad() loss.backward() optimizer.step() train_loss.append(loss.item()) if batch_idx % 10 == 0: print(epoch, batch_idx, loss.item()) # 绘制损失曲线 plt.figure(figsize=(16, 8)) plt.grid(True, linestyle='--', alpha=0.5) plt.plot(train_loss, label='loss') plt.legend(loc="best") plt.show() # 测试准确率 total_correct = 0 for batch_idx, (x, y) in enumerate(test_iter): out = net(x) pred = out.argmax(dim=1) correct = pred.eq(y).sum().float().item() total_correct += correct total_num = len(test_iter.dataset) test_acc = total_correct / total_num print(total_correct, total_num) print("test acc:", test_acc)帮我加图片」
展开阅读网页