您的位置:首页 > 其它

UFLDL教程Exercise答案(4):Softmax Regression

2016-11-21 13:13 567 查看
教程地址:http://deeplearning.stanford.edu/wiki/index.php/UFLDL_Tutorial

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

代码

Step 0: Initialize constants and parameters ——代码已给

Step 1: Load data ——代码已给

注意根据自己的文件目录和文件名修改

当前路径下 (mat和m文件在一起):  load  ***.mat; 
在下一级路径下:  load  .\下一级路径的文件名\***.mat; 
在上一级路径下:  load  ..\***.mat; 
在平行文件夹内:   load  ..\平行的文件夹\***.mat;

images = loadMNISTImages('./mnist/train-images.idx3-ubyte');
labels = loadMNISTLabels('./mnist/train-labels.idx1-ubyte');

Step 2: Implement softmaxCost ——softmaxCost.m

%% ---------- YOUR CODE HERE --------------------------------------
% Instructions: Compute the cost and gradient for softmax regression.
% You need to compute thetagrad and cost.
% The groundTruth matrix might come in handy.

M1 = theta*data; % (numClasses*numCases)
M2 = bsxfun(@minus, M1, max(M1,[],1)); %max(M,[],1)返回一个行向量,对应每一列的最大值;
%bsxfun将M中每个元素减去每列的最大值,防止数据溢出
M3 = exp(M2);
p = bsxfun(@rdivide, M3,sum(M3)); %概率矩阵,compute the hypothesis (by dividing all elements in each column by their column sum)
cost = -1/numCases*sum(sum(groundTruth.*log(p))) + lambda/2*sum(sum(theta.^2));
thetagrad = -1/numCases*((groundTruth - p)*data') + lambda*theta; %(numClasses*numCases)

% ------------------------------------------------------------------

Step 3: Gradient checking ——代码已给

检测梯度计算是否正确,调试时将 DEBUG 设置为 “true”,只有当DEBUG为“true”时才执行此步操作,真正训练预测时不执行此步操作。

Step 4: Learning parameters ——代码已给

Step 5: Testing ——softmaxPredict.m

%% ---------- YOUR CODE HERE --------------------------------------
% Instructions: Compute pred using theta assuming that the labels start
% from 1.

[~,pred] = max(theta * data); %pred存放每列最大值所在行号,即该样本所属类别

% ---------------------------------------------------------------------注:运行时将DEBUG设置为“false”。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: