PyTorch基础(part4)

网友投稿 290 2022-11-18

PyTorch基础(part4)

学习笔记,仅供参考,有错必纠

文章目录

​​PyTorch 基础​​

​​MNIST数据识别​​

​​常用代码​​​​导包​​​​载入数据​​​​定义网络结构​​

PyTorch 基础

MNIST数据识别

常用代码

# 支持多行输出from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = 'all' #默认为'last'

导包

# 导入常用的包import torchfrom torch import nn,optimimport numpy as npimport matplotlib.pyplot as pltfrom torch.autograd import Variablefrom torchvision import datasets, transformsfrom torch.utils.data import DataLoader

载入数据

# 载入数据train_dataset = datasets.MNIST(root = './data/', # 载入的数据存放的位置 train = True, # 载入训练集数据 transform = transforms.ToTensor(), # 将载入进来的数据变成Tensor download = True) # 是否下载数据test_dataset = datasets.MNIST(root = './data/', # 载入的数据存放的位置 train = False, # 载入测试集数据 transform = transforms.ToTensor(), # 将载入进来的数据变成Tensor download = True) # 是否下载数据

Downloading to ./data/MNIST\raw\train-images-idx3-ubyte.gz31.0%IOPub message rate exceeded.The notebook server will temporarily stop sending outputto the client in order to avoid crashing it.To change this limit, set the config variable`--NotebookApp.iopub_msg_rate_limit`.89.6%IOPub message rate exceeded.The notebook server will temporarily stop sending outputto the client in order to avoid crashing it.To change this limit, set the config variable`--NotebookApp.iopub_msg_rate_limit`.100.0%Extracting ./data/MNIST\raw\t10k-images-idx3-ubyte.gz to ./data/MNIST\rawDownloading to ./data/MNIST\raw\t10k-labels-idx1-ubyte.gz112.7%Extracting ./data/MNIST\raw\t10k-labels-idx1-ubyte.gz to ./data/MNIST\raw

# 设置每次训练的批次大小batch_size = 64# 数据生成器(打乱数据集, 并每次迭代返回一个批次的数据)train_loader = DataLoader(dataset = train_dataset, batch_size = batch_size, shuffle = True)test_loader = DataLoader(dataset = test_dataset, batch_size = batch_size, shuffle = True)

# 查看数据生成器的内部结构for i, data in enumerate(train_loader): inputs, labels = data print("批次:", i) print("输入数据的形状:", inputs.shape) print("标签的形状:", labels.shape) break

批次: 0输入数据的形状: torch.Size([64, 1, 28, 28])标签的形状: torch.Size([64])

torch.Size([64, 1, 28, 28]) 中:

64代表包含的样本数;1代表通道数,如果图像为黑白图像,那么通道数为1,如果图像为彩色图像,那么通道数为3;最后两个数值28, 28表示图像的尺寸.

len(train_loader)

938

labels

tensor([9, 3, 8, 1, 1, 3, 0, 4, 7, 4, 8, 4, 6, 4, 8, 5, 0, 0, 2, 0, 1, 6, 8, 3, 3, 6, 6, 5, 0, 6, 7, 0, 5, 3, 8, 3, 2, 5, 9, 9, 1, 5, 4, 3, 8, 3, 1, 3, 1, 7, 8, 6, 5, 3, 9, 4, 2, 7, 0, 1, 9, 1, 0, 0])

定义网络结构

class MyNet(nn.Module): def __init__(self): super(MyNet, self).__init__() self.fc1 = nn.Linear(784, 10) self.softmax = nn.Softmax(dim = 1) # 输出的维度为(64, 10), 对维度dim = 1进行概率转换,使其和为1 def forward(self, x): x = x.view(x.size()[0], -1) x = self.fc1(x) out = self.softmax(x) return out

LR = 0.5# 定义模型model = MyNet()# 定义代价函数mse_loss = nn.MSELoss()# 定义优化器optimizer = optim.SGD(model.parameters(), lr = LR)

# 定义训练def train(): for i, data in enumerate(train_loader): # 或者某个批次的数据和标签 inputs, labels = data # 获取预测结果 out = model(inputs) # 把数据标签标称独热编码 labels = labels.reshape(-1, 1) one_hot = torch.zeros(inputs.shape[0], 10).scatter(1, labels, 1) # tensor.scatter(dim, index, src) # dim:对哪个维度进行独热编码 # index:要将src中对应的值放到tensor的哪个位置。 # src:插入index的数值 # 计算 loss loss = mse_loss(out, one_hot) # 梯度清0 optimizer.zero_grad() # 计算梯度 loss.backward() # 修改权值 optimizer.step()

# 定义测试def test(): correct = 0 for i, data in enumerate(test_loader): inputs, labels = data out = model(inputs) # 获得最大值,以及最大值所在的位置 _, predicted = torch.max(out, 1) # 预测正确的数量 correct += (predicted == labels).sum() print("Test Acc:{0}".format(correct.item()/len(test_dataset)))

for epoch in range(10): print("epoch:", epoch) train() test()

epoch: 0Test Acc:0.8882epoch: 1Test Acc:0.9epoch: 2Test Acc:0.9078epoch: 3Test Acc:0.911epoch: 4Test Acc:0.9145epoch: 5Test Acc:0.9159epoch: 6Test Acc:0.9168epoch: 7Test Acc:0.9179epoch: 8Test Acc:0.9184epoch: 9Test Acc:0.9195

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:spring boot udp或者tcp接收数据的实例详解
下一篇:网络摄像机与模拟摄像机的区别对比以及光纤熔接的解决
相关文章

 发表评论

暂时没有评论,来抢沙发吧~