CNN tflearn处理mnist图像识别代码解说——conv_2d参数解释,整个网络的训练,主要就是为了学那个卷积核啊。

简介:

官方参数解释:

Convolution 2D

tflearn.layers.conv.conv_2d (incoming, nb_filter, filter_size, strides=1, padding='same', activation='linear', bias=True, weights_init='uniform_scaling', bias_init='zeros', regularizer=None, weight_decay=0.001, trainable=True, restore=True, reuse=False, scope=None, name='Conv2D')

Input

4-D Tensor [batch, height, width, in_channels].

Output

4-D Tensor [batch, new height, new width, nb_filter].

Arguments

  • incoming: Tensor. Incoming 4-D Tensor.
  • nb_filter: int. The number of convolutional filters.
  • filter_size: int or list of int. Size of filters.
  • strides: 'intor list ofint`. Strides of conv operation. Default: [1 1 1 1].
  • padding: str from "same", "valid". Padding algo to use. Default: 'same'.
  • activation: str (name) or function (returning a Tensor) or None. Activation applied to this layer (see tflearn.activations). Default: 'linear'.
  • bias: bool. If True, a bias is used.
  • weights_init: str (name) or Tensor. Weights initialization. (see tflearn.initializations) Default: 'truncated_normal'.
  • bias_init: str (name) or Tensor. Bias initialization. (see tflearn.initializations) Default: 'zeros'.
  • regularizer: str (name) or Tensor. Add a regularizer to this layer weights (see tflearn.regularizers). Default: None.
  • weight_decay: float. Regularizer decay parameter. Default: 0.001.
  • trainable: bool. If True, weights will be trainable.
  • restore: bool. If True, this layer weights will be restored when loading a model.
  • reuse: bool. If True and 'scope' is provided, this layer variables will be reused (shared).
  • scope: str. Define this layer scope (optional). A scope can be used to share variables between layers. Note that scope will override name.
  • name: A name for this layer (optional). Default: 'Conv2D'.

 

代码:

     # 64 filters
     net  =  tflearn.conv_2d(net,  64 ,  3 , activation = 'relu' )
按照我的理解:

其中的filter(卷积核)就是
[1 0 1 
 0 1 0
 1 0 1],size=3
因为设置了64个filter,那么卷积操作后有64个卷积结果作为输入的特征(feature map)。难道后面激活函数就是因为选择部分激活???


图的原文:http://cs231n.github.io/convolutional-networks/

 

如果一个卷积层有4个feature map,那是不是就有4个卷积核?
是的。


这4个卷积核如何定义?
通常是随机初始化再用BP算梯度做训练。如果数据少或者没有labeled data的话也可以考虑用K-means的K个中心点,逐层做初始化。

卷积核是学习的。卷积核是因为权重的作用方式跟卷积一样,所以叫卷积层,其实你还是可以把它看成是一个parameter layer,需要更新的。
这四个卷积核就属于网络的参数,然后通过BP进行训练
整个网络的训练,主要就是为了学那个卷积核啊。
先初始化一个,之后BP调整,你可以去看看caffe的源码。



--------------------------------------------------------------------------------------------------
下面内容摘自:http://blog.csdn.net/bugcreater/article/details/53293075
  1. from __future__ import division, print_function, absolute_import  
  2.   
  3. import tflearn  
  4. from tflearn.layers.core import input_data, dropout, fully_connected  
  5. from tflearn.layers.conv import conv_2d, max_pool_2d  
  6. from tflearn.layers.normalization import local_response_normalization  
  7. from tflearn.layers.estimator import regression  
  8. #加载大名顶顶的mnist数据集(http://yann.lecun.com/exdb/mnist/)  
  9. import tflearn.datasets.mnist as mnist  
  10. X, Y, testX, testY = mnist.load_data(one_hot=True)  
  11. X = X.reshape([-1, 28, 28, 1])  
  12. testX = testX.reshape([-1, 28, 28, 1])  
  13.   
  14. network = input_data(shape=[None, 28, 28, 1], name='input')  
  15. # CNN中的卷积操作,下面会有详细解释  
  16. network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")  
  17. # 最大池化操作  
  18. network = max_pool_2d(network, 2)  
  19. # 局部响应归一化操作  
  20. network = local_response_normalization(network)  
  21. network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")  
  22. network = max_pool_2d(network, 2)  
  23. network = local_response_normalization(network)  
  24. # 全连接操作  
  25. network = fully_connected(network, 128, activation='tanh')  
  26. # dropout操作  
  27. network = dropout(network, 0.8)  
  28. network = fully_connected(network, 256, activation='tanh')  
  29. network = dropout(network, 0.8)  
  30. network = fully_connected(network, 10, activation='softmax')  
  31. # 回归操作  
  32. network = regression(network, optimizer='adam', learning_rate=0.01,  
  33.                      loss='categorical_crossentropy', name='target')  
  34.   
  35. # Training  
  36. # DNN操作,构建深度神经网络  
  37. model = tflearn.DNN(network, tensorboard_verbose=0)  
  38. model.fit({'input': X}, {'target': Y}, n_epoch=20,  
  39.            validation_set=({'input': testX}, {'target': testY}),  
  40.            snapshot_step=100, show_metric=True, run_id='convnet_mnist')  

 


