chap5 CNN

卷积神经网络(CNN)

问题描述:

利用卷积神经网络,实现对MNIST数据集的分类问题

数据集:

MNIST数据集包括60000张训练图片和10000张测试图片。图片样本的数量已经足够训练一个很复杂的模型(例如 CNN的深层神经网络)。它经常被用来作为一个新 的模式识别模型的测试用例。而且它也是一个方便学生和研究者们执行用例的数据集。除此之外,MNIST数据集是一个相对较小的数据集,可以在你的笔记本CPUs上面直接执行

题目要求

Pytorch版本的卷积神经网络需要补齐self.conv1中的nn.Conv2d()self.conv2()的参数,还需要填写x=x.view()中的内容。
训练精度应该在96%以上。

import os
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.utils.data as Data
import torchvision
import torch.nn.functional as F
import numpy as np
learning_rate = 1e-4
keep_prob_rate = 0.7 #
max_epoch = 3
BATCH_SIZE = 50DOWNLOAD_MNIST = False
if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'):# not mnist dir or mnist is empyt dirDOWNLOAD_MNIST = Truetrain_data = torchvision.datasets.MNIST(root='./mnist/',train=True, transform=torchvision.transforms.ToTensor(), download=DOWNLOAD_MNIST,)
train_loader = Data.DataLoader(dataset = train_data ,batch_size= BATCH_SIZE ,shuffle= True)test_data = torchvision.datasets.MNIST(root = './mnist/',train = False)
test_x = Variable(torch.unsqueeze(test_data.test_data,dim  = 1),volatile = True).type(torch.FloatTensor)[:500]/255.
test_y = test_data.test_labels[:500].numpy()class CNN(nn.Module):def __init__(self):super(CNN, self).__init__()self.conv1 = nn.Sequential(nn.Conv2d( # ???# patch 7 * 7 ; 1  in channels ; 32 out channels ; ; stride is 1# padding style is same(that means the convolution opration's input and output have the same size)in_channels=1,out_channels=32,kernel_size=7,stride=1,padding=3,),nn.ReLU(),        # activation functionnn.MaxPool2d(2),  # pooling operation)self.conv2 = nn.Sequential( # ???# line 1 : convolution function, patch 5*5 , 32 in channels ;64 out channels; padding style is same; stride is 1# line 2 : choosing your activation funciont# line 3 : pooling operation function.nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, padding=2, stride=1),nn.ReLU(),nn.AvgPool2d(2),)self.out1 = nn.Linear( 7*7*64 , 1024 , bias= True)   # full connection layer oneself.dropout = nn.Dropout(keep_prob_rate)self.out2 = nn.Linear(1024,10,bias=True)def forward(self, x):x = self.conv1(x)x = self.conv2(x)x = x.view(-1, 7*7*64)  # flatten the output of coonv2 to (batch_size ,32 * 7 * 7)    # ???out1 = self.out1(x)out1 = F.relu(out1)out1 = self.dropout(out1)out2 = self.out2(out1)output = F.softmax(out2)return outputdef test(cnn):global predictiony_pre = cnn(test_x)_,pre_index= torch.max(y_pre,1)pre_index= pre_index.view(-1)prediction = pre_index.data.numpy()correct  = np.sum(prediction == test_y)return correct / 500.0def train(cnn):optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate )loss_func = nn.CrossEntropyLoss()for epoch in range(max_epoch):for step, (x_, y_) in enumerate(train_loader):x ,y= Variable(x_),Variable(y_)output = cnn(x)  loss = loss_func(output,y)optimizer.zero_grad()loss.backward()optimizer.step()if step != 0 and step % 20 ==0:print("=" * 10,step,"="*5,"="*5, "test accuracy is ",test(cnn) ,"=" * 10 )if __name__ == '__main__':cnn = CNN()train(cnn)

训练结果为:

========== 20 ===== ===== test accuracy is  0.224 ==========
========== 40 ===== ===== test accuracy is  0.362 ==========
========== 60 ===== ===== test accuracy is  0.402 ==========
========== 80 ===== ===== test accuracy is  0.51 ==========
========== 100 ===== ===== test accuracy is  0.608 ==========
========== 120 ===== ===== test accuracy is  0.624 ==========
========== 140 ===== ===== test accuracy is  0.708 ==========
========== 160 ===== ===== test accuracy is  0.684 ==========
========== 180 ===== ===== test accuracy is  0.738 ==========
========== 200 ===== ===== test accuracy is  0.766 ==========
========== 220 ===== ===== test accuracy is  0.778 ==========
========== 240 ===== ===== test accuracy is  0.796 ==========
========== 260 ===== ===== test accuracy is  0.802 ==========
========== 280 ===== ===== test accuracy is  0.81 ==========
========== 300 ===== ===== test accuracy is  0.812 ==========
========== 320 ===== ===== test accuracy is  0.82 ==========
========== 340 ===== ===== test accuracy is  0.848 ==========
========== 360 ===== ===== test accuracy is  0.83 ==========
========== 380 ===== ===== test accuracy is  0.852 ==========
========== 400 ===== ===== test accuracy is  0.852 ==========
========== 420 ===== ===== test accuracy is  0.856 ==========
========== 440 ===== ===== test accuracy is  0.874 ==========
========== 460 ===== ===== test accuracy is  0.85 ==========
========== 480 ===== ===== test accuracy is  0.874 ==========
========== 500 ===== ===== test accuracy is  0.864 ==========
========== 520 ===== ===== test accuracy is  0.858 ==========
========== 540 ===== ===== test accuracy is  0.884 ==========
========== 560 ===== ===== test accuracy is  0.872 ==========
========== 580 ===== ===== test accuracy is  0.9 ==========
========== 600 ===== ===== test accuracy is  0.88 ==========
========== 620 ===== ===== test accuracy is  0.886 ==========
========== 640 ===== ===== test accuracy is  0.882 ==========
========== 660 ===== ===== test accuracy is  0.886 ==========
========== 680 ===== ===== test accuracy is  0.876 ==========
========== 700 ===== ===== test accuracy is  0.882 ==========
========== 720 ===== ===== test accuracy is  0.886 ==========
========== 740 ===== ===== test accuracy is  0.894 ==========
========== 760 ===== ===== test accuracy is  0.894 ==========
========== 780 ===== ===== test accuracy is  0.9 ==========
========== 800 ===== ===== test accuracy is  0.898 ==========
========== 820 ===== ===== test accuracy is  0.912 ==========
========== 840 ===== ===== test accuracy is  0.894 ==========
========== 860 ===== ===== test accuracy is  0.898 ==========
========== 880 ===== ===== test accuracy is  0.888 ==========
========== 900 ===== ===== test accuracy is  0.896 ==========
========== 920 ===== ===== test accuracy is  0.888 ==========
========== 940 ===== ===== test accuracy is  0.91 ==========
========== 960 ===== ===== test accuracy is  0.908 ==========
========== 980 ===== ===== test accuracy is  0.918 ==========
========== 1000 ===== ===== test accuracy is  0.906 ==========
========== 1020 ===== ===== test accuracy is  0.908 ==========
========== 1040 ===== ===== test accuracy is  0.906 ==========
========== 1060 ===== ===== test accuracy is  0.914 ==========
========== 1080 ===== ===== test accuracy is  0.908 ==========
========== 1100 ===== ===== test accuracy is  0.906 ==========
========== 1120 ===== ===== test accuracy is  0.906 ==========
========== 1140 ===== ===== test accuracy is  0.924 ==========
========== 1160 ===== ===== test accuracy is  0.918 ==========
========== 1180 ===== ===== test accuracy is  0.904 ==========
========== 20 ===== ===== test accuracy is  0.924 ==========
========== 40 ===== ===== test accuracy is  0.908 ==========
========== 60 ===== ===== test accuracy is  0.92 ==========
========== 80 ===== ===== test accuracy is  0.91 ==========
========== 100 ===== ===== test accuracy is  0.926 ==========
========== 120 ===== ===== test accuracy is  0.91 ==========
========== 140 ===== ===== test accuracy is  0.922 ==========
========== 160 ===== ===== test accuracy is  0.932 ==========
========== 180 ===== ===== test accuracy is  0.932 ==========
========== 200 ===== ===== test accuracy is  0.93 ==========
========== 220 ===== ===== test accuracy is  0.94 ==========
========== 240 ===== ===== test accuracy is  0.918 ==========
========== 260 ===== ===== test accuracy is  0.934 ==========
========== 280 ===== ===== test accuracy is  0.93 ==========
========== 300 ===== ===== test accuracy is  0.934 ==========
========== 320 ===== ===== test accuracy is  0.934 ==========
========== 340 ===== ===== test accuracy is  0.93 ==========
========== 360 ===== ===== test accuracy is  0.944 ==========
========== 380 ===== ===== test accuracy is  0.938 ==========
========== 400 ===== ===== test accuracy is  0.92 ==========
========== 420 ===== ===== test accuracy is  0.936 ==========
========== 440 ===== ===== test accuracy is  0.948 ==========
========== 460 ===== ===== test accuracy is  0.934 ==========
========== 480 ===== ===== test accuracy is  0.938 ==========
========== 500 ===== ===== test accuracy is  0.916 ==========
========== 520 ===== ===== test accuracy is  0.916 ==========
========== 540 ===== ===== test accuracy is  0.928 ==========
========== 560 ===== ===== test accuracy is  0.936 ==========
========== 580 ===== ===== test accuracy is  0.942 ==========
========== 600 ===== ===== test accuracy is  0.922 ==========
========== 620 ===== ===== test accuracy is  0.94 ==========
========== 640 ===== ===== test accuracy is  0.94 ==========
========== 660 ===== ===== test accuracy is  0.96 ==========
========== 680 ===== ===== test accuracy is  0.938 ==========
========== 700 ===== ===== test accuracy is  0.936 ==========
========== 720 ===== ===== test accuracy is  0.94 ==========
========== 740 ===== ===== test accuracy is  0.946 ==========
========== 760 ===== ===== test accuracy is  0.946 ==========
========== 780 ===== ===== test accuracy is  0.948 ==========
========== 800 ===== ===== test accuracy is  0.95 ==========
========== 820 ===== ===== test accuracy is  0.948 ==========
========== 840 ===== ===== test accuracy is  0.95 ==========
========== 860 ===== ===== test accuracy is  0.94 ==========
========== 880 ===== ===== test accuracy is  0.956 ==========
========== 900 ===== ===== test accuracy is  0.944 ==========
========== 920 ===== ===== test accuracy is  0.948 ==========
========== 940 ===== ===== test accuracy is  0.95 ==========
========== 960 ===== ===== test accuracy is  0.944 ==========
========== 980 ===== ===== test accuracy is  0.94 ==========
========== 1000 ===== ===== test accuracy is  0.946 ==========
========== 1020 ===== ===== test accuracy is  0.952 ==========
========== 1040 ===== ===== test accuracy is  0.952 ==========
========== 1060 ===== ===== test accuracy is  0.944 ==========
========== 1080 ===== ===== test accuracy is  0.956 ==========
========== 1100 ===== ===== test accuracy is  0.96 ==========
========== 1120 ===== ===== test accuracy is  0.948 ==========
========== 1140 ===== ===== test accuracy is  0.942 ==========
========== 1160 ===== ===== test accuracy is  0.948 ==========
========== 1180 ===== ===== test accuracy is  0.944 ==========
========== 20 ===== ===== test accuracy is  0.952 ==========
========== 40 ===== ===== test accuracy is  0.96 ==========
========== 60 ===== ===== test accuracy is  0.948 ==========
========== 80 ===== ===== test accuracy is  0.954 ==========
========== 100 ===== ===== test accuracy is  0.948 ==========
========== 120 ===== ===== test accuracy is  0.948 ==========
========== 140 ===== ===== test accuracy is  0.958 ==========
========== 160 ===== ===== test accuracy is  0.942 ==========
========== 180 ===== ===== test accuracy is  0.948 ==========
========== 200 ===== ===== test accuracy is  0.952 ==========
========== 220 ===== ===== test accuracy is  0.952 ==========
========== 240 ===== ===== test accuracy is  0.95 ==========
========== 260 ===== ===== test accuracy is  0.966 ==========
========== 280 ===== ===== test accuracy is  0.96 ==========
========== 300 ===== ===== test accuracy is  0.956 ==========
========== 320 ===== ===== test accuracy is  0.96 ==========
========== 340 ===== ===== test accuracy is  0.956 ==========
========== 360 ===== ===== test accuracy is  0.956 ==========
========== 380 ===== ===== test accuracy is  0.954 ==========
========== 400 ===== ===== test accuracy is  0.96 ==========
========== 420 ===== ===== test accuracy is  0.966 ==========
========== 440 ===== ===== test accuracy is  0.96 ==========
========== 460 ===== ===== test accuracy is  0.954 ==========
========== 480 ===== ===== test accuracy is  0.968 ==========
========== 500 ===== ===== test accuracy is  0.958 ==========
========== 520 ===== ===== test accuracy is  0.958 ==========
========== 540 ===== ===== test accuracy is  0.962 ==========
========== 560 ===== ===== test accuracy is  0.968 ==========
========== 580 ===== ===== test accuracy is  0.958 ==========
========== 600 ===== ===== test accuracy is  0.952 ==========
========== 620 ===== ===== test accuracy is  0.95 ==========
========== 640 ===== ===== test accuracy is  0.964 ==========
========== 660 ===== ===== test accuracy is  0.962 ==========
========== 680 ===== ===== test accuracy is  0.96 ==========
========== 700 ===== ===== test accuracy is  0.962 ==========
========== 720 ===== ===== test accuracy is  0.964 ==========
========== 740 ===== ===== test accuracy is  0.958 ==========
========== 760 ===== ===== test accuracy is  0.96 ==========
========== 780 ===== ===== test accuracy is  0.972 ==========
========== 800 ===== ===== test accuracy is  0.962 ==========
========== 820 ===== ===== test accuracy is  0.968 ==========
========== 840 ===== ===== test accuracy is  0.964 ==========
========== 860 ===== ===== test accuracy is  0.96 ==========
========== 880 ===== ===== test accuracy is  0.964 ==========
========== 900 ===== ===== test accuracy is  0.96 ==========
========== 920 ===== ===== test accuracy is  0.96 ==========
========== 940 ===== ===== test accuracy is  0.97 ==========
========== 960 ===== ===== test accuracy is  0.956 ==========
========== 980 ===== ===== test accuracy is  0.966 ==========
========== 1000 ===== ===== test accuracy is  0.964 ==========
========== 1020 ===== ===== test accuracy is  0.964 ==========
========== 1040 ===== ===== test accuracy is  0.97 ==========
========== 1060 ===== ===== test accuracy is  0.974 ==========
========== 1080 ===== ===== test accuracy is  0.962 ==========
========== 1100 ===== ===== test accuracy is  0.97 ==========
========== 1120 ===== ===== test accuracy is  0.974 ==========
========== 1140 ===== ===== test accuracy is  0.978 ==========
========== 1160 ===== ===== test accuracy is  0.976 ==========
========== 1180 ===== ===== test accuracy is  0.974 ==========

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/20322.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

商用未来何时来?软银揭示量子计算商业应用现状

内容来源:量子前哨(ID:Qforepost) 文丨沛贤/浪味仙 排版丨沛贤 深度好文:3000字丨10分钟阅读 摘要:软银(SoftBank)先进技术研究所正在积极推进量子计算商业应用,借助与…

SpringCloud Feign用法

1.在目标应用的启动类上添加开启远程fein调用注解: 2.添加一个feign调用的interface FeignClient("gulimall-coupon") public interface CouponFeignService {PostMapping("/coupon/spubounds/save")R save(RequestBody SpuBondTo spuBounds);…

随记-点选验证码(二)

之前写过一篇文章 随记-点选验证码 ,当时借助了 ddddocr 完成了ocr 识别,这篇文章算是对之前的补充。 本次更换了新的方案: 通过 ultralytics(YOLO8)训练自己的模型 吐槽一句:标注真是一件耗时的事情啊&am…

鸿蒙实现汉字转拼音

1.使用三方库 pinyin-pro 地址:OpenHarmony三方库中心仓 亲测可用,一共三个关于 转pinyin的库,一个无法使用,另一个时间太久。 ohpm i pinyin-proimport { pinyin } from pinyin-pro;// 获取带音调拼音 pinyin(汉语拼音); // …

【Java数据结构】详解LinkedList与链表(一)

🔒文章目录: 1.❤️❤️前言~🥳🎉🎉🎉 2.ArrayList的缺陷 3.链表的概念及结构 4.无头单向非循环链表的实现 4.1成员属性 4.2成员方法 createList display——打印链表 addFirst——头插 addLast…

少样本学习与零样本学习:理解与应用

少样本学习与零样本学习:理解与应用 在现代机器学习领域中,少样本学习(Few-Shot Learning)和零样本学习(Zero-Shot Learning)正变得越来越重要。这些技术能够在数据稀缺的情况下有效地进行学习和推理&…

2024就业寒潮下的挑战与机遇:能否守住饭碗,人工智能能否成为新春天?

前言 随着时代的飞速发展,2024年的就业市场迎来了前所未有的挑战。数以百万计的高校毕业生涌入市场,使得就业竞争愈发激烈。然而,在这股就业寒潮中,我们也看到了新的曙光——人工智能的崛起。这一新兴行业以其独特的魅力和巨大的…

深入分析 Android Service (五)

文章目录 深入分析 Android Service (五)1. 深入分析 Service 与 Activity 之间的通信2. Messenger 的内部工作原理2.1 服务端实现2.2 客户端实现 3. AIDL 的内部工作原理3.1 定义 AIDL 接口3.2 服务端实现3.3 客户端实现 4. Service 的优化建议和最佳实践4.1 异步操作4.2 资源…

【Linux】权限的概念

1.Linux权限的概念 Linux下有两种用户:超级用户(root)、普通用户。 超级用户:可以再linux系统下做任何事情,不受权限限制 普通用户:在linux下做有限的事情,受权限设置。 windows下也有超级用户…

Object.entries方法的使用

Object.entries() 方法返回一个给定对象自身可枚举属性的键值对数组。 有以下需求&#xff1a; let cpuData reactive([{ label: 总量, content: test },{ label: 已使用, content: test },{ label: 未使用, content: test } ])<el-form label-position"left" l…

环卫车北斗GPS视频监控定位解决方案的应用与优势

一、引言 随着城市化进程的加快&#xff0c;环卫车作为城市环境卫生的重要保障力量&#xff0c;其运行效率与安全性直接关系到城市形象与居民生活品质。然而&#xff0c;传统的环卫车管理模式往往存在信息不对称、调度不合理、行驶不规范等问题&#xff0c;导致城市道路污染和…

微信小程序对接发货功能

注&#xff1a;微信小程序对接发货功能 文档地址&#xff1a;https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html php代码 common.php use think\Config; use think\Db; use fast\Http; us…

LabVIEW远程开发与调试

在现代项目开发中&#xff0c;远程开发与调试已经成为一种常见的模式&#xff0c;特别是在使用LabVIEW进行工程项目时。本文将详细分析LabVIEW远程开发与调试的优缺点&#xff0c;并从多个角度说明如何建议客户采用这种方式&#xff0c;以提高项目效率和质量。 优点 灵活性和便…

Linux【安全 02】OpenSSH漏洞修复(离线升级最新版本流程)网盘分享3个安装包+26个离线依赖

OpenSSH离线升级最新版本流程 1. 漏洞信息2. 环境说明3.依赖安装3.1 在线安装3.2 离线安装 4.备份卸载4.1 备份4.2 卸载旧版本 5.安装5.1 zlib5.2 ssl5.3 openssh5.3.1 安装5.3.2 配置 6.脚本整理7.文件资源 本文仅针对CentOS7.8版本&#xff0c;其他版本未测试&#xff0c;安装…

GSM信令流程(附着、去附着、PDP激活、修改流程)

1、联合附着流程 附着包括身份认证、鉴权等 2、去附着流程 用户发起去附着 SGSN发起去附着 HLR发起去附着 GSSN使用S4发起去附着 3、Activation Procedures(PDP激活流程) 4、PDP更新或修改流程 5、Deactivate PDP Context 6、RAU(Routeing Area Update)流程 7、鉴权加…

生成式AI,在云端的绽放与盛开

编辑&#xff1a;阿冒 设计&#xff1a;沐由 毫无疑问&#xff0c;生成式AI已然成为当今技术发展和应用创新的重要引擎之一。 过去的一年多时间里&#xff0c;我们每个人都在目睹和见证着生成式AI是如何以移山倒海的力量&#xff0c;为诸多行业带来革命性乃至颠覆性的变革&…

新版校园跑腿外卖独立版+APP+小程序前端外卖配送平台源码

同城校园跑腿外卖配送平台源码&#xff0c;这套目前全网还没有人分享过&#xff0c;这个是开源的&#xff0c;所以没有任何问题了&#xff0c;这套源码非常吊&#xff0c;支持自定义diy 你可以设计你的页面&#xff0c;设计你自己的风格&#xff0c;支持多校园&#xff0c;独立…

深度学习中的模型架构详解:RNN、LSTM、TextCNN和Transformer

深度学习中的模型架构详解&#xff1a;RNN、LSTM、TextCNN和Transformer 文章目录 深度学习中的模型架构详解&#xff1a;RNN、LSTM、TextCNN和Transformer循环神经网络 (RNN)RNN的优点RNN的缺点RNN的代码实现 长短期记忆网络 (LSTM)LSTM的优点LSTM的缺点LSTM的代码实现 TextCN…

mac电脑安卓设备文件传输助手:MacDroid pro 中文激活版

MacDroid Pro是一款专为Mac电脑和Android设备设计的软件&#xff0c;旨在简化两者之间的文件传输和数据管理&#xff0c;双向文件传输&#xff1a;支持从Mac电脑向Android设备传输文件&#xff0c;也可以将Android设备上的文件轻松传输到Mac电脑上。完整的文件访问和管理&#…

机器学习笔记 - PyTorch 分布式训练概览

一、简述 对于大规模的数据集,只能进行分布式训练,分布式训练会尽可能的利用我们的算力,使模型训练更加高效。PyTorch提供了Data Parallel包,它可以实现单机、多GPU并行。 PyTorch 数据并行模块的内部工作原理 上面的图像说明了PyTorch 如何在单个系统中利用多个 G…