Exercise:softmax Regression
Links to Exercises:Exercise:softmax Regression
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,whereEach column of data (:, i) corresponds to% a single testSet% Labels-an M x1Matrix containing the labels corresponding forThe input dataPercent Unroll the parameters fromThetatheta=reshape (Theta, numclasses, inputsize); Numcases= Size (data,2); Groundtruth= Full (sparse (labels,1: Numcases,1));%cost =0;%thetagrad =zeros (numclasses, inputsize);Percent----------YOUR CODE here--------------------------------------% instructions:compute the cost and gradient forsoftmax regression.%You need to compute Thetagrad and cost.% The Groundtruth matrix might comeinchHandy.weightdecay= (1/2) * Lambda * SUM (SUM (theta.*theta));% M1 (r, c) isTheta (R)'* x (c)M1 = Theta *data;%Preventing overflowsM1= Bsxfun (@minus, M1, Max (M1, [],1));% M2 (r, c) isExp (Theta (R)'* x (c))M2 =exp (M1);% M2 isThe predicted matrixM2=Bsxfun (@rdivide, M2, sum (M2));%1{·}operatorOnly preserve a part of positions of log (M2) M= Groundtruth. *log (M2); cost= -(1/numcases) * SUM (SUM (M)) +Weightdecay;%Thetagradthetagrad=zeros (numclasses, inputsize);%difference between ground truth and predict Valuediff= Groundtruth-M2; forI=1: Numclasses Thetagrad (i,:)= -(1/numcases) * SUM (data. * Repmat (diff (I,:), inputsize,1)) ,2)'+ lambda * theta (i,:);End%------------------------------------------------------------------% unroll the gradient matrices into a vector forMinfuncgrad=Thetagrad (:); end
Softmaxpredict.m
function [pred] =softmaxpredict (Softmaxmodel, data)% Softmaxmodel-model TrainedusingSoftmaxtrain% data-the N x M input matrix,whereEach column of data (:, i) corresponds to% a single testSet%%Your code should produce the prediction matrix% pred,wherePred (i) isArgmax_c P (Y (c) |x (i)). % unroll the parameters fromThetatheta= Softmaxmodel.opttheta; % Thisprovides a numclasses x inputsize matrix%pred = Zeros (1, Size (data,2));Percent----------YOUR CODE here--------------------------------------% Instructions:compute predusingTheta Assuming that the labels start% from 1. Result= Theta *data;%Sort by column[~,ind] =sort (Result);p Red= IND (Size (theta,1), :);% ---------------------------------------------------------------------End
"Deeplearning" Exercise:softmax Regression