关于conv_2d函数,在源码里是可以看到总共有14个参数,分别如下:

1.incoming: 输入的张量,形式是[batch, height, width, in_channels]
2.nb_filter: filter的个数
3.filter_size: filter的尺寸,是int类型
4.strides: 卷积操作的步长,默认是[1,1,1,1]
5.padding: padding操作时标志位,"same"或者"valid",默认是“same”
6.activation: 激活函数(ps:这里需要了解的知识很多,会单独讲)
7.bias: bool量,如果True,就是使用bias
8.weights_init: 权重的初始化
9.bias_init: bias的初始化,默认是0,比如众所周知的线性函数y=wx+b,其中的w就相当于weights,b就是bias
10.regularizer: 正则项(这里需要讲解的东西非常多,会单独讲)
11.weight_decay: 权重下降的学习率
12.trainable: bool量,是否可以被训练
13.restore: bool量,训练的模型是否被保存
14.name: 卷积层的名称,默认是"Conv2D"
 
关于max_pool_2d函数,在源码里有5个参数,分别如下:
1.incoming ,类似于conv_2d里的incoming
2.kernel_size:池化时核的大小,相当于conv_2d时的filter的尺寸
3.strides:类似于conv_2d里的strides
4.padding:同上
5.name:同上
 
看了这么多参数,好像有些迷糊,我先用一张图解释下每个参数的意义。
 

其中的filter就是
[1 0 1 
 0 1 0
 1 0 1],size=3,由于每次移动filter都是一个格子,所以strides=1.
 
关于最大池化可以看看下面这张图,这里面 strides=1,kernel_size =2(就是每个颜色块的大小),图中示意的最大池化(可以提取出显著信息,比如在进行文本分析时可以提取一句话里的关键字,以及图像处理中显著颜色,纹理等),关于池化这里多说一句,有时需要平均池化,有时需要最小池化。

下面说说其中的padding操作,做图像处理的人对于这个操作应该不会陌生,说白了,就是填充。比如你对图像做卷积操作,比如你用的3×3的卷积核,在进行边上操作时,会发现卷积核已经超过原图像,这时需要把原图像进行扩大,扩大出来的就是填充,基本都填充0。




Convolution Demo. Below is a running demo of a CONV layer. Since 3D volumes are hard to visualize, all the volumes (the input volume (in blue), the weight volumes (in red), the output volume (in green)) are visualized with each depth slice stacked in rows. The input volume is of size W1=5,H1=5,D1=3, and the CONV layer parameters are K=2,F=3,S=2,P=1. That is, we have two filters of size 3×3, and they are applied with a stride of 2. Therefore, the output volume size has spatial size (5 - 3 + 2)/2 + 1 = 3. Moreover, notice that a padding of P=1

 

 

General pooling. In addition to max pooling, the pooling units can also perform other functions, such as average pooling or even L2-norm pooling. Average pooling was often used historically but has recently fallen out of favor compared to the max pooling operation, which has been shown to work better in practice.

 
 
Pooling layer downsamples the volume spatially, independently in each depth slice of the input volume.  Left: In this example, the input volume of size [224x224x64] is pooled with filter size 2, stride 2 into output volume of size [112x112x64]. Notice that the volume depth is preserved.  Right: The most common downsampling operation is max, giving rise to  max pooling, here shown with a stride of 2. That is, each max is taken over 4 numbers (little 2x2 square).

 




















本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/7908217.html,如需转载请自行联系原作者


