UFLDL教程Exercise答案(6):Implement deep networks for digit classification

来源:互联网 发布:codeblocks c语言 编辑:程序博客网 时间:2024/05/21 10:50

教程地址:http://deeplearning.stanford.edu/wiki/index.php/UFLDL_Tutorial

Exercise地址:http://deeplearning.stanford.edu/wiki/index.php/Exercise:_Implement_deep_networks_for_digit_classification

代码

将sparseAutoencoderCost.m   softmaxCost.m feedForwardAutoencoder.m   initializeParameters.m  loadMNISTImages.m   loadMNISTLabels.m文件以及minFunc文件夹从以前的文件中拷贝到本节练习所在的工作路径中。

Step 0: Initialize constants and parameters ——代码已给
Step 00: Load data from the MNIST database——代码已给

根据自己的训练集所在的路径修改相应路径。

我的路径设置为:'F:/DeepLearning/UFLDL/mnist/train-images.idx3-ubyte' ;'F:/DeepLearning/UFLDL/mnist/train-labels.idx1-ubyte'

Step 1: Train the data on the first stacked autoencoder ——stackedAEExercise.m

%% ---------------------- YOUR CODE HERE  ---------------------------------%  Instructions: Train the first layer sparse autoencoder, this layer has%                an hidden size of "hiddenSizeL1"%                You should store the optimal parameters in sae1OptThetasae1OptTheta = sae1Theta;%  Use minFunc to minimize the function  addpath minFunc/  options.Method = 'lbfgs'; % Here, we use L-BFGS to optimize our cost function.  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);  % -------------------------------------------------------------------------

Step 2: Train the data on the second stacked autoencoder ——stackedAEExercise.m

%% ---------------------- YOUR CODE HERE  ---------------------------------%  Instructions: Train the second layer sparse autoencoder, this layer has%                an hidden size of "hiddenSizeL2" and an inputsize of%                "hiddenSizeL1"%%                You should store the optimal parameters in sae2OptThetasae2OptTheta = sae2Theta;%  Use minFunc to minimize the function    options.Method = 'lbfgs'; % Here, we use L-BFGS to optimize our cost function.  options.maxIter = 400;    % Maximum number of iterations of L-BFGS to run   options.display = 'on';  [sae2OptTheta, cost] = minFunc( @(p) sparseAutoencoderCost(p, ...                                     hiddenSizeL1, hiddenSizeL2, ...                                     lambda, sparsityParam, ...                                     beta, sae1Features), ...                                sae2Theta, options);  % -------------------------------------------------------------------------

Step 3: Train the softmax classifier on the L2 features  ——stackedAEExercise.m 

%% ---------------------- YOUR CODE HERE  ---------------------------------%  Instructions: Train the softmax classifier, the classifier takes in%                input of dimension "hiddenSizeL2" corresponding to the%                hidden layer size of the 2nd layer.%%                You should store the optimal parameters in saeSoftmaxOptTheta %%  NOTE: If you used softmaxTrain to complete this part of the exercise,%        set saeSoftmaxOptTheta = softmaxModel.optTheta(:);%%%use softmaxTrain.m options.maxIter = 100;softmaxModel = softmaxTrain(hiddenSizeL2, numClasses, lambda, ...                                        sae2Features, trainLabels, options);saeSoftmaxOptTheta = softmaxModel.optTheta(:);%%%use softmaxCost.m % addpath minFunc/  % options.Method = 'lbfgs'; % Here, we use L-BFGS to optimize our cost function. % minFuncOptions.display = 'on';  % options.maxIter = 400; % [saeSoftmaxOptTheta, cost3] = minFunc( @(p) softmaxCost(p, ...  %                                    numClasses, hiddenSizeL2, lambda, ...  %                                    sae2Features, trainLabels), ...                                     %                               saeSoftmaxTheta, options);% ------------------------------------------------------------------------- 

Step 4: Implement fine-tuning ——stackedAEExercise.m  + stackedAECost.m

%%%stackedAEExercise.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".%%options.Method = 'lbfgs';  options.maxIter = 400;  options.display = 'on';  [stackedAEOptTheta, cost] = minFunc( @(p) stackedAECost(p, inputSize, hiddenSizeL2, ...                                                numClasses, netconfig, ...                                                lambda, trainData, trainLabels), ...                                                                          stackedAETheta, options);  % -------------------------------------------------------------------------
%%%stackedAECost.m%% --------------------------- YOUR CODE HERE -----------------------------%  Instructions: Compute the cost function and gradient vector for %                the stacked autoencoder.%%                You are given a stack variable which is a cell-array of%                the weights and biases for every layer. In particular, you%                can refer to the weights of Layer d, using stack{d}.w and%                the biases using stack{d}.b . To get the total number of%                layers, you can use numel(stack).%%                The last layer of the network is connected to the softmax%                classification layer, softmaxTheta.%%                You should compute the gradients for the softmaxTheta,%                storing that in softmaxThetaGrad. Similarly, you should%                compute the gradients for each layer in the stack, storing%                the gradients in stackgrad{d}.w and stackgrad{d}.b%                Note that the size of the matrices in stackgrad should%                match exactly that of the size of the matrices in stack.%% %【1】进行前馈传递%计算hiddenL1和hiddenL2的神经元的激活值depth = numel(stack);  %网络的hidden层的总层数 2层% stack{i}.z 存储第i层的值,% stack{i}.a  %存储第i层的激活值stack{1}.a = data;     %stack{1}.a中存储输入值for i = 1 : depth    stack{i+1}.z = stack{i}.w * stack{i}.a + repmat(stack{i}.b,1,M);    stack{i+1}.a = sigmoid(stack{i+1}.z);end%计算softmax层的输出p = softmaxTheta * stack{end}.a;       p = bsxfun(@minus, p, max(p, [],1));   p = exp(p);   %   p = bsxfun(@rdivide, p, sum(p)); %【2】计算softmax的cost functioncost = -1/M * sum(sum(groundTruth .* log(p))) + lambda/2 * sum(sum(softmaxTheta.^2));%【3】计算误差项stack{depth+1}.delta = -(softmaxTheta' * (groundTruth - p)) .*  stack{end}.a .* (1-stack{end}.a);for i= depth:-1:2    stack{i}.delta = stack{i}.w' * stack{i+1}.delta .* stack{i}.a .* (1 - stack{i}.a);end%【4】计算偏导数%计算隐藏层的偏导数for i=depth : -1 : 1    stackgrad{i}.w = stack{i+1}.delta * stack{i}.a' /M;    stackgrad{i}.b = sum(stack{i+1}.delta,2) /M;end%计算softmax的偏导数softmaxThetaGrad = -1/M * (groundTruth - p) * stack{end}.a' + lambda * softmaxTheta;% -------------------------------------------------------------------------

Step 5: Test the model ——stackedAEPredict.m

%% ---------- YOUR CODE HERE --------------------------------------%  Instructions: Compute pred using theta assuming that the labels start %                from 1.M = size(data,2);  depth = numel(stack);  stack{1}.a = data; for i = 1:depth              stack{i+1}.z = stack{i}.w * stack{i}.a + repmat(stack{i}.b, 1, M);      stack{i+1}.a = sigmoid(stack{i+1}.z);  end  softmaxOut = softmaxTheta * stack{end}.a;  [~, pred] = max(softmaxOut);% -----------------------------------------------------------



                                             
0 0
原创粉丝点击