230517-pytorch-深度神经网络-minist数据集

import torch
import torch.nn as nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable

# 超参数定义
input_size = 784 #图片是28*28
hidden_size = 500
num_classes = 10
num_epoches = 5
batch_size = 100
learning_rate = 0.001

# MNIST数据集
train_dataset = dsets.MNIST(root="./data",
                            train=True,
                            transform=transforms.ToTensor(),
                            download=True
                            )

test_dataset = dsets.MNIST(root = "./data",
                           train=False,
                           transform=transforms.ToTensor())
# Data Loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                            batch_size=batch_size,
                                            shuffle = True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                            batch_size=batch_size,
                                            shuffle = False)
# 神经网络(1层)
class Net(nn.Module):
    def __init__(self,input_size,hidden_size,num_classes):
        super(Net,self).__init__()
        self.fc1 = nn.Linear(input_size,hidden_size)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(hidden_size,num_classes)
    def forward(self,x):
        out = self.fc1(x)
        out = self.relu(out)
        out = self.fc2(out)
        return out

net = Net(input_size,hidden_size,num_classes)

# 损失和优化
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(),lr=learning_rate)
# 训练模型
for epoch in range(num_epoches):
    for i,(images,labels) in enumerate(train_loader):
        #convert torch tensor to Variable
        images = Variable(images.view(-1,28*28))
        labels = Variable(labels)
        #前向 后向 优化
        optimizer.zero_grad()  #清空梯度
        outputs = net(images)
        loss = criterion(outputs,labels)
        loss.backward()
        optimizer.step()
        #打印迭代过程
        if (i+1)%100 ==0:
            print(f"Epoch [{epoch+1}/{num_epoches}],step [{i+1}/{len(train_dataset)//batch_size}],Loss {loss.data}")
#测试模型
correct = 0
total = 0
for images,labels in test_loader:
    images = Variable(images.view(-1,28*28))
    outputs = net(images)
    _,predicted = torch.max(outputs.data,1)
    total += labels.size(0)
    correct += (predicted == labels).sum()
acc = 100*correct/total
print(f'Accuracy of the network on the 10000 test images:{acc} %')
#保存模型
torch.save(net.state_dict(),'model.pkl')


训练过程

Epoch [1/5],step [100/600],Loss 0.3197133243083954
Epoch [1/5],step [200/600],Loss 0.28982335329055786
Epoch [1/5],step [300/600],Loss 0.2986948490142822
Epoch [1/5],step [400/600],Loss 0.15444719791412354
Epoch [1/5],step [500/600],Loss 0.06452725827693939
Epoch [1/5],step [600/600],Loss 0.19026128947734833
Epoch [2/5],step [100/600],Loss 0.11610180884599686
Epoch [2/5],step [200/600],Loss 0.1191488727927208
Epoch [2/5],step [300/600],Loss 0.2345687747001648
Epoch [2/5],step [400/600],Loss 0.24533721804618835
Epoch [2/5],step [500/600],Loss 0.13246887922286987
Epoch [2/5],step [600/600],Loss 0.05705363303422928
Epoch [3/5],step [100/600],Loss 0.10597468167543411
Epoch [3/5],step [200/600],Loss 0.020756449550390244
Epoch [3/5],step [300/600],Loss 0.11784526705741882
Epoch [3/5],step [400/600],Loss 0.09559911489486694
Epoch [3/5],step [500/600],Loss 0.024600012227892876
Epoch [3/5],step [600/600],Loss 0.027161266654729843
Epoch [4/5],step [100/600],Loss 0.04551023989915848
Epoch [4/5],step [200/600],Loss 0.10365648567676544
Epoch [4/5],step [300/600],Loss 0.09536314010620117
Epoch [4/5],step [400/600],Loss 0.07759392261505127
Epoch [4/5],step [500/600],Loss 0.04291127994656563
Epoch [4/5],step [600/600],Loss 0.02904725819826126
Epoch [5/5],step [100/600],Loss 0.018705612048506737
Epoch [5/5],step [200/600],Loss 0.037347324192523956
Epoch [5/5],step [300/600],Loss 0.023575354367494583
Epoch [5/5],step [400/600],Loss 0.06412452459335327
Epoch [5/5],step [500/600],Loss 0.010783343575894833
Epoch [5/5],step [600/600],Loss 0.02018059976398945
Accuracy of the network on the 10000 test images:97.86000061035156 %

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值