2021-09-14

这篇博客介绍了如何使用PyTorch实现MNIST手写数字识别。首先加载训练和测试数据集,然后定义一个简单的全连接神经网络模型,包括ReLU激活函数和均方误差损失函数。通过SGD优化器进行训练,并绘制损失下降曲线。最后,计算并显示测试集的准确率,以及部分测试样本的预测结果。
摘要由CSDN通过智能技术生成

pytorch初体验

实例:数字识别
代码部分:
``

import torch
from torch import nn, relu  # 完成神经网络的相关工作
from torch import optim

import torchvision
from matplotlib import pyplot as plt
from torch.nn.functional import mse_loss

from utils import plot_image,plot_curve,one_hot
batch_size=512
#1、加载训练数据集
train_loader = torch.utils.data.DataLoader(
    torchvision.datasets.MNIST('mnist_data', train=True, download=True,#如果文件不存在那么就从网上下载;trian:数据是用来训练的
                               transform=torchvision.transforms.Compose([
                                   torchvision.transforms.ToTensor(),
                                   torchvision.transforms.Normalize(#图片标准化,性能提升,使数据在0周围,如果去掉这行代码会使性能变差
                                       (0.1307,), (0.3081,))
                               ])),
    batch_size=batch_size, shuffle=True)
#一次取多少张图片,去的时候要随机打散(shuffle)
#加载测试数据集
test_loader = torch.utils.data.DataLoader(
    torchvision.datasets.MNIST('mnist_data/', train=False, download=True,
                               transform=torchvision.transforms.Compose([
                                   torchvision.transforms.ToTensor(),
                                   torchvision.transforms.Normalize(
                                       (0.1307,), (0.3081,))
                               ])),
    batch_size=batch_size, shuffle=False)

x,y=next(iter(train_loader))
print(x.shape,y.shape,x.min(),x.max())
#plot_image(x,y,'image sample')
#加载数据完毕
#2、创建网络
class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        #wx+b
        self.fc1=nn.Linear(28*28,256)#256为随即决定的
        self.fc2=nn.Linear(256,64)
        self.fc3=nn.Linear(64,10)

    def forward(self,x):
        # x:[b,1,28,28]
        #h1=relu(xw+b)
        x=relu(self.fc1(x))
        #h2=relu(xw+b)
        x=relu(self.fc2(x))
        #最后一层可加可不加激活函数,并使用均方差
        #h3=h2w3+b3
        x=self.fc3(x)

        return x
#网络初始化
net=Net()
train_loss=[]
#优化器
optimzer=optim.SGD(net.parameters(),lr=0.01,momentum=0.9)
for epoch in range(3):#整个数据集迭代三次
    #整个数据集全部迭代
    for batch_idx,(x,y) in enumerate(train_loader):
        #x:[b,1,28,28],y:[512],对数据进行重组,符合网络输入的格式
        x=x.view(x.size(0),28*28)
        #=>[b,10]
        out=net(x)
       #[b,10]
        y_onehot=one_hot(y)
        #loss=mse(out,y_out)
        loss=mse_loss(out,y_onehot)
        #清零
        optimzer.zero_grad()
        #w'=w-lr*grad
        loss.backward()
        optimzer.step()
        train_loss.append(loss.item())
        if batch_idx%10==0:
            print(epoch,batch_idx,loss.item())

#我们这个时候会得到一个比较好的[w1,b1,w2,b2,w3,b3]
total_correct=0
plot_curve(train_loss)
#做一个准确度测试
for x,y in test_loader:
    x=x.view(x.size(0),28*28)
    out=net(x)
    #得到第一维度存在最大值的索引
    pred=out.argmax(dim=1)
    correct=pred.eq(y).sum().float().item()
    total_correct+=correct

total_num=len(test_loader.dataset)
acc=total_correct/total_num
print("test acc:",acc)
#测试数据集
x,y=next(iter(test_loader))
out=net(x.view(x.size(0),28*28))
pred =out.argmax(dim=1)
plot_image(x,pred,'test')

```python
在这里插入代码片
辅助函数:
`import  torch
from    matplotlib import pyplot as plt

#画下降曲线
def plot_curve(data):
    fig = plt.figure()
    plt.plot(range(len(data)), data, color='blue')
    plt.legend(['value'], loc='upper right')
    plt.xlabel('step')
    plt.ylabel('value')
    plt.show()



def plot_image(img, label, name):

    fig = plt.figure()
    for i in range(6):
        plt.subplot(2, 3, i + 1)
        plt.tight_layout()
        plt.imshow(img[i][0]*0.3081+0.1307, cmap='gray', interpolation='none')
        plt.title("{}: {}".format(name, label[i].item()))
        plt.xticks([])
        plt.yticks([])
    plt.show()


def one_hot(label, depth=10):
    out = torch.zeros(label.size(0), depth)
    idx = torch.LongTensor(label).view(-1, 1)
    out.scatter_(dim=1, index=idx, value=1)
    return out`
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值