博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UFLDL stackedae_exercise编程答案
阅读量:4147 次
发布时间:2019-05-25

本文共 6569 字,大约阅读时间需要 21 分钟。

Step 0: Initialize constants and parameters

Open stackedAEExercise.m. In this step, we set meta-parameters to the same values that were used in previous exercise, which should produce reasonable results. You may to modify the meta-parameters if you wish.

Step 1: Train the data on the first stacked autoencoder

Train the first autoencoder on the training images to obtain its parameters. This step is identical to the corresponding step in the sparse autoencoder and STL assignments, complete this part of the code so as to learn a first layer of features using your sparseAutoencoderCost.m and minFunc.

Step 2: Train the data on the second stacked autoencoder

We first forward propagate the training set through the first autoencoder (using feedForwardAutoencoder.m that you completed in ) to obtain hidden unit activations. These activations are then used to train the second sparse autoencoder. Since this is just an adapted application of a standard autoencoder, it should run similarly with the first. Complete this part of the code so as to learn a first layer of features using your sparseAutoencoderCost.m and minFunc.

This part of the exercise demonstrates the idea of greedy layerwise training with the same learning algorithm reapplied multiple times.

代码实现:

addpath minFunc/options.Method = 'lbfgs'; % Here, we use L-BFGS to optimize our cost                          % function. Generally, for minFunc to work, you                          % need a function pointer with two outputs: the                          % function value and the gradient. In our problem,                          % sparseAutoencoderCost.m satisfies this.options.maxIter = 400;	  % Maximum number of iterations of L-BFGS to run options.display = 'on';% % [sae1OptTheta, cost] = minFunc( @(p) sparseAutoencoderCost(p, ...                                   inputSize, hiddenSizeL1, ...                                   lambda, sparsityParam, ...                                   beta, trainData), ...                              sae1Theta, options);% save('sae1OptTheta','sae1OptTheta');% load sae1OptTheta.mat;

Step 3: Train the softmax classifier on the L2 features

Next, continue to forward propagate the L1 features through the second autoencoder (using feedForwardAutoencoder.m) to obtain the L2 hidden unit activations. These activations are then used to train the softmax classifier. You can either use softmaxTrain.m or directly use softmaxCost.m that you completed in  to complete this part of the assignment.

代码实现

[sae2OptTheta, cost] = minFunc( @(p) sparseAutoencoderCost(p, ...                                   hiddenSizeL1, hiddenSizeL2, ...                                   lambda, sparsityParam, ...                                   beta, sae1Features), ...                              sae2Theta, options);% save('sae2OptTheta','sae2OptTheta');    % load sae2OptTheta.mat;
softmax训练
代码实现:

% %train sftmax regressionoptions.maxIter = 100;softmaxModel = softmaxTrain(hiddenSizeL2, numClasses, lambda, ...                            sae2Features, trainLabels, options);saeSoftmaxOptTheta = softmaxModel.optTheta(:);% save('saeSoftmaxOptTheta','saeSoftmaxOptTheta');% load saeSoftmaxOptTheta.mat;

Step 4: Implement fine-tuning

To implement fine tuning, we need to consider all three layers as a single model. Implement stackedAECost.m to return the cost and gradient of the model. The cost function should be as defined as the log likelihood and a gradient decay term. The gradient should be computed using . The predictions should consist of the activations of the output layer of the softmax model.

To help you check that your implementation is correct, you should also check your gradients on a synthetic small dataset. We have implemented checkStackedAECost.m to help you check your gradients. If this checks passes, you will have implemented fine-tuning correctly.

Note: When adding the weight decay term to the cost, you should regularize only the softmax weights (do not regularize the weights that compute the hidden layer activations).

Implementation Tip: It is always a good idea to implement the code modularly and check (the gradient of) each part of the code before writing the more complicated parts.

stackedAECost.m中代码实现:

