UFLDL Exercise: Softmax Regression
来源:互联网 发布:淘宝商品名称字数限制 编辑:程序博客网 时间:2024/06/05 04:12
这是UFLDL关于softmax回归的练习题。
练习主要编写softmaxCost.m和softmaxPredict.m两个文件。
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.M = theta * data;M = bsxfun(@minus, M, max(M, [], 1));M = exp(M);M = bsxfun(@rdivide, M, sum(M));thetagrad = (groundTruth-M)*data'/(-numCases) + lambda*theta;cost = groundTruth(:)'*log(M(:))/(-numCases) + sum(theta(:).^2)*lambda/2;% ------------------------------------------------------------------% 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 matrixpred = zeros(1, size(data, 2));%% ---------- YOUR CODE HERE --------------------------------------% Instructions: Compute pred using theta assuming that the labels start % from 1.%numClasses = softmaxModel.numClasses;%inputSize = softmaxModel.inputSize;%theta = reshape(theta, numClasses, inputSize);M = theta*data;M = bsxfun(@minus,M,max(M,[],1));M = exp(M);M = bsxfun(@rdivide,M,sum(M));[maxv,pred] = max(M);% ---------------------------------------------------------------------end
迭代100次,Accuracy: 92.640%。
参考:
[1]http://deeplearning.stanford.edu/wiki/index.php/Softmax回归
[2]http://deeplearning.stanford.edu/wiki/index.php/Exercise:Softmax_Regression
[3]http://www.cnblogs.com/tornadomeet/archive/2013/03/23/2977621.html【参考此文章修改了部分实现,加快了训练速度】
0 0
- UFLDL Exercise:Softmax Regression
- UFLDL Exercise:Softmax Regression
- UFLDL Exercise:Softmax Regression
- UFLDL Exercise: Softmax Regression
- UFLDL教程:Exercise:Softmax Regression
- Ufldl Exercise:Softmax Regression Softmax回归练习
- Stanford UFLDL教程 Exercise:Softmax Regression
- UFLDL教程答案(4):Exercise:Softmax Regression
- UFLDL教程Exercise答案(4):Softmax Regression
- UFLDL——Exercise: Softmax Regression (softmax回归)
- 【UFLDL-exercise5-Softmax Regression】
- UFLDL 08 Softmax Regression
- UFLDL Softmax Regression 推导
- UFLDL Tutorial-Softmax Regression
- Exercise:Softmax Regression 代码示例
- UFLDL练习(PCA and Whitening && Softmax Regression)
- UFLDL学习笔记3(Softmax Regression)
- ufldl学习笔记与编程作业:Softmax Regression(vectorization加速)
- 线性布局下最左/右问题
- 初学《 NotificationBar》
- win7下 Windows Process Activation Service 服务导致的 iis无法启动的解决方法
- Linux内核Socket CAN中文文档
- 今天刚刚认真区别了一下两个文件读写方式
- UFLDL Exercise: Softmax Regression
- 利用Freemarker生成doc文件(包含list循环,ifelse判断,合并单元格,嵌入表格单元格字数过多报错等)
- Asynchronous Programming with async and await (C#)用async和await实现异步编程
- 在Spring3中,配置DataSource的方法有6种
- [java]PropertiesUtils操作属性文件
- 华信Java课第三节
- Android知识点梳理--应用本地数据的存储
- 阿里RocketMQ Quick Start
- 一步步编写一个AndroidStudio_NDK UDPClient 程序