UFLDL Exercise: Softmax Regression

本文介绍了一个softmax回归的实战案例,主要内容包括实现softmaxCost.m和softmaxPredict.m两个关键文件,通过具体的代码示例展示了如何进行softmax回归的成本计算和预测,并给出了经过100次迭代后的准确率。
摘要由CSDN通过智能技术生成

这是UFLDL关于softmax回归的练习题。

练习主要编写softmaxCost.m和softmaxPredict.m两个文件。

softmaxCost.m

function [cost, grad] = softmaxCost(theta, numClasses, inputSize, lambda, data, labels)

% numClasses - the number of classes 
% inputSize - the size N of the input vector
% lambda - weight decay parameter
% data - the N x M input matrix, where each column data(:, i) corresponds to
%        a single test set
% labels - an M x 1 matrix containing the labels corresponding for the input data
%

% Unroll the parameters from theta
theta = reshape(theta, numClasses, inputSize);

numCases = size(data, 2);

groundTruth = full(sparse(labels, 1:numCases, 1));
%cost = 0;

%thetagrad = zeros(numClasses, inputSize);

%% ---------- 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.
M = theta * data;
M = bsxfun(@minus, M, max(M, [], 1));
M = exp(M);
M = bsxfun(@rdivide, M, sum(M));
thetagrad = (groundTruth-M)*data'/(-numCases) + lambda*theta;
cost = groundTruth(:)'*log(M(:))/(-numCases) + sum(theta(:).^2)*lambda/2;
% ------------------------------------------------------------------
% Unroll the gradient matrices into a vector for minFunc
grad = [thetagrad(:)];
end

softmaxPredict.m

function [pred] = softmaxPredict(softmaxModel, data)

% softmaxModel - model trained using softmaxTrain
% data - the N x M input matrix, where each column data(:, i) corresponds to
%        a single test set
%
% Your code should produce the prediction matrix 
% pred, where pred(i) is argmax_c P(y(c) | x(i)).
 
% Unroll the parameters from theta
theta = softmaxModel.optTheta;  % this provides a numClasses x inputSize matrix
pred = zeros(1, size(data, 2));

%% ---------- YOUR CODE HERE --------------------------------------
%  Instructions: Compute pred using theta assuming that the labels start 
%                from 1.
%numClasses = softmaxModel.numClasses;
%inputSize = softmaxModel.inputSize;
%theta = reshape(theta, numClasses, inputSize);
M = theta*data;
M = bsxfun(@minus,M,max(M,[],1));
M = exp(M);
M = bsxfun(@rdivide,M,sum(M));
[maxv,pred] = max(M);
% ---------------------------------------------------------------------

end

迭代100次,Accuracy: 92.640%。

参考:

[1]http://deeplearning.stanford.edu/wiki/index.php/Softmax回归

[2]http://deeplearning.stanford.edu/wiki/index.php/Exercise:Softmax_Regression

[3]http://www.cnblogs.com/tornadomeet/archive/2013/03/23/2977621.html【参考此文章修改了部分实现,加快了训练速度】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值