"Deeplearning" Exercise:softmax Regression

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.