【深度学习笔记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

相关文章

深度解析Python中递归下降解析器的原理与实现

《深度解析Python中递归下降解析器的原理与实现》在编译器设计、配置文件处理和数据转换领域,递归下降解析器是最常用且最直观的解析技术,本文将详细介绍递归下降解析器的原理与实现,感兴趣的小伙伴可以跟随... 目录引言:解析器的核心价值一、递归下降解析器基础1.1 核心概念解析1.2 基本架构二、简单算术表达

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

Java MCP 的鉴权深度解析

《JavaMCP的鉴权深度解析》文章介绍JavaMCP鉴权的实现方式,指出客户端可通过queryString、header或env传递鉴权信息,服务器端支持工具单独鉴权、过滤器集中鉴权及启动时鉴权... 目录一、MCP Client 侧(负责传递,比较简单)(1)常见的 mcpServers json 配置

Maven中生命周期深度解析与实战指南

《Maven中生命周期深度解析与实战指南》这篇文章主要为大家详细介绍了Maven生命周期实战指南,包含核心概念、阶段详解、SpringBoot特化场景及企业级实践建议,希望对大家有一定的帮助... 目录一、Maven 生命周期哲学二、default生命周期核心阶段详解(高频使用)三、clean生命周期核心阶

深度剖析SpringBoot日志性能提升的原因与解决

《深度剖析SpringBoot日志性能提升的原因与解决》日志记录本该是辅助工具,却为何成了性能瓶颈,SpringBoot如何用代码彻底破解日志导致的高延迟问题,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言第一章:日志性能陷阱的底层原理1.1 日志级别的“双刃剑”效应1.2 同步日志的“吞吐量杀手”

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499