吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1516|回复: 0
收起左侧

[Python 转载] pytorch训练MNIST数据集

[复制链接]
luoshiyong123 发表于 2020-9-11 22:24
本帖最后由 luoshiyong123 于 2020-9-11 22:26 编辑

本文采用全连接网络对MNIST数据集进行训练,训练模型主要由五个线性单元和relu激活函数组成
[Python] 纯文本查看 复制代码
import torch
from torchvision import transforms
from torchvision import datasets
from torch.utils.data import DataLoader
import torch.nn.functional as  F
import torch.optim as optim
import os
import sys
 
batch_size = 64
transform = transforms.Compose(
    [
        transforms.ToTensor(),     #将0-255变成0-1
        transforms.Normalize((0.1307,),(0.3081,)) #正则化
    ]
)
train_dataset =datasets.MNIST(root='../dataset/mnist',
                                train = True,
                                download = True,
                                transform = transform)
train_loader = DataLoader(train_dataset,
                        shuffle=True,
                        batch_size=batch_size)
test_dataset =datasets.MNIST(root='../dataset/mnist',
                                train = False,
                                download = True,
                                transform = transform)
test_loader = DataLoader(test_dataset,
                        shuffle = False,
                        batch_size=batch_size)
class  Net(torch.nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        self.f1 = torch.nn.Linear(784,512)
        self.f2 = torch.nn.Linear(512,256)
        self.f3 = torch.nn.Linear(256,128)
        self.f4 = torch.nn.Linear(128,64)
        self.f5 = torch.nn.Linear(64,10)
    def forward(self,x):
        #这里将
        x = x.view(-1,784)  #展成1*784
        x = F.relu(self.f1(x))
        x = F.relu(self.f2(x))
        x = F.relu(self.f3(x))
        x = F.relu(self.f4(x))
        return self.f5(x)
model = Net()
#loss--交叉熵
criterion = torch.nn.CrossEntropyLoss()  
#带冲量
optimzer = optim.SGD(model.parameters(),lr=0.01,momentum = 0.5)
#训练
def train(epoch):
    running_loss =0.0
    for batch_idx,data in enumerate(train_loader):
        inputs,target = data
        optimzer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs,target)
        loss.backward()
        optimzer.step()
        running_loss += loss.item()
        if batch_idx%300==299:
            print('[%d,%5d] loss:%.3f' % (epoch+1,batch_idx+1,running_loss/300))
            running_loss = 0.0
def test():
    correct = 0
    total = 0
    with torch.no_grad():
        for batch_idx,data in enumerate(test_loader):
            images,labels = data
            outputs = model(images)
            _,predicted = torch.max(outputs.data,dim=1)
            total += labels.size(0)
            correct += (predicted==labels).sum().item()
    
    print('Accuracy on test set:%d %%' % (100*correct/total)) 
if __name__== '__main__':
        for epoch in range(7):
            train(epoch)
            test()
        #保存网络参数
       


结果: 经过7论训练测试集可以达到97%

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

天空宫阙 发表于 2020-9-11 23:02
五层全连接模型的参数多点,显卡好就可以硬怼
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 01:37

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表