当前位置:网站首页>Lenet5 training model of convolutional neural network for machine learning
Lenet5 training model of convolutional neural network for machine learning
2022-06-28 16:13:00 【Hua Weiyun】
Lenet5 Training models
Download datasets

It can be downloaded in advance or online
train_data = torchvision.datasets.MNIST(root='./',download=True,train=True,transform=transform)test_data = torchvision.datasets.MNIST(root='./',download=True,train=False,transform=transform)Training models
import torchimport torchvisionclass Lenet5(torch.nn.Module): def __init__(self): super(Lenet5, self).__init__() self.model = torch.nn.Sequential( torch.nn.Conv2d(in_channels=1,out_channels=6,kernel_size=5), # 1*32*32 # 6*28*28 torch.nn.ReLU(), torch.nn.MaxPool2d(kernel_size=2, # 6*14*14 stride=2), torch.nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5), # 16 *10*10 torch.nn.ReLU(), torch.nn.MaxPool2d(kernel_size=2, stride=2), # 16*5*5 torch.nn.Conv2d(in_channels=16, out_channels=120, kernel_size=5), # 120*1*1 torch.nn.ReLU(), torch.nn.Flatten(), # Flattening become 120*1 dimension torch.nn.Linear(120, 84), torch.nn.Linear(84, 10) ) def forward(self,x): x = self.model(x) return xtransform = torchvision.transforms.Compose( [torchvision.transforms.Resize(32), torchvision.transforms.ToTensor()])train_data = torchvision.datasets.MNIST(root='./',download=True,train=True,transform=transform)test_data = torchvision.datasets.MNIST(root='./',download=True,train=False,transform=transform)# Load data in batches 64 128train_loader =torch.utils.data.DataLoader(train_data,batch_size=64,shuffle=True)test_loader =torch.utils.data.DataLoader(test_data,batch_size=64,shuffle=True)#gpudevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')net = Lenet5().to(device)loss_func = torch.nn.CrossEntropyLoss().to(device)optim = torch.optim.Adam(net.parameters(),lr=0.001)net.train()for epoch in range(10): for step,(x,y) in enumerate(train_loader): x = x.to(device) y = y.to(device) ouput = net(x) loss = loss_func(ouput,y) # Calculate the loss optim.zero_grad() loss.backward() optim.step() print('epoch:',epoch,"loss:",loss)torch.save(net,'net.pkl')
import torchimport torchvisionclass Lenet5(torch.nn.Module): def __init__(self): super(Lenet5, self).__init__() self.model = torch.nn.Sequential( torch.nn.Conv2d(in_channels=1,out_channels=6,kernel_size=5), # 1*32*32 # 6*28*28 torch.nn.ReLU(), torch.nn.MaxPool2d(kernel_size=2, # 6*14*14 stride=2), torch.nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5), # 16 *10*10 torch.nn.ReLU(), torch.nn.MaxPool2d(kernel_size=2, stride=2), # 16*5*5 torch.nn.Conv2d(in_channels=16, out_channels=120, kernel_size=5), # 120*1*1 torch.nn.ReLU(), torch.nn.Flatten(), # Flattening become 120*1 dimension torch.nn.Linear(120, 84), torch.nn.Linear(84, 10) ) def forward(self,x): x = self.model(x) return xtransform = torchvision.transforms.Compose( [torchvision.transforms.Resize(32), torchvision.transforms.ToTensor()])test_data = torchvision.datasets.MNIST(root='./',download=False,train=False,transform=transform)test_loader =torch.utils.data.DataLoader(test_data,batch_size=64,shuffle=True)device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')net = torch.load('net.pkl')net.eval() # Show reasoning with torch.no_grad(): for step,(x,y) in enumerate(test_loader): x,y = x.to(device),y.to(device) pre = net(x) print(pre) pre_y = torch.max(pre.cpu(),1)[1].numpy() print(pre_y) y = y.cpu().numpy() acc = (pre_y == y).sum()/len(y) print("accu:",acc)
边栏推荐
- 请问下大家有遇到过这种设置的主健和数据库一致的错误吗?
- 逆向调试入门-PE结构详解02/07
- 一次简单的反射型XSS操作及思路
- 面试官: 线程池是如何做到线程复用的?有了解过吗,说说看
- 【Hot100】1. 两数之和
- Opengauss kernel: analysis of SQL parsing process
- Qt5.5.1配置MSVC2010编绎器和windbg调试器
- NFT质押LP流动性挖矿系统开发详情
- 早晨有些犹豫
- A 24-year-old bald programmer teaches you how to continuously integrate and deliver microservice delivery. You can't learn how to cut me off
猜你喜欢

平台即代码的未来是Kubernetes扩展

24岁秃头程序员教你微服务交付下如何持续集成交付,学不会砍我

Azure Kinect微软摄像头Unity开发小结

RedmiBook Pro 14增强版 打不开台达软件DRAStudio_v1.00.07.52

Visual studio 2019 software installation package and installation tutorial

Visual Studio 2019软件安装包和安装教程

【MySQL】官网文档学习之查询语句sql注意事项
![The k-th element in the array [heap row + actual time complexity of heap building]](/img/69/bcafdcb09ffbf87246a03bcb9367aa.png)
The k-th element in the array [heap row + actual time complexity of heap building]

Etcd可视化工具:Kstone简介(一)

Knowing these commands allows you to master shell's own tools
随机推荐
数字藏品热潮之下,你必须知道的那些事儿
Slim GAIN(SGAIN)介绍及代码实现——基于生成对抗网络的缺失数据填补
The k-th element in the array [heap row + actual time complexity of heap building]
Jenkins的安装及使用
A little hesitant in the morning
【初学者必看】vlc实现的rtsp服务器及转储H264文件
[high concurrency foundation] MySQL index optimization
NFT质押LP流动性挖矿系统开发详情
机器学习之卷积神经网络Lenet5训练模型
Introduction to reverse commissioning PE structure details 02/07
[Spock] process non ASCII characters in an identifier
简单介绍一下tensorflow与pytorch的相互转换(主要是tensorflow转pytorch)
Geoffrey Hinton:我的五十年深度学习生涯与研究心法
Gartner发布当前至2024年的五大隐私趋势
面试官: 线程池是如何做到线程复用的?有了解过吗,说说看
物联网云融合安全指南
【Proteus仿真】L297驱动步进电机
【MySQL】官网文档学习之查询语句sql注意事项
IPDK — Overview
【Hot100】2.两数相加