deep learning toolbox学习4之CNN

本文详细解析了CNN(卷积神经网络)的结构及训练流程,包括网络结构设计、数据初始化、卷积层与池化层的工作原理,以及前向传播与反向传播的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看了论文和博客,对于CNN还是有些模糊,索性直接看代码,下面总结一下Toolbox中CNN的过程:
网络结构是采用1-6c-2s-12c-2s的结构,对于初始层,相当于只有一层特征层作为输入,然后是CNN中所特有的c层和s层,这里说一下c层和s层,c层就是convolutional层,将输入层通过不同的卷积核map到几个特征层上,这里面就涉及到卷积操作和CNN的一大卖点共享权重,这一层主要相当于特征提取;然后是s层,就是subsampling层,也就是需要进行pooling操作的层,这一层相当于特征压缩。对应到上述网络结构就是一层输入层->6个c层特征图(需要6个卷积核)->6个s层(这里的2s是指pooling的scale大小是2,即2*2的进行pooling)->12个c层(从6个s层作为输入,需要12个卷积核)->12个s层(pooling scale size是2),最终的输出层再作为分类器的输入,进行结果的矫正。
1.数据初始化
将x的784维reshape成28*28的2D输入,依旧是做归一化处理;定义5层网络结构(i1-c2-s3-c4-s5):
对于c2和c4层定义mapsize,分别从28*28map到24*24以及12*12map到8*8,因为kernelsize=5。定义输入层的各个特征层到输出层(即输入的上一层)的各个特征层的卷积核,定义fan_out和fan_in用于卷积核部分,对于卷积核的定义,k_ij = (rand(n_l.kernelsize) - 0.5) * 2 * sqrt(6 / (fan_in + fan_out),直观上来看就是随机生成一个5*5的卷积核矩阵,k_ij是指输入层的第i个特征图到输出层的第j个特征图之间的卷积核。定义每个输出层的每个特征图的bias,即每个特征图对应一个b。
对于s3和s5两层,定义mapsize,显然在c层基础上减半,因为在s层没有做卷积运算,所以只需要定义各个特征图的bias即可
Q:在Notes on Convolutional Neural Networks这篇论文中,对于s层也涉及卷积,因为卷积运算对于ff和bp过程是有影响的,这里没有?
Q:定义隐藏层的大小fan_out,是一个(后层特征图数量)*(用来卷积的patch图的大小) ;定义fan_in对于每一个后层特征图,有多少个参数链到前层?
最后定义输出层和分类器层,显然最后输出神经元个数为4*4*12,定义b和w,w的定义和前面卷积核采用相同定义方式。
2.训练阶段
大体过程和NN过程类似,这里是50个作为一个batch,因此每次输入的数据为28*28*50
2.1feedforward pass阶段
在ff阶段,也是分为c层和s层的处理。
对于c层,对于每个特征层(注意区分c层是指五层网络中的,特征层是指该层分为的特征层),计算输入层的每个特征层到与该层的卷积,并求和作为权值和z,类似于NN中的W*x的结果,只不过这里使用的卷积操作来代替NN中的*,28*28的输入map与5*5的卷积map做卷积后便是24*24的,对于该map层的激活值a_lj(l表示层数,j表示特征层数)=sig(z+b_j),如此得到c层各个map层的激活值a,作为下层(s层)的输入
对于s层,对下层的每个map输入层,做pooling操作,将pooling操作结果作为该层各个特征层的激活值,对于pooling操作,原本的理解是将2*2块做平均或取最大操作即可,这里作者的处理还有点不理解,这里做的是mean pooling:
Q:作者是将下层a与2*2的值为0.25的卷积核做卷积,得到的矩阵横竖方向各隔1取值作为pooling 结果?
最后的分类器层,将每个的4*4*12的特征(50个一个batch)化为192,即将12个层次的2d图像化为1层的1d特征作为分类器输入,然后使用w和b用sigm函数分类,如此ff过程结束。
2.2back propagation阶段
这里的bp过程基本和NN类似,公式部分也很相似,大体思路一样计算顶层和下层的残差,根据残差得到梯度,根据梯度来更新w和b,这里的w是卷积核k。
首先计算顶层的残差,此时是192*50的,当计算下层的时候,需要reshape出各个map层,比如顶层需reshape出12*4*4*50的结构。
对于c层层数l的各个特征层j:
d_lj = a_lj.* (1 - a_lj) .* (expand(d_l+1_j,[2,2,1])/ net.layers{l + 1}.scale ^ 2);该式子前部分就是求f’,和NN中一样,后半部分将上一层残差d poolin的每个值变成2*2相同的值,再除以4,个人感觉这里相当于NN中的w_l'*d_l+1的部分,(NN中d_l = w_l' * d_l+1  * f')
Q:这里的原理?
对于s层层数l的各个特征层i:
如何求i层的残差d呢,需要将l+1层的各个map层j与  将第i层到j层的卷积核做rot180处理后 再做卷积运算,个人理解就是卷积的逆运算,
例如现在要将c412个map层8*8的残差传递到s3层6个map层12*12,定义z为12*12*50
for i = 1 to 6
    for j = 1 to 12
        z=z+conv(d_l+1_j,rot180(k_l+1_ij))
    d_l_i = z
如此可以计算c和s层的残差,下面根据残差计算下降的梯度,由于只有c层用到了sigmoid函数,因此只需要对c层进行计算,对于b的下降梯度和NN一样直接使用残差即可,对于w也就是这里的k,NN中是d*a,这里是采用卷积操作,依然涉及到rot操作。
可以参考之前的NN部分的bp的过程,就知道此处d*a的含义。
最后便是根据梯度来更新k和b,依然有学习率的参与,NN中的其他防止过拟合的参数没有涉及目前。
如此完成训练阶段
3.测试
和NN一样,用训练好的各层k和b,再对testx进行一次卷积前向传播过程,与y比较,可以分析效果。
目前来看,感觉CNN很慢,原因?
Deep Learning Toolbox™提供了一个框架,用于设计和实现具有算法,预训练模型和应用程序的深度神经网络。您可以使用卷积神经网络(ConvNets,CNN)和长期短期记忆(LSTM)网络对图像,时间序列和文本数据进行分类和回归。应用程序和图表可帮助您可视化激活,编辑网络体系结构以及监控培训进度。 对于小型训练集,您可以使用预训练的深层网络模型(包括SqueezeNet,Inception-v3,ResNet-101,GoogLeNet和VGG-19)以及从TensorFlow™-Keras和Caffe导入的模型执行传输学习。 了解深度学习工具箱的基础知识 深度学习图像 从头开始训练卷积神经网络或使用预训练网络快速学习新任务 使用时间序列,序列和文本进行深度学习 为时间序列分类,回归和预测任务创建和训练网络 深度学习调整和可视化 绘制培训进度,评估准确性,进行预测,调整培训选项以及可视化网络学习的功能 并行和云中的深度学习 通过本地或云中的多个GPU扩展深度学习,并以交互方式或批量作业培训多个网络 深度学习应用 通过计算机视觉,图像处理,自动驾驶,信号和音频扩展深度学习工作流程 深度学习导入,导出和自定义 导入和导出网络,定义自定义深度学习图层以及自定义数据存储 深度学习代码生成 生成MATLAB代码或CUDA ®和C ++代码和部署深学习网络 函数逼近和聚类 使用浅层神经网络执行回归,分类和聚类 时间序列和控制系统 基于浅网络的模型非线性动态系统; 使用顺序数据进行预测。
深度学习工具包 Deprecation notice. ----- This toolbox is outdated and no longer maintained. There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/) I would suggest you use one of the tools mentioned above rather than use this toolbox. Best, Rasmus. DeepLearnToolbox ================ A Matlab toolbox for Deep Learning. Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data. It is inspired by the human brain's apparent deep (layered, hierarchical) architecture. A good overview of the theory of Deep Learning theory is [Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf) For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng. * [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007) * [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010) * [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011) If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284) ``` @MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012", } ``` Contact: rasmusbergpalm at gmail dot com Directories included in the toolbox ----------------------------------- `NN/` - A library for Feedforward Backpropagation Neural Networks `CNN/` - A library for Convolutional Neural Networks `DBN/` - A library for Deep Belief Networks `SAE/` - A library for Stacked Auto-Encoders `CAE/` - A library for Convolutional Auto-Encoders `util/` - Utility functions used by the libraries `data/` - Data used by the examples `tests/` - unit tests to verify toolbox is working For references on each library check REFS.md Setup ----- 1. Download. 2. addpath(genpath('DeepLearnToolbox')); Example: Deep Belief Network --------------------- ```matlab function test_example_DBN load mnist_uint8; train_x = double(train_x) / 255; test_x = double(test_x) / 255; train_y = double(train_y); test_y = double(test_y); %% ex1 train a 100 hidden unit RBM and visualize its weights rand('state',0) dbn.sizes = [100]; opts.numepochs = 1; opts.batchsize = 100; opts.momentum = 0; opts.alpha = 1; dbn = dbnsetup(dbn, train_x, opts); dbn = dbntrain(dbn, train_x, opts); figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights %% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NN rand('state',0) %train dbn dbn.sizes = [100 100]; opts.numepochs = 1; opts.batchsize = 100; opts.momentum = 0; opts.alpha = 1; dbn = dbnsetup(dbn, train_x, opts); dbn = dbntrain(dbn, train_x, opts); %unfold dbn to nn nn = dbnunfoldtonn(dbn, 10); nn.activation_function = 'sigm'; %train nn opts.numepochs = 1; opts.batchsize = 100; nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.10, 'Too big error'); ``` Example: Stacked Auto-Encoders --------------------- ```matlab function test_example_SAE load mnist_uint8; train_x = double(train_x)/255; test_x = double(test_x)/255; train_y = double(train_y); test_y = double(test_y); %% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN % Setup and train a stacked denoising autoencoder (SDAE) rand('state',0) sae = saesetup([784 100]); sae.ae{1}.activation_function = 'sigm'; sae.ae{1}.learningRate = 1; sae.ae{1}.inputZeroMaskedFraction = 0.5; opts.numepochs = 1; opts.batchsize = 100; sae = saetrain(sae, train_x, opts); visualize(sae.ae{1}.W{1}(:,2:end)') % Use the SDAE to initialize a FFNN nn = nnsetup([784 100 10]); nn.activation_function = 'sigm'; nn.learningRate = 1; nn.W{1} = sae.ae{1}.W{1}; % Train the FFNN opts.numepochs = 1; opts.batchsize = 100; nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.16, 'Too big error'); ``` Example: Convolutional Neural Nets --------------------- ```matlab function test_example_CNN load mnist_uint8; train_x = double(reshape(train_x',28,28,60000))/255; test_x = double(reshape(test_x',28,28,10000))/255; train_y = double(train_y'); test_y = double(test_y'); %% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% error rand('state',0) cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer }; cnn = cnnsetup(cnn, train_x, train_y); opts.alpha = 1; opts.batchsize = 50; opts.numepochs = 1; cnn = cnntrain(cnn, train_x, train_y, opts); [er, bad] = cnntest(cnn, test_x, test_y); %plot mean squared error figure; plot(cnn.rL); assert(er<0.12, 'Too big error'); ``` Example: Neural Networks --------------------- ```matlab function test_example_NN load mnist_uint8; train_x = double(train_x) / 255; test_x = double(test_x) / 255; train_y = double(train_y); test_y = double(test_y); % normalize [train_x, mu, sigma] = zscore(train_x); test_x = normalize(test_x, mu, sigma); %% ex1 vanilla neural net rand('state',0) nn = nnsetup([784 100 10]); opts.numepochs = 1; % Number of full sweeps through data opts.batchsize = 100; % Take a mean gradient step over this many samples [nn, L] = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.08, 'Too big error'); %% ex2 neural net with L2 weight decay rand('state',0) nn = nnsetup([784 100 10]); nn.weightPenaltyL2 = 1e-4; % L2 weight decay opts.numepochs = 1; % Number of full sweeps through data opts.batchsize = 100; % Take a mean gradient step over this many samples nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.1, 'Too big error'); %% ex3 neural net with dropout rand('state',0) nn = nnsetup([784 100 10]); nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through data opts.batchsize = 100; % Take a mean gradient step over this many samples nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.1, 'Too big error'); %% ex4 neural net with sigmoid activation function rand('state',0) nn = nnsetup([784 100 10]); nn.activation_function = 'sigm'; % Sigmoid activation function nn.learningRate = 1; % Sigm require a lower learning rate opts.numepochs = 1; % Number of full sweeps through data opts.batchsize = 100; % Take a mean gradient step over this many samples nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.1, 'Too big error'); %% ex5 plotting functionality rand('state',0) nn = nnsetup([784 20 10]); opts.numepochs = 5; % Number of full sweeps through data nn.output = 'softmax'; % use softmax output opts.batchsize = 1000; % Take a mean gradient step over this many samples opts.plot = 1; % enable plotting nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.1, 'Too big error'); %% ex6 neural net with sigmoid activation and plotting of validation and training error % split training data into training and validation data vx = train_x(1:10000,:); tx = train_x(10001:end,:); vy = train_y(1:10000,:); ty = train_y(10001:end,:); rand('state',0) nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax output opts.numepochs = 5; % Number of full sweeps through data opts.batchsize = 1000; % Take a mean gradient step over this many samples opts.plot = 1; % enable plotting nn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally) [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.1, 'Too big error'); ``` [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值