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.rhkb.cn/news/337040.html

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

相关文章

gcc 内建函数示例 __builtin_return_address

1,理论未动&#xff0c;示例先行 hello_gcc_callstack.c #include <stdio.h>void do_backtrace() {void *pc0 __builtin_return_address(0);void *pc1 __builtin_return_address(1);void *pc2 __builtin_return_address(2);void *pc3 __builtin_return_address(3);…

低边驱动与高边驱动

一.高边驱动和低边驱动 低边驱动(LSD): 在电路的接地端加了一个可控开关&#xff0c;低边驱动就是通过闭合地线来控制这个开关的开关。容易实现&#xff08;电路也比较简单&#xff0c;一般由MOS管加几个电阻、电容&#xff09;、适用电路简化和成本控制的情况。 高边驱动&am…

JVM哪些区域可能出现内存溢出,哪些地方需要GC?

GC顾名思义也就是垃圾回收&#xff0c;有人的地方就有江湖&#xff0c;那有数据的地方也理应有垃圾回收&#xff0c;所以思考一下&#xff0c;沿着之前提到过的JVM内存分区&#xff0c;堆&#xff0c;栈&#xff0c;程序计数器&#xff0c;方法区 堆、栈、方法区…

一键安装 HaloDB 之 Ansible for Halo

↑ 关注“少安事务所”公众号&#xff0c;欢迎⭐收藏&#xff0c;不错过精彩内容~ 前倾回顾 前面介绍了“光环”数据库的基本情况和安装办法。 哈喽&#xff0c;国产数据库&#xff01;Halo DB! 三步走&#xff0c;Halo DB 安装指引 以及 HaloDB 的 Oracle 和 MySQL 兼容模式: …

ChatGPT-4o 有何特别之处?

文章目录 多模态输入&#xff0c;多模态输出之前的模型和现在模型对比 大家已经知道&#xff0c;OpenAI 在 GPT-4 发布一年多后终于推出了一个新模型。它仍然是 GPT-4 的一个变体&#xff0c;但具有前所未见的多模态功能。 有趣的是&#xff0c;它包括实时视频处理等强大功能&…

Mac安装第三方软件的命令安装方式

场景&#xff1a; 打开终端命令行&#xff0c;sudo xattr -rd com.apple.quarantine&#xff0c;注意最后quarantine 后面加一个空格&#xff01;然后打开Finder&#xff08;访达&#xff09;&#xff0c;点击左侧的 应用程序&#xff0c;找到相关应用&#xff0c;拖进终端qua…

六一见!|Post Microsoft Build and AI Day 上海开发者日

编辑/排版&#xff1a;Alan Wang 大小朋友明天见&#xff01; 6月1日&#xff0c;Microsoft Azure & Microsoft Reactor 面向大小朋友特别推出六一特辑&#xff0c;「Post Microsoft Build and AI Day 上海开发者日」 探讨 Microsoft Build 2024 带来的最新发布&#xff0…

KT6368A双模蓝牙芯片上电到正常发送AT指令或指令复位需要多久

一、简介 KT6368A芯片上电到正常发送AT指令&#xff0c;或者开启蓝牙广播被搜索到&#xff0c;或者指令复位需要多久等等系列问题总结 详细描述 其实这些问题归结到一起&#xff0c;就还是一个问题&#xff0c;芯片上电需要多久的时间 在另外一份文档里面&#xff0c;是有描…

热门新游 2024 植物大战僵尸杂交版 Mac 版本下载安装详细教程

最近植物大战僵尸杂交版可谓是非常的火&#xff0c;好多主播都在播这款游戏&#xff0c;我一个 Mac 党也想玩&#xff0c;可奈何该游戏目前只有 PC 版本&#xff0c;经过一番折腾终于在我的 Mac 上安装上了该游戏&#xff0c;分享给大家 其实安装过程也很简单&#xff0c;只需…

C++ | Leetcode C++题解之第119题杨辉三角II

题目&#xff1a; 题解&#xff1a; class Solution { public:vector<int> getRow(int rowIndex) {vector<int> row(rowIndex 1);row[0] 1;for (int i 1; i < rowIndex; i) {row[i] 1LL * row[i - 1] * (rowIndex - i 1) / i;}return row;} };

现在的时代,您必会的“调教”AI技巧。

人工智能大行其道&#xff0c;如何借势&#xff1f;始于问询。要得要得预期&#xff0c;精于“提问技巧”&#xff01; (笔记模板由python脚本于2024年05月30日 18:37:27创建&#xff0c;本篇笔记适合有独立编程基础的coder翻阅) 【学习的细节是欢悦的历程】 Python 官网&#…

通过定时器和脉冲控制LED

一、定时器 &#xff08;一&#xff09;定时器简介 STM32定时器是STM32微控制器中的重要块&#xff0c;用于生成精确的时间基准。它可以用于测量时间间隔、产生脉冲、实现定时中断等功能。通过配置寄存器&#xff0c;用户可以灵活地控制定时器的工作模式和参数&#xff0c;实现…

Linux自动挂载服务autofs讲解

1.产生原因 2.配置文件讲解 总结&#xff1a;配置客户端&#xff0c;先构思好要挂载的目录如&#xff1a;/abc/cb 然后在autofs.master中编辑&#xff1a; /abc&#xff08;要挂载的主目录&#xff09; /etc/qwe&#xff08;在这个文件里去找要挂载的副目录&#xff0c;这个名…

Linux虚拟机根目录磁盘扩容

一、VMWare虚拟机扩展磁盘空间 在vmware软件中&#xff0c;选择对应的虚拟机&#xff0c;点击“硬盘”。【需要先关机再操作】 扩展 更改磁盘大小&#xff0c;点击“扩展”&#xff0c;然后一路“确定”。扩展到45G 二、启动虚拟机并扩展磁盘空间 查看磁盘使用情况 df -Th …

2024最新群智能优化算法:大甘蔗鼠算法(Greater Cane Rat Algorithm,GCRA)求解23个函数,提供MATLAB代码

一、大甘蔗鼠算法 大甘蔗鼠算法&#xff08;Greater Cane Rat Algorithm&#xff0c;GCRA&#xff09;由Jeffrey O. Agushaka等人于2024年提出&#xff0c;该算法模拟大甘蔗鼠的智能觅食行为。 参考文献 [1]Agushaka J O, Ezugwu A E, Saha A K, et al. Greater Cane Rat Alg…

Docker安装Zookeeper(单机)

Docker安装Zookeeper&#xff08;单机&#xff09; 目录 Docker安装Zookeeper&#xff08;单机&#xff09;拉取镜像创建目录添加配置文件启动容器测试 拉取镜像 docker pull zookeeper创建目录 mkdir -p /data/zookeeper/data # 数据挂载目录 mkdir -p /data/zookeeper/conf…

【量算分析工具-坡度】GeoServer改造Springboot番外系列七

【量算分析工具-概述】GeoServer改造Springboot番外系列三-CSDN博客 【量算分析工具-水平距离】GeoServer改造Springboot番外系列四-CSDN博客 【量算分析工具-水平面积】GeoServer改造Springboot番外系列五-CSDN博客 【量算分析工具-方位角】GeoServer改造Springboot番外系列…

【机器学习】洞悉数据奥秘:决策树算法在机器学习中的魅力

在机器学习的分类和回归问题中&#xff0c;决策树是一种广泛使用的算法。决策树模型因其直观性、易于理解和实现&#xff0c;以及处理分类和数值特征的能力而备受欢迎。本文将解释决策树算法的概念、原理、应用、优化方法以及未来的发展方向。 &#x1f680;时空传送门 &#x…

5月安全月报 | 钓鱼事件频发,OKLink带你开启“防钓”模式

本月全网安全事件所造成的损失环比上升 27.27%&#xff0c;钓鱼与诈骗事件占比 60% 以上。 安全意识是您保护数字资产的第一道防线&#xff0c;OKLink 提供 40 头部区块链浏览器与一站式查询入口以及地址监控、代币授权查询和地址健康度等工具&#xff0c;为您的资产安全保驾护…

系统安全及应用11

一个新的服务器到手之后&#xff0c;部署服务器的初始化 1、配置IP地址 网关 dns解析&#xff08;static&#xff09;内网和外网 2、安装源外网&#xff08;在线即可&#xff09;&#xff0c;内网&#xff08;只能用源码包编译安装&#xff09; 3、磁盘分区&#xff0c;lvm …