相关文章
|
机器学习/深度学习 人工智能 算法
AI 基础知识从 0.6 到 0.7—— 彻底拆解深度神经网络训练的五大核心步骤
本文以一个经典的PyTorch手写数字识别代码示例为引子,深入剖析了简洁代码背后隐藏的深度神经网络(DNN)训练全过程。
1892 56
|
机器学习/深度学习 存储 算法
NoProp:无需反向传播,基于去噪原理的非全局梯度传播神经网络训练,可大幅降低内存消耗
反向传播算法虽是深度学习基石,但面临内存消耗大和并行扩展受限的问题。近期,牛津大学等机构提出NoProp方法,通过扩散模型概念,将训练重塑为分层去噪任务,无需全局前向或反向传播。NoProp包含三种变体(DT、CT、FM),具备低内存占用与高效训练优势,在CIFAR-10等数据集上达到与传统方法相当的性能。其层间解耦特性支持分布式并行训练,为无梯度深度学习提供了新方向。
1080 1
NoProp:无需反向传播,基于去噪原理的非全局梯度传播神经网络训练,可大幅降低内存消耗
|
机器学习/深度学习 算法
PSO和GA优化BP神经网络参数
PSO和GA优化BP神经网络参数
487 5
|
存储 弹性计算 网络协议
阿里云服务器ECS实例规格族是什么?不同规格CPU型号、处理器主频及网络性能参数均不同
阿里云ECS实例规格族是指具有不同性能特点和适用场景的实例类型集合。不同规格族如计算型c9i、通用算力型u1、经济型e等,在CPU型号、主频、网络性能、云盘IOPS等方面存在差异。即使CPU和内存配置相同,性能参数和价格也各不相同,适用于不同业务需求。
943 144
|
机器学习/深度学习 数据可视化 网络架构
PINN训练新思路:把初始条件和边界约束嵌入网络架构,解决多目标优化难题
PINNs训练难因多目标优化易失衡。通过设计硬约束网络架构,将初始与边界条件内嵌于模型输出,可自动满足约束,仅需优化方程残差,简化训练过程,提升稳定性与精度,适用于气候、生物医学等高要求仿真场景。
1207 4
PINN训练新思路:把初始条件和边界约束嵌入网络架构,解决多目标优化难题
|
机器学习/深度学习 文件存储 异构计算
YOLOv11改进策略【模型轻量化】| 替换骨干网络为EfficientNet v2,加速训练,快速收敛
YOLOv11改进策略【模型轻量化】| 替换骨干网络为EfficientNet v2,加速训练,快速收敛
1644 18
YOLOv11改进策略【模型轻量化】| 替换骨干网络为EfficientNet v2,加速训练,快速收敛
|
机器学习/深度学习 数据可视化 API
DeepSeek生成对抗网络(GAN)的训练与应用
生成对抗网络(GANs)是深度学习的重要技术,能生成逼真的图像、音频和文本数据。通过生成器和判别器的对抗训练,GANs实现高质量数据生成。DeepSeek提供强大工具和API,简化GAN的训练与应用。本文介绍如何使用DeepSeek构建、训练GAN,并通过代码示例帮助掌握相关技巧,涵盖模型定义、训练过程及图像生成等环节。
|
机器学习/深度学习 文件存储 异构计算
RT-DETR改进策略【模型轻量化】| 替换骨干网络为EfficientNet v2,加速训练,快速收敛
RT-DETR改进策略【模型轻量化】| 替换骨干网络为EfficientNet v2,加速训练,快速收敛
567 1
|
人工智能 搜索推荐 决策智能
不靠更复杂的策略,仅凭和大模型训练对齐,零样本零经验单LLM调用,成为网络任务智能体新SOTA
近期研究通过调整网络智能体的观察和动作空间,使其与大型语言模型(LLM)的能力对齐,显著提升了基于LLM的网络智能体性能。AgentOccam智能体在WebArena基准上超越了先前方法,成功率提升26.6个点(+161%)。该研究强调了与LLM训练目标一致的重要性,为网络任务自动化提供了新思路,但也指出其性能受限于LLM能力及任务复杂度。论文链接:https://arxiv.org/abs/2410.13825。
432 12
|
Go 数据安全/隐私保护 UED
优化Go语言中的网络连接:设置代理超时参数
优化Go语言中的网络连接:设置代理超时参数

热门文章

最新文章