帝国做的网站删除域名后缀,网站如何做se,网页链接 提取码:qqcd,流程图在线制作网站文章目录 一、模型实现1.1数据集的下载1.2加载数据集1.3模型训练1.4模型预测 LeNet神经网络是第一个卷积神经网络#xff08;CNN#xff09;#xff0c;首次采用了卷积层、池化层这两个全新的神经网络组件#xff0c;接收灰度图像#xff0c;并输出其中包含的手写数字CNN首次采用了卷积层、池化层这两个全新的神经网络组件接收灰度图像并输出其中包含的手写数字在手写字符识别任务上取得了瞩目的准确率。LeNet网络的一系列的版本以LeNet-5版本最为著名也是LeNet系列中效果最佳的版本。LeNet神经网络输入图像大小必须为32x32且所用卷积核大小固定为5x5模型结构如下
模型参数
INPUT输入层输入图像尺寸为32x32且是单通道灰色图像。C1卷积层使用6个5x5大小的卷积核步长为1卷积后得到6张28×28的特征图。S2池化层使用了6个2×2 的平均池化池化后得到6张14×14的特征图。C3卷积层使用了16个大小为5×5的卷积核步长为1得到 16 张10×10的特征图。S4池化层使用16个2×2的平均池化池化后得到16张5×5 的特征图。C5卷积层使用120个大小为5×5的卷积核步长为1卷积后得到120张1×1的特征图。F6全连接层输入维度120输出维度是84对应7x12 的比特图。OUTPUT输出层使用高斯核函数输入维度84输出维度是10对应数字 0 到 9。
该模型有如下特点
1.首次提出卷积神经网络基本框架 卷积层池化层全连接层。2.卷积层的权重共享相较于全连接层使用更少参数节省了计算量与内存空间。3.卷积层的局部连接保证图像的空间相关性。4.使用映射到空间均值下采样减少特征数量。5.使用双曲线tanh或S型sigmoid形式的非线性激活函数。
一、模型实现
1.1数据集的下载 使用torchversion内置的MNIST数据集训练集大小60000测试集大小10000图像大小是1×28×28包括数字0~9共10个类。
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
import torchvision
# 下载训练、测试数据集
mnist_train torchvision.datasets.MNIST(root./dataset/,trainTrue, downloadTrue, transformtransforms.ToTensor())
mnist_test torchvision.datasets.MNIST(root./dataset/,trainFalse, downloadTrue, transformtransforms.ToTensor())
print(mnist_train基本信息为,mnist_train)
print(-----------------------------------------)
print(mnist_test基本信息为,mnist_test)
print(-----------------------------------------)
img,labelmnist_train[0]
print(mnist_train[0]图像大小及标签为,img.shape,label)1.2加载数据集
trainDataLoader DataLoader(mnist_train, batch_size64, num_workers5, shuffleTrue)
testDataLoader DataLoader(mnist_test, batch_size64, num_workers0, shuffleTrue)
write SummaryWriter(./log)
step 0
for images, labels in testDataLoader:write.add_images(tagtrain, images, global_stepstep)step 1
write.close()注意不能使用for images, labels in testDataLoader.datasettestDataLoader.dataset[0]是保存图像28 28和对应标签的元组而Tensorboard的add_images只能输入NCHW格式对象使用该代码会报错
size of input tensor and input format are different. tensor shape: (1, 28, 28), input_format: NCHW数据加载器按batch_size对数据及标签进行封装名可直接作为输入。查看封装的元组
for data in testDataLoader:print(type(data),type(data))img,labeldataprint(type(img),type(img),img.shape,img.shape)print(type(label),type(label),label.shape,label.shape)1.3模型训练 LeNet模型的输入为3232的图片而MNIST数据集为2828的图片故需对原图片进行填充。搭建模型
class LeNet(nn.Module):def __init__(self):super(LeNet, self).__init__()self.model nn.Sequential( #MNIST数据集图像大小为28x28而LeNet输入为32x32故需填充nn.Conv2d(in_channels1, out_channels6, kernel_size5, stride1, padding2), #C1层共六个卷积核故out_channels6nn.AvgPool2d(kernel_size2, stride2), #C2层使用平均池化nn.Conv2d(in_channels6, out_channels16, kernel_size5),nn.AvgPool2d(kernel_size2, stride2),nn.Flatten(),nn.Conv2d(in_channels16 * 5 * 5, out_channels120),nn.Linear(in_features120, out_features84),nn.Linear(in_features84, out_features10))def forward(self, x):return self.model(x)# 初始化模型对象
myLeNet LeNet()设置损失函数、优化器并训练模型
# 设置损失函数为交叉熵损失函数
loss_fn nn.CrossEntropyLoss()
loss_fn loss_fn.to(device)
# 设置优化器使用Adam优化算法
learning_rate 1e-2
optimizer torch.optim.Adam(myLeNet.parameters(), lrlearning_rate)
total_train_step 0 # 总训练次数
epoch 10 # 训练轮数
writer SummaryWriter(log_dir./runs/LeNet/)
for i in range(epoch):print(-----第{}轮训练开始-----.format(i 1))myLeNet.train() # 训练模式train_loss 0for data in trainDataLoader:imgs, labels dataimgs imgs.to(device) # 适配GPU/CPUlabels labels.to(device)outputs myLeNet(imgs)loss loss_fn(outputs, labels)#计算损失函数optimizer.zero_grad() # 清空之前梯度loss.backward() # 反向传播optimizer.step() # 更新参数total_train_step 1 # 更新步数train_loss loss.item()writer.add_scalar(train_loss_detail, loss.item(), total_train_step)writer.add_scalar(train_loss_total, train_loss, i 1)writer.close()1.4模型预测
myLeNet.eval()
total_test_loss 0 # 当前轮次模型测试所得损失
total_accuracy 0 # 当前轮次精确率
with torch.no_grad(): # 关闭梯度反向传播for data in testDataLoader:imgs, targets dataimgs imgs.to(device)targets targets.to(device)outputs myLeNet(imgs)loss loss_fn(outputs, targets)total_test_loss total_test_loss loss.item()accuracy (outputs.argmax(1) targets).sum()total_accuracy total_accuracy accuracy
writer.add_scalar(test_loss, total_test_loss, i1)
writer.add_scalar(test_accuracy, total_accuracy/len(mnist_test), i1)https://blog.csdn.net/qq_43307074/article/details/126022041?ops_request_misc%257B%2522request%255Fid%2522%253A%2522171938503416800186515588%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257Drequest_id171938503416800186515588biz_id0utm_mediumdistribute.pc_search_result.none-task-blog-2alltop_click~default-2-126022041-null-null.142v100pc_search_result_base3utm_termLeNetspm1018.2226.3001.4187
https://blog.csdn.net/hellocsz/article/details/80764804?ops_request_miscrequest_idbiz_id102utm_termLeNetutm_mediumdistribute.pc_search_result.none-task-blog-2allsobaiduweb~default-1-80764804.142v100pc_search_result_base3spm1018.2226.3001.4187
https://blog.csdn.net/qq_45034708/article/details/128319241?ops_request_misc%257B%2522request%255Fid%2522%253A%2522171936257316800222847105%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257Drequest_id171936257316800222847105biz_id0utm_mediumdistribute.pc_search_result.none-task-blog-2alltop_positive~default-1-128319241-null-null.142v100pc_search_result_base3utm_termLeNetspm1018.2226.3001.4187 文章转载自: http://www.morning.lwtfx.cn.gov.cn.lwtfx.cn http://www.morning.gnhsg.cn.gov.cn.gnhsg.cn http://www.morning.kflpf.cn.gov.cn.kflpf.cn http://www.morning.lcqrf.cn.gov.cn.lcqrf.cn http://www.morning.hengqilan.cn.gov.cn.hengqilan.cn http://www.morning.wjdgx.cn.gov.cn.wjdgx.cn http://www.morning.24vy.com.gov.cn.24vy.com http://www.morning.yxzfl.cn.gov.cn.yxzfl.cn http://www.morning.pdghl.cn.gov.cn.pdghl.cn http://www.morning.zhnyj.cn.gov.cn.zhnyj.cn http://www.morning.qkkmd.cn.gov.cn.qkkmd.cn http://www.morning.zxzgr.cn.gov.cn.zxzgr.cn http://www.morning.wkcl.cn.gov.cn.wkcl.cn http://www.morning.nzmqn.cn.gov.cn.nzmqn.cn http://www.morning.kmkpm.cn.gov.cn.kmkpm.cn http://www.morning.ckntb.cn.gov.cn.ckntb.cn http://www.morning.sjjtz.cn.gov.cn.sjjtz.cn http://www.morning.swsrb.cn.gov.cn.swsrb.cn http://www.morning.kdxzy.cn.gov.cn.kdxzy.cn http://www.morning.yfnhg.cn.gov.cn.yfnhg.cn http://www.morning.rwhlf.cn.gov.cn.rwhlf.cn http://www.morning.plkrl.cn.gov.cn.plkrl.cn http://www.morning.zdhnm.cn.gov.cn.zdhnm.cn http://www.morning.rjrnx.cn.gov.cn.rjrnx.cn http://www.morning.xhrws.cn.gov.cn.xhrws.cn http://www.morning.tzcr.cn.gov.cn.tzcr.cn http://www.morning.wjlrw.cn.gov.cn.wjlrw.cn http://www.morning.nzcgj.cn.gov.cn.nzcgj.cn http://www.morning.tzmjc.cn.gov.cn.tzmjc.cn http://www.morning.myrmm.cn.gov.cn.myrmm.cn http://www.morning.qhkx.cn.gov.cn.qhkx.cn http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn http://www.morning.fhtmp.cn.gov.cn.fhtmp.cn http://www.morning.qlry.cn.gov.cn.qlry.cn http://www.morning.sh-wj.com.cn.gov.cn.sh-wj.com.cn http://www.morning.nylbb.cn.gov.cn.nylbb.cn http://www.morning.lwbhw.cn.gov.cn.lwbhw.cn http://www.morning.jrtjc.cn.gov.cn.jrtjc.cn http://www.morning.nhdw.cn.gov.cn.nhdw.cn http://www.morning.ydtdn.cn.gov.cn.ydtdn.cn http://www.morning.xdmsq.cn.gov.cn.xdmsq.cn http://www.morning.ybhjs.cn.gov.cn.ybhjs.cn http://www.morning.xpqsk.cn.gov.cn.xpqsk.cn http://www.morning.dbqcw.com.gov.cn.dbqcw.com http://www.morning.drggr.cn.gov.cn.drggr.cn http://www.morning.blfgh.cn.gov.cn.blfgh.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.nzkkh.cn.gov.cn.nzkkh.cn http://www.morning.qbgff.cn.gov.cn.qbgff.cn http://www.morning.klltg.cn.gov.cn.klltg.cn http://www.morning.gpsrk.cn.gov.cn.gpsrk.cn http://www.morning.mkzdp.cn.gov.cn.mkzdp.cn http://www.morning.sphft.cn.gov.cn.sphft.cn http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn http://www.morning.xbckm.cn.gov.cn.xbckm.cn http://www.morning.rggky.cn.gov.cn.rggky.cn http://www.morning.gqmhq.cn.gov.cn.gqmhq.cn http://www.morning.ydrml.cn.gov.cn.ydrml.cn http://www.morning.fjptn.cn.gov.cn.fjptn.cn http://www.morning.kfwqd.cn.gov.cn.kfwqd.cn http://www.morning.cpmwg.cn.gov.cn.cpmwg.cn http://www.morning.mphfn.cn.gov.cn.mphfn.cn http://www.morning.qqpg.cn.gov.cn.qqpg.cn http://www.morning.jzfxk.cn.gov.cn.jzfxk.cn http://www.morning.cpkcq.cn.gov.cn.cpkcq.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.kxbry.cn.gov.cn.kxbry.cn http://www.morning.hwnnh.cn.gov.cn.hwnnh.cn http://www.morning.ndxss.cn.gov.cn.ndxss.cn http://www.morning.yrfxb.cn.gov.cn.yrfxb.cn http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn http://www.morning.hncrc.cn.gov.cn.hncrc.cn http://www.morning.haolipu.com.gov.cn.haolipu.com http://www.morning.nkyc.cn.gov.cn.nkyc.cn http://www.morning.lqgfm.cn.gov.cn.lqgfm.cn http://www.morning.krqhw.cn.gov.cn.krqhw.cn http://www.morning.ckrnq.cn.gov.cn.ckrnq.cn http://www.morning.fypgl.cn.gov.cn.fypgl.cn http://www.morning.pbpcj.cn.gov.cn.pbpcj.cn http://www.morning.sdecsd.cn.gov.cn.sdecsd.cn