Exercise:Softmax Regression

习题的链接: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 matrix containing the labels corresponding for the input data
% % Unroll the parameters from theta
theta = reshape(theta, numClasses, inputSize); numCases = size(data, ); % labels row, numCases col
groundTruth = full(sparse(labels, :numCases, ));
cost = ; 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, [], ));
M = exp(M);
M = bsxfun(@rdivide, M, sum(M));
diff = groundTruth - M; cost = -(/numCases) * sum(sum(groundTruth .* log(M))) + (lambda/) * sum(sum(theta .* theta));
for i=:numClasses
thetagrad(i, :) = -(/numCases) * (sum(data .* repmat(diff(i, :), inputSize, ), ))' + lambda * theta(i, :);
end
% ------------------------------------------------------------------
% 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(, size(data, )); %% ---------- YOUR CODE HERE --------------------------------------
% Instructions: Compute pred using theta assuming that the labels start
% from . [~, pred] = max(theta * data); % --------------------------------------------------------------------- end

Accuracy: 92.640%

【DeepLearning】Exercise:Softmax Regression的更多相关文章

  1. 【DeepLearning】Exercise:Convolution and Pooling

    Exercise:Convolution and Pooling 习题链接:Exercise:Convolution and Pooling cnnExercise.m %% CS294A/CS294 ...

  2. 【DeepLearning】Exercise: Implement deep networks for digit classification

    Exercise: Implement deep networks for digit classification 习题链接:Exercise: Implement deep networks fo ...

  3. 【DeepLearning】Exercise:Self-Taught Learning

    Exercise:Self-Taught Learning 习题链接:Exercise:Self-Taught Learning feedForwardAutoencoder.m function [ ...

  4. 【DeepLearning】Exercise:Learning color features with Sparse Autoencoders

    Exercise:Learning color features with Sparse Autoencoders 习题链接:Exercise:Learning color features with ...

  5. 【DeepLearning】Exercise:PCA and Whitening

    Exercise:PCA and Whitening 习题链接:Exercise:PCA and Whitening pca_gen.m %%============================= ...

  6. 【DeepLearning】Exercise:PCA in 2D

    Exercise:PCA in 2D 习题的链接:Exercise:PCA in 2D pca_2d.m close all %%=================================== ...

  7. 【DeepLearning】Exercise:Vectorization

    Exercise:Vectorization 习题的链接:Exercise:Vectorization 注意点: MNIST图片的像素点已经经过归一化. 如果再使用Exercise:Sparse Au ...

  8. 【DeepLearning】Exercise:Sparse Autoencoder

    Exercise:Sparse Autoencoder 习题的链接:Exercise:Sparse Autoencoder 注意点: 1.训练样本像素值需要归一化. 因为输出层的激活函数是logist ...

  9. 论文速读(Chuhui Xue——【arxiv2019】MSR_Multi-Scale Shape Regression for Scene Text Detection)

    Chuhui Xue--[arxiv2019]MSR_Multi-Scale Shape Regression for Scene Text Detection 论文 Chuhui Xue--[arx ...

随机推荐

  1. 机器学习中的损失函数 (着重比较:hinge loss vs softmax loss)

    https://blog.csdn.net/u010976453/article/details/78488279 1. 损失函数 损失函数(Loss function)是用来估量你模型的预测值 f( ...

  2. 知乎:GAN 的发展对于研究通用人工智能有什么意义?

    https://www.zhihu.com/question/57668112/answer/155367561 Lyken 愿以有涯随无涯 收录于 编辑推荐知乎圆桌 · 296 人赞同了该回答 资历 ...

  3. Topic Model的分类和设计原则

    Topic Model的分类和设计原则 http://blog.csdn.net/xianlingmao/article/details/7065318 topic model的介绍性文章已经很多,在 ...

  4. nginx不浏览直接下载文件

    当我们使用Nginx时,如果要让一些附件比如txt,pdf,doc等不直接在浏览器打开,而弹出另存为的对话框(也就是下载),则可以在nginx里添加如下配置: location /{if ($requ ...

  5. IntelliJ idea配置python

    为什么选择Intellij?因为我需要系统地管理python工程,Intellij可断点调试. 1.下载IntelliJ idea 在百度中搜索“IntelliJ idea”,并点击官网地址进行下载: ...

  6. (算法)Travel Information Center

    题目: Aps Island has many cities. In the summer, many travellers will come to the island and attend fe ...

  7. AS打开速度慢,AS项目导入慢,新建项目导入慢

    1.AS打开速度慢 在Android Studio的bin目录里,打开idea.prooperties文件,添加disable.android.first.run=true 再次打开时,启动加快 2. ...

  8. ccc如何在一台windows主机上搭建mysql主从复制

    参考:http://www.cnblogs.com/wzjbk/p/6266899.htmlc 进入mysql: 进入到mysql的bin目录下才可以输入 mysql -hlocalhost -uro ...

  9. android中动态修改ImageView控件的宽高度

    本例实现了动态修改ImageView控件的宽高度,有两个按钮,一个按钮实现放大image,一个按钮实现缩小image activity_main.xml <?xml version=" ...

  10. Windows 下 Git 安装与初始配置

    官方下载地址:https://git-scm.com/download/win,我下载的最新版是 Git-2.15.1.2-64-bit.exe . Windows 下安装步骤 1.相关信息,直接“ ...