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. 对Kalman(卡尔曼)滤波器的理解@@zz

    1.简介(Brief Introduction) 在学习卡尔曼滤波器之前,首先看看为什么叫“卡尔曼”.跟其他著名的理论(例如傅立叶变换,泰勒级数等等)一样,卡尔曼也是一个人的名字,而跟他们不同的是,他 ...

  2. 解剖android中的闹钟app 一

    首先,看一看android市场上有哪些主流的闹钟app了,我们来进行一个简单的评测: 一.正点闹钟 这是一款源自金山技术的闹钟app,其主力创始团队都是来自于金山,其装机量,下载量都是排名第一.老样子 ...

  3. 转:TensorFlow入门(六) 双端 LSTM 实现序列标注(分词)

    http://blog.csdn.net/Jerr__y/article/details/70471066 欢迎转载,但请务必注明原文出处及作者信息. @author: huangyongye @cr ...

  4. 平均值(Mean)、方差(Variance)、标准差(Standard Deviation) (转)

    http://blog.csdn.net/xidiancoder/article/details/71341345 平均值 平均值的概念很简单:所有数据之和除以数据点的个数,以此表示数据集的平均大小: ...

  5. Mysql 创建表时错误:Tablespace for table `tablexx` exists. Please DISCARD the tablespace before IMPORT.

    在本地的一个数据库创建表时意外的出现了以下错误,说是表已经存在了 但通过desc 查看表信息时又说表不存在 在本地系统搜索表名却发现一个.ibd文件[InnoDB File] 在删除了该.ibd文件文 ...

  6. 利用Docker搭建java项目开发环境

    一.需求 一台 Ubuntu 16.0.4 LTS ,安装了Docker服务,Rancher服务,也制作了Tomcat相关的image,接下来我们就来说一下如何快速的构建一个开发环境和测试环境 二.步 ...

  7. 支付宝支付-PC电脑网站支付

    支付产品全面升级(更新时间:2017/05/05 ),若您使用的是老接口,请移步老版本即时到账文档. 支持沙盒环境的测试 此项目已开源欢迎Start.PR.发起Issues一起讨论交流共同进步 htt ...

  8. Android Studio 的 10 个你非常有可能不知道的技巧

    本文首发:http://prototypez.github.io/2016/04/19/about-10-things-you-probably-didn-t-know-you-could-do-in ...

  9. Solidworks如何把装配体做成单个零件

    先把装配体另存为IGS格式   再把IGS打开之后另存为SLDPRT格式(打开IGS需要比较长的时间,中途会弹出提示,而且IGS文件体积也比较大)   另存为SLDPRT之后体积就很小了       ...

  10. android中LitePal的使用

    网上有一篇文章写的挺好的,推荐给大家:安卓项目实战之:数据库框架 LitePal 3.0 的使用详解 LitePal是对SQLite数据库操作进行了封装,采用对象映射的方式操作SQLite数据库,简化 ...