【DeepLearning】Exercise:Softmax Regression
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的更多相关文章
- 【DeepLearning】Exercise:Convolution and Pooling
Exercise:Convolution and Pooling 习题链接:Exercise:Convolution and Pooling cnnExercise.m %% CS294A/CS294 ...
- 【DeepLearning】Exercise: Implement deep networks for digit classification
Exercise: Implement deep networks for digit classification 习题链接:Exercise: Implement deep networks fo ...
- 【DeepLearning】Exercise:Self-Taught Learning
Exercise:Self-Taught Learning 习题链接:Exercise:Self-Taught Learning feedForwardAutoencoder.m function [ ...
- 【DeepLearning】Exercise:Learning color features with Sparse Autoencoders
Exercise:Learning color features with Sparse Autoencoders 习题链接:Exercise:Learning color features with ...
- 【DeepLearning】Exercise:PCA and Whitening
Exercise:PCA and Whitening 习题链接:Exercise:PCA and Whitening pca_gen.m %%============================= ...
- 【DeepLearning】Exercise:PCA in 2D
Exercise:PCA in 2D 习题的链接:Exercise:PCA in 2D pca_2d.m close all %%=================================== ...
- 【DeepLearning】Exercise:Vectorization
Exercise:Vectorization 习题的链接:Exercise:Vectorization 注意点: MNIST图片的像素点已经经过归一化. 如果再使用Exercise:Sparse Au ...
- 【DeepLearning】Exercise:Sparse Autoencoder
Exercise:Sparse Autoencoder 习题的链接:Exercise:Sparse Autoencoder 注意点: 1.训练样本像素值需要归一化. 因为输出层的激活函数是logist ...
- 论文速读(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 ...
随机推荐
- 根据ip地址获取用户所在地
java代码: package com.henu.controller; import java.io.BufferedReader; import java.io.IOException; impo ...
- 【Kafka】Kafka-数据倾斜问题-参考资料-解决方案
Kafka-数据倾斜问题-参考资料-解决方案 Spark Master at spark://node-01:7077 kafka 多线程producer 数据 不均匀_百度搜索 kafka 分片使用 ...
- 【Spark】Spark-foreachRDD需要注意的问题
Spark-foreachRDD需要注意的问题 dstream.foreachRDD_百度搜索 通过Spark Streaming的foreachRDD把处理后的数据写入外部存储系统中 - 吾心光明 ...
- nginx高效学习方法
nginx高效学习方法 nginx做为一个高性能的web服务器,相对于apache等服务器来说它的代码量并不是非常庞大,反而非常精简.看过nginx源码的人都知道,nginx将“一切皆为模块”和“所有 ...
- 【python】理想论坛爬虫1.08
#------------------------------------------------------------------------------------ # 理想论坛爬虫1.08, ...
- [海蜘蛛] 海蜘蛛 V8 全线无限试用版 免费发布破解教程
http://bbs.p52.cn/forum.php?mod=viewthread&tid=3499&extra=page%3D1&page=1&_dsign=79c ...
- 通过项目逐步深入了解Mybatis<二>
Mybatis 解决 jdbc 编程的问题 1. 数据库链接创建.释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库链接池可解决此问题. 解决:在SqlMapConfig.xml中配置数据链接池 ...
- 更新image的方法
1. 修改container 1.1 执行 docker images 查看目前的所有images: REPOSITORY TAG IMAGE ID ...
- VMware用于Site Recovery Manager 5的vSphere Replication功能一览
http://www.searchstorage.com.cn/showcontent_54838.htm 参考:深度解析SRM 5.0和vSphere Replication http://wenk ...
- linux CPU占用率高(转)
来自:http://www.cnitblog.com/houcy/archive/2012/11/28/86801.html 1.用top命令查看哪个进程占用CPU高 gateway网关进程14094 ...