【深度学习笔记2.1】LeNet-5

2024-06-06 05:58
文章标签 学习 笔记 深度 2.1 lenet

本文主要是介绍【深度学习笔记2.1】LeNet-5,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

概述

LeNet-5中的-5是个啥?

Gradient-Based Learning Applied to Document Recognition

enter image description here

图1 [3]
![enter image description here](https://lh3.googleusercontent.com/-KPfsR5nep9A/W2rbZF4xk-I/AAAAAAAAAFc/PtinL8z9rCA0Pzf_GiovJ7kS8zRkm7nrACLcBGAs/s0/lenet5_2.png "lenet5_2.png")
图2 [2]

Input:shape=[-1, 28, 28, 1]
  | 
  | 
  |  filter.shape = [5, 5, 1, 6]
  |  C1 = tf.nn.conv2d(Input, filter, strides=[1,1,1,1], padding=‘SAME’)
  | 
  |  conv2d后C1层feature maps的shape为[-1, 28, 28, 6]
  | 
  |  参数个数:6x(5x5+1)=156
  |      一个卷积核的大小为5x5,每个卷积核有5x5个参数,每个卷积核做完所有卷积后还要与一个bias相加,故每个卷积核对应有5x5+1个参数;从Input到C1一共有6个卷积核,所以从Input到C1共有6x(5x5+1)个参数需要训练;(疑问:能否一个卷积核做完一次卷积后就和一个bias相加?)
  |  连接个数:6x(5x5+1)x28x28=122304
  |      一个卷积核每做完一次卷积后都会在C1层生成一个像素,该像素对应着(5x5+1)个连接,又C1层每个通道有28x28个像素,故C1层每个通道有(5x5+1)x28x28个连接;又C1层有6个通道,故从Input到C1层一共有6x(5x5+1)x28x28个连接;
  |      疑问:根据图2可知(从文献[1]以及网上的很多示例代码也能看出),这里的一个卷积核是和整个Input做完卷积后再和一个bias相加的,那么连接的个数不应该是6x(5x5x28x28+1)吗?
  |      答:一个卷积核是和整个Input做完卷积后得到的是一个28x28的feature map,该feature map加上一个数值bias可以等价于feature map的每个像素都加上一个bias,所以一个大小为28x28的feature map的每个像素都会和bias相加。
  | 
C1 Layer:shape=[-1, 28, 28, 6]
  | 
  | 
  |  ksize=[1,2,2,1]
  |  bias1 = tf.Variable( tf.truncated_normal( [6] ) )
  |  S2 = tf.nn.max_pool(tf.nn.sigmoid(C1 + bias1), ksize, strides=[1, 2, 2, 1], padding=‘SAME’)
  | 
  |  参数个数:6x(1+1)=12; 1个训练参数w,一个偏置b
  |      C1层的2x2感受野的四个输入相加,然后乘以一个可训练参数,再加上一个可训练偏置。结果通过 sigmoid 函数计算。可训练系数和偏置控制着 sigmoid 函数的非线性程度。如果系数比较小,那么运算近似于线性运算,亚采样相当于模糊图像。如果系数比较大,根据偏置的大小亚采样可以被看成是有噪声的“或”运算或者有噪声的“与”运算。
  |  连接个数:6x(4+1)x14x14=5880
  |      从一个平面到下一个平面的映射可以看作是作卷积运算,S-层可看作是模糊滤波器,起到二次特征提取的作用。隐层与隐层之间空间分辨率递减,而每层所含的平面数递增,这样可用于检测更多的特征信息[2]。
  |      问:按照很多文章介绍说的,那么程序应该是下面这样的吧:
  |        c1 = conv2d( input, filter, … ) + bias;
  |        s2 = sigmoid( pooling( c1, pool_filter, … ) + bias );
  |      但是实际上在很多程序具体实现的过程中却是下面这样的:
  |        c1 = conv2d( input, filter, … );
  |        s2 = pooling( sigmoid( c1 + bias ) );
  |      这是为什么?
  | 
S2 Layer:shape=[-1, 14, 14, 6]
  | 
  | 
  |  filter.shape = [5, 5, 6, 16]
  |  C3 = tf.nn.conv2d(S2, filter, strides=[1, 1, 1, 1], padding=‘VALID’)
  | 
  |  参数个数:6x(3x5x5+1)+6x(4x5x5+1)+3x(4x5x5+1)+1x(6x5x5+1)=1516
  |  连接个数:由于C3 Layer图像大小为10x10,所以共有151600个参数;
  | 
C3 Layer:shape=[-1, 10, 10, 16]
  | 
  | 
  |  ksize=[1,2,2,1]
  |  bias2 = tf.Variable(tf.truncated_normal([16]))
  |  S4 = tf.nn.max_pool(tf.nn.sigmoid(C3 + bias2), ksize, strides=[1, 2, 2, 1], padding=‘SAME’)
  | 
S4 Layer:shape=[-1, 5, 5, 16]
  | 
  | 
  |  filter.shape=[5, 5, 16, 120]
  |  C5 = tf.nn.conv2d(S4, filter, strides=[1, 1, 1, 1], padding=‘SAME’)
  | 
C5 Layer:shape=[-1, 5, 5, 120]
  | 
  | 
  |  C5_flat = tf.reshape( C5, [-1, 5 * 5 * 120] )
  |  W_fc1 = tf.Variable( tf.truncated_normal( [5 * 5 * 120, 84]) )
  |  b_fc1 = tf.Variable( tf.truncated_normal( [84] ) )
  |  h_fc1 = tf.nn.sigmoid( tf.matmul( C5_flat, W_fc1 ) + b_fc1)
  |  参数个数:84x120+84=10164
  | 
F6 Layer:全连接层
  | 
  | 
  |  W_fc2 = tf.Variable( tf.truncated_normal( [80, 10] ) )
  |  b_fc2 = tf.Variable( tf.truncated_normal( [10] ) )
  |  y_conv = tf.nn.softmax( tf.matmul( h_fc1, W_fc2 ) + b_fc2 )
  | 
Output Layer

代码示例

代码参考文献[1] 程序13.10

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import timedatapath = '/home/xiajun/res/MNIST_data'
mnist_data_set = input_data.read_data_sets(datapath, validation_size=0, one_hot=True)x = tf.placeholder('float', [None, 784])
y_ = tf.placeholder('float', [None, 10])
x_image = tf.reshape(x, [-1, 28, 28, 1])#第一层卷积层,初始化卷积核参数、偏置值,该卷积层5*5大小,1个通道,共有6个不同卷积核
filter1 = tf.Variable(tf.truncated_normal([5, 5, 1, 6]))
bias1 = tf.Variable(tf.truncated_normal([6]))
conv1 = tf.nn.conv2d(x_image, filter1, strides=[1, 1, 1, 1], padding='SAME')
# 此时conv1.shape = [-1, 28, 28, 6]
h_conv1 = tf.nn.sigmoid(conv1 + bias1)
# h_conv1 = tf.nn.relu(conv1 + bias1)maxPool2 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
# 此时maxPool2.shape = [-1, 14, 14, 6]filter2 = tf.Variable(tf.truncated_normal([5, 5, 6, 16]))
bias2 = tf.Variable(tf.truncated_normal([16]))
conv2 = tf.nn.conv2d(maxPool2, filter2, strides=[1, 1, 1, 1], padding='SAME')
# 此时conv2.shape = [-1, 14, 14, 16]
h_conv2 = tf.nn.sigmoid(conv2 + bias2)
# h_conv2 = tf.nn.relu(conv2 + bias2)maxPool3 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
# 此时maxPool3.shape = [-1, 7, 7, 16]filter3 = tf.Variable(tf.truncated_normal([5, 5, 16, 120]))
bias3 = tf.Variable(tf.truncated_normal([120]))
conv3 = tf.nn.conv2d(maxPool3, filter3, strides=[1, 1, 1, 1], padding='SAME')
# 此时conv3.shape = [-1, 7, 7, 120]
h_conv3 = tf.nn.sigmoid(conv3 + bias3)
# h_conv3 = tf.nn.relu(conv3 + bias3)# 全连接层
# 权值参数
W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 120, 80]))
# 偏置值
b_fc1 = tf.Variable(tf.truncated_normal([80]))
# 将卷积的产出展开
h_pool2_flat = tf.reshape(h_conv3, [-1, 7 * 7 * 120])
# 神经网络计算,并添加sigmoid激活函数
h_fc1 = tf.nn.sigmoid(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
# h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
# 此时h_fc1.shape = [-1, 80]# 输出层,使用softmax进行多分类
W_fc2 = tf.Variable(tf.truncated_normal([80, 10]))
b_fc2 = tf.Variable(tf.truncated_normal([10]))
y_output = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)# 损失函数
cross_entropy = -tf.reduce_sum(y_ * tf.log(y_output))
# 使用GD优化算法来调整参数
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(cross_entropy)sess = tf.InteractiveSession()# 测试正确率
correct_prediction = tf.equal(tf.argmax(y_output, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))# 所有变量进行初始化
sess.run(tf.initialize_all_variables())'''
# Debug
batch_xs, batch_ys = mnist_data_set.train.next_batch(5)
x = tf.Variable(tf.truncated_normal([5, 5, 1, 6]))
init = tf.global_variables_initializer()
with tf.Session() as sess:sess.run(init)print(conv1.eval(feed_dict={x: batch_xs}).shape)print(h_conv1.eval(feed_dict={x: batch_xs}).shape)print(maxPool2.eval(feed_dict={x: batch_xs}).shape)print(conv2.eval(feed_dict={x: batch_xs}).shape)print(h_conv2.eval(feed_dict={x: batch_xs}).shape)print(maxPool3.eval(feed_dict={x: batch_xs}).shape)print(h_conv3.eval(feed_dict={x: batch_xs}).shape)print(h_fc1.eval(feed_dict={x: batch_xs}).shape)print('debug')
'''# 进行训练
batch_size = 200
start_time = time.time()
for i in range(20000):for iteration in range(mnist_data_set.train.num_examples//batch_size):# 获取训练数据batch_xs, batch_ys = mnist_data_set.train.next_batch(batch_size)train_step.run(feed_dict={x: batch_xs, y_: batch_ys})batch_xs, batch_ys = mnist_data_set.test.images, mnist_data_set.test.labelstrain_accuracy = accuracy.eval(feed_dict={x: batch_xs, y_: batch_ys})print("step %d, training accuracy %g" % (i, train_accuracy))end_time = time.time()print('time: ', (end_time - start_time))start_time = end_time# 关闭会话
sess.close()

注意事项:将sigmoid激活函数改为relu激活函数后,好像效果更差了(在我的笔记本上训练前3步后准确率都在0.09以下,我的笔记本速度太慢,不知继续训练下去会怎样,留待高级服务器上试试)。

各种优化后的loss和accuracy曲线图

enter image description here

enter image description here

参考文献

[1] 王晓华. TensorFlow深度学习应用实践
[2] Deep Learning(深度学习)学习笔记整理系列之LeNet-5卷积参数个人理解
[3] Gradient-Based Learning Applied to Document Recognition

这篇关于【深度学习笔记2.1】LeNet-5的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1035270

相关文章

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Python中__init__方法使用的深度解析

《Python中__init__方法使用的深度解析》在Python的面向对象编程(OOP)体系中,__init__方法如同建造房屋时的奠基仪式——它定义了对象诞生时的初始状态,下面我们就来深入了解下_... 目录一、__init__的基因图谱二、初始化过程的魔法时刻继承链中的初始化顺序self参数的奥秘默认

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

Python 中的异步与同步深度解析(实践记录)

《Python中的异步与同步深度解析(实践记录)》在Python编程世界里,异步和同步的概念是理解程序执行流程和性能优化的关键,这篇文章将带你深入了解它们的差异,以及阻塞和非阻塞的特性,同时通过实际... 目录python中的异步与同步:深度解析与实践异步与同步的定义异步同步阻塞与非阻塞的概念阻塞非阻塞同步

Redis中高并发读写性能的深度解析与优化

《Redis中高并发读写性能的深度解析与优化》Redis作为一款高性能的内存数据库,广泛应用于缓存、消息队列、实时统计等场景,本文将深入探讨Redis的读写并发能力,感兴趣的小伙伴可以了解下... 目录引言一、Redis 并发能力概述1.1 Redis 的读写性能1.2 影响 Redis 并发能力的因素二、

最新Spring Security实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)

《最新SpringSecurity实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)》本章节介绍了如何通过SpringSecurity实现从配置自定义登录页面、表单登录处理逻辑的配置,并简单模拟... 目录前言改造准备开始登录页改造自定义用户名密码登陆成功失败跳转问题自定义登出前后端分离适配方案结语前言

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

Redis 内存淘汰策略深度解析(最新推荐)

《Redis内存淘汰策略深度解析(最新推荐)》本文详细探讨了Redis的内存淘汰策略、实现原理、适用场景及最佳实践,介绍了八种内存淘汰策略,包括noeviction、LRU、LFU、TTL、Rand... 目录一、 内存淘汰策略概述二、内存淘汰策略详解2.1 ​noeviction(不淘汰)​2.2 ​LR

Python与DeepSeek的深度融合实战

《Python与DeepSeek的深度融合实战》Python作为最受欢迎的编程语言之一,以其简洁易读的语法、丰富的库和广泛的应用场景,成为了无数开发者的首选,而DeepSeek,作为人工智能领域的新星... 目录一、python与DeepSeek的结合优势二、模型训练1. 数据准备2. 模型架构与参数设置3