机器学习课程练习(四)——softmax
来源:互联网 发布:java多线程计算器 编辑:程序博客网 时间:2024/05/18 00:16
前言
斯坦福的UFLDL教程每一个章节都配有练习
本文是softmax这一章节的练习的解答
练习的目的是学习softmax算法的基本知识,并且训练一个softmax手写体数字识别器
具体内容可以浏览课程网页
注意事项
1.matlab是从1开始计数的所以为了方便起见,将所有数字0的标记变为10
2.注意利用稀疏矩阵函数sparse和full生成ground_truth
3.为了防止指数函数的上溢,将所有的指数项都减去其最大值,这并不影响分类的结果
4.善于利用bsxfun函数有助于减少代码量
5.需要利用到稀疏自动编码器一章中的l-bfgs函数,已经梯度函数验证函数和手写体数字的数据以及读取函数
softmaxCost
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.%计算h(x)hypothesis = zeros(numClasses, inputSize);hypothesis = theta * data;hypothesis = bsxfun(@minus, hypothesis, max(hypothesis, [], 1));hypothesis = exp(hypothesis);hypothesis = bsxfun(@rdivide, hypothesis, sum(hypothesis));%计算代价函数theta2 = theta .^ 2;weight_decay = 0.5 * lambda * sum(theta2(:));log_h = groundTruth .* log(hypothesis);cost = - sum(log_h(:)) ./ numCases + weight_decay;%计算梯度thetagrad = - (groundTruth - hypothesis) * data' ./ numCases + lambda * theta;% ------------------------------------------------------------------% Unroll the gradient matrices into a vector for minFuncgrad = [thetagrad(:)];end
softmaxPredict函数
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.hypothesis = exp(theta * data);[a,pred] = max(hypothesis);% ---------------------------------------------------------------------end
0 0
- 机器学习课程练习(四)——softmax
- 机器学习课程练习(五)——自我学习
- 机器学习练习(四)——多元逻辑回归
- 机器学习练习(四)——异常检测
- 基础—机器学习—softMax regression
- 机器学习课程练习(一)——稀疏自动编码机
- 机器学习课程练习(二)——向量化与手写体识别
- 机器学习课程练习(三)——PCA与whitening
- 课程练习四—problem A
- 简单易学的机器学习算法——Softmax Regression
- 简单易学的机器学习算法——Softmax Regression
- 简单易学的机器学习算法——Softmax Regression
- 简单易学的机器学习算法——Softmax Regression
- 机器学习——线性模型之softmax回归
- Coursera 机器学习(by Andrew Ng)课程学习笔记(四)——神经网络(一)
- 机器学习—课程学习(机器学习+机器视觉CS231n)
- 机器学习(四)从信息论交叉熵的角度看softmax/逻辑回归损失
- Andrew NG 机器学习课程笔记(四)
- Android之过渡动画
- Linux五种IO模型性能分析
- Web前端开发人员实用Chrome插件收集
- 蓝桥杯 十六进制转八进制
- 计算某段代码的运行时间
- 机器学习课程练习(四)——softmax
- 转:程序员关于读书和选书
- explicit构造函数
- 服务器架设:CentOS搭建LAMP详细教程
- matlab编程与工程应用(第二版) 第六章 自定义函数 笔记
- Attribute value "xx" is quoted with " which must be escaped when used within the
- win8下安装.NET Framework 3.5步骤
- MFC 线程学习之事件对象
- 黑马程序员----基本数据类型封装类