softmax-练习(UFLDL)

本文详细介绍了UFLDL中softmax算法的工作原理,并通过两个关键公式解释了损失函数及参数梯度求导过程。此外,还提供了实现该算法的Matlab代码样例。

UFLDL-softmax章节详细推导了算法原理。最终凝结为两个重要公式:
损失函数和参数梯度求导公式:
这里写图片描述
1.损失函数J(θ)计算方法为:
对于样本i为例:
1)计算样本i在各个类别j上的输入θTjx(i)
2)减去结果(1)中的最大值
3)求取结果(2)中的exp值
4)归一化结果(3),得到各个类别上的概率
5)求log
得出的结果便是样本i在相应类别上的概率值(对数似然情况下);将所有样本按照1~5计算概率并求和便得到损失函数J(θ).
2. θj求导公式,直观的理解为,样本的线性组合,其中系数值跟类别相关:
1)计算样本i属于类别j的概率。即:p(y(i)=j|x(i);θ)=eθTjx(i)kl=1eθTlx(i)
2) 如果样本i属于类别j,系数值为1p,否则系数为p
3)将所有样本线性组合,得到θj的梯度。
其中,θj为向量,其他类别计算方法与此一致。

主要代码如下:
softmaxExercise.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); %权重值,第i行为第i类别的所有权重

numCases = size(data, 2); % 训练样本的个数

groundTruth = full(sparse(labels, 1:numCases, 1)); %真实类别标签
cost = 0;

thetagrad = zeros(numClasses, inputSize); % 权重值的梯度,第i行为第i类别的参数梯度值

%% ---------- 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 = bsxfun(@minus,theta*data,max(theta*data, [], 1)); %减去最大值,防止溢出  
M = exp(M); 
p = bsxfun(@rdivide, M, sum(M));    
cost = -1/numCases * groundTruth(:)' * log(p(:)) + lambda/2 * sum(theta(:) .^ 2);
thetagrad = -1/numCases * (groundTruth - p) * data' + lambda * theta;


% ------------------------------------------------------------------
% 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.


[nop, pred] = max(theta * data);


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

end
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值