博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【DeepLearning】Exercise:Softmax Regression
阅读量:5736 次
发布时间:2019-06-18

本文共 2504 字,大约阅读时间需要 8 分钟。

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, 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 thetatheta = 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.weightDecay = (1/2) * lambda * sum(sum(theta.*theta));% M1(r, c) is theta(r)' * x(c)M1 = theta * data;% preventing overflowsM1 = bsxfun(@minus, M1, max(M1, [], 1));% M2(r, c) is exp(theta(r)' * x(c))M2 = exp(M1);% M2 is the predicted matrixM2 = bsxfun(@rdivide, M2, sum(M2));% 1{·} operator only 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;for i=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 for minFuncgrad = 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 thetatheta = 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.result = theta * data;% sort by column[~,ind] = sort(result);pred = ind(size(theta,1), :);% ---------------------------------------------------------------------end

 

转载地址:http://verwx.baihongyu.com/

你可能感兴趣的文章
AJAX
查看>>
BitTorrent Sync
查看>>
tomcat集群session复制
查看>>
get和post
查看>>
国庆节游嵩山
查看>>
phantomjs 的缓存
查看>>
安装emulator
查看>>
NAS上创建 iSCSI并挂载
查看>>
云时代,你离专业企业移动工作平台之间,还差什么—Cnskype
查看>>
【深入Cocos2d-x】使用MVC架构搭建游戏Four
查看>>
前端jsp页面script引入url项目名使用${appName}
查看>>
整理python小爬虫
查看>>
Matrix Computations 1
查看>>
1111 Online Map
查看>>
v-bind:的基本用法
查看>>
053:Field的常用参数详解:
查看>>
BeautifulSoup解析库详解
查看>>
JS基础属性跟运算
查看>>
Junit4学习(四)Junit4常用注解
查看>>
JQuery学习笔记 [Ajax实现新闻点评功能] (6-3)
查看>>