% feedforwardn=numel(stack);z=cell(n+1,1);a=cell(n+1,1);a{1}=data;for i=1:n    z_temp=stack{i}.w*a{i};    z{i+1}=bsxfun(@plus,z_temp,stack{i}.b);    a{i+1}=sigmoid(z{i+1});end%softmax outputH=softmaxTheta*a{i+1};H=bsxfun(@minus, H, max(H, [], 1)); % to prevent overflowExpM=exp(H);P=bsxfun(@rdivide,ExpM,sum(ExpM));%costcost=-1/M*sum(sum(groundTruth.*log(P)))+lambda/2*sum((softmaxThetaGrad(:)).^2);%the gradientdelta=cell(n+1,1);delta{n+1}=-softmaxTheta.'*(groundTruth-P).*a{n+1}.*(1-a{n+1}); %200*10softmaxThetaGrad=-1/M.*(groundTruth-P)*a{n+1}.'+lambda.*softmaxThetaGrad;%deltafor l=n:-1:2    delta{l}=stack{l}.w.'*delta{l+1}.*a{l}.*(1-a{l});    stackgrad{l}.w=delta{l+1}*(a{l}).'./M;    stackgrad{l}.b=sum(delta{l+1},2)./M;endstackgrad{1}.w=delta{2}*(a{1}).'./M;stackgrad{1}.b=sum(delta{2},2)./M;
主函数中代码实现:

%% ---------------------- YOUR CODE HERE  ---------------------------------%  Instructions: Train the deep network, hidden size here refers to the '%                dimension of the input to the classifier, which corresponds %                to "hiddenSizeL2".%%[stackedAEOptTheta, cost] = minFunc( @(p) stackedAECost(p, ...                                   inputSize, hiddenSizeL2, ...                                   numClasses,netconfig,lambda, ...                                   trainData, trainLabels), ...                              stackedAETheta, options);

Step 5: Test the model

Finally, you will need to classify with this model; complete the code in stackedAEPredict.m to classify using the stacked autoencoder with a classification layer.

After completing these steps, running the entire script in stackedAETrain.m will perform layer-wise training of the stacked autoencoder, finetune the model, and measure its performance on the test set. If you've done all the steps correctly, you should get an accuracy of about 87.7% before finetuning and 97.6% after finetuning (for the 10-way classification problem).

代码实现:

n=numel(stack);z=cell(n+1,1);a=cell(n+1,1);a{1}=data;for i=1:n    z_temp=stack{i}.w*a{i};    z{i+1}=bsxfun(@plus,z_temp,stack{i}.b);    a{i+1}=sigmoid(z{i+1});end%softmax outputH=softmaxTheta*a{i+1};H=bsxfun(@minus, H, max(H, [], 1)); % to prevent overflowExpM=exp(H);P=bsxfun(@rdivide,ExpM,sum(ExpM));[hmax,pred]=max(P);clear hmax;
运行结果:

微调之前 识别精度为:88.080%

微调之后 识别精度为:97.760%

源代码下载:

转载地址:http://tmjti.baihongyu.com/

你可能感兴趣的文章
这才是学习Vite2的正确姿势!
查看>>
7 个适用于所有前端开发人员的很棒API,你需要了解一下
查看>>
25个构建Web项目的HTML建议,你需要了解一下!
查看>>
【web素材】02-10款大气的购物商城网站模板
查看>>
6种方式实现JavaScript数组扁平化(flat)方法的总结
查看>>
49个在工作中常用且容易遗忘的CSS样式清单整理
查看>>
20种在学习编程的同时也可以在线赚钱的方法
查看>>
隐藏搜索框:CSS 动画正反向序列
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(上)
查看>>
【视频教程】Javascript ES6 教程27—ES6 构建一个Promise
查看>>
【5分钟代码练习】01—导航栏鼠标悬停效果的实现
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(中)
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(下)
查看>>
【web素材】03-24款后台管理系统网站模板
查看>>
Flex 布局教程:语法篇
查看>>
年薪50万+的90后程序员都经历了什么?
查看>>
2019年哪些外快收入可达到2万以上?
查看>>
【JavaScript 教程】标准库—Date 对象
查看>>
前阿里手淘前端负责人@winter:前端人如何保持竞争力?
查看>>
【JavaScript 教程】面向对象编程——实例对象与 new 命令
查看>>