学习笔记,仅供参考,有错必纠
文章目录
PyTorch 基础
MNIST数据识别
常用代码
# 支持多行输出
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all' #默认为'last'
导包
# 导入常用的包
import torch
from torch import nn,optim
import numpy as np
import matplotlib.pyplot as plt
from torch.autograd import Variable
from torchvision import datasets, transforms
from 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 http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to ./data/MNIST\raw\train-images-idx3-ubyte.gz
31.0%IOPub message rate exceeded.
The notebook server will temporarily stop sending output
to 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 output
to 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\raw
Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz to ./data/MNIST\raw\t10k-labels-idx1-ubyte.gz
112.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: 0
Test Acc:0.8882
epoch: 1
Test Acc:0.9
epoch: 2
Test Acc:0.9078
epoch: 3
Test Acc:0.911
epoch: 4
Test Acc:0.9145
epoch: 5
Test Acc:0.9159
epoch: 6
Test Acc:0.9168
epoch: 7
Test Acc:0.9179
epoch: 8
Test Acc:0.9184
epoch: 9
Test Acc:0.9195