ufldl学习笔记与编程作业:Softmax Regression(softmax回归)

ufldl出了新教程。感觉比之前的好,从基础讲起。系统清晰,又有编程实践。

在deep learning高质量群里面听一些前辈说,不必深究其它机器学习的算法,能够直接来学dl。

于是近期就開始搞这个了。教程加上matlab编程,就是完美啊。

新教程的地址是:http://ufldl.stanford.edu/tutorial/

本节学习链接:http://ufldl.stanford.edu/tutorial/supervised/SoftmaxRegression/

softmax回归事实上是逻辑回归的扩展形式,

逻辑回归通经常使用作2类的分类器,

softmax则用作多类的分类器。

从数学形式来说,事实上逻辑回归就是softmax回归中k=2的情况。这点教程里也说了。

softmax的目标函数和參数的偏导数教程推导也非常清楚。

对于编程作业。因为对matlab实现不熟,跳了非常多坑。

弄了非常久,并且还仅仅是用for循环来实现的。

这次最终体会到了,for循环的性能之差了。迭代了200次。1个多小时。

也跟这个模型比前两个模型复杂有关。

先贴第一个版本号的代码吧。以后想出了向量化的编程再补上。

下面是softmax_regression.m的代码

function [f,g] = softmax_regression_vec(theta, X,y)
%
% Arguments:
% theta - A vector containing the parameter values to optimize.
% In minFunc, theta is reshaped to a long vector. So we need to
% resize it to an n-by-(num_classes-1) matrix.
% Recall that we assume theta(:,num_classes) = 0.
%
% X - The examples stored in a matrix.
% X(i,j) is the i'th coordinate of the j'th example.
% y - The label for each example. y(j) is the j'th example's label.
%
m=size(X,2);
n=size(X,1); %theta本来是矩阵,传參的时候,theta(:)这样进来的。是一个vector,仅仅有一列,如今我们得把她变为矩阵
% theta is a vector; need to reshape to n x num_classes.
theta=reshape(theta, n, []);
num_classes=size(theta,2)+1; % initialize objective value and gradient.
f = 0;
g = zeros(size(theta)); h = theta'*X;%h(k,i)第k个theta。第i个样本 麻痹还是得循环求啊
a = exp(h);
a = [a;ones(1,size(a,2))];%加行
b = sum(a,1); for i=1:m
for j=1:num_classes
if y(i)!=j
continue;
end
f+=log2(a(j,i)/b(i));
end
end
f=-f;%符号 flag=0;
for j=1:num_classes-1
for i=1:m
if (y(i)==j)
flag =1;
else
flag=0;
end
g(:,j)+=X(:,i)*(a(j,i)/b(i)-flag);
end
end
%
% TODO: Compute the softmax objective function and gradient using vectorized code.
% Store the objective function value in 'f', and the gradient in 'g'.
% Before returning g, make sure you form it back into a vector with g=g(:);
%
%%% YOUR CODE HERE %%% g=g(:); % make gradient a vector for minFunc

下面是执行结果:

旧教程http://deeplearning.stanford.edu/wiki/index.php/Exercise:Softmax_Regression

也有softmax的编程作业。里面也是识别手写体数字。

当中提到准确率的问题。

Our implementation achieved an accuracy of 92.6%.
If your model's accuracy is significantly less (less than 91%), check your code, ensure that you are using the trained weights, and that you are training your model on the full 60000 training images. Conversely, if your accuracy is too high (99-100%), ensure
that you have not accidentally trained your model on the test set as well.

也就是说,从准确率来说,我的代码还是能够的。

接下来就是想办法实现向量化编程,加高速度了。

假设您有什么好想法。记得分享一下哦!

本文作者:linger

本文链接:http://blog.csdn.net/lingerlanlan/article/details/38410123

版权声明:本文博客原创文章,博客,未经同意,不得转载。

最新文章

  1. canvas arcTo()用法详解
  2. 完成对数据库的CRUD操作
  3. Pairs Forming LCM(素因子分解)
  4. UE4 自定义物理表面类型(Surface Type)
  5. JAVA入门第二季 第一章 类和对象
  6. D-Bus,kdbus和Binder
  7. C++ 不使用virtual实现多态
  8. ios @property
  9. erlang nif小结
  10. 使用Skaffold一键将项目发布到Kubernetes
  11. vs2017 exe在Linux上运行
  12. C++雾中风景12:聊聊C++中的Mutex,以及拯救生产力的Boost
  13. asp.net C# int 类型在32/64位环境下取值范围无变化
  14. macbook air 获取root权限
  15. 001.DHCP简介
  16. button 左边图片右边文字样式
  17. SL 的 DATAGRID中如何加入计算列?
  18. python 静态 封装 继承 mro 接口 super
  19. Web 服务器被配置为不列出此目录的内容
  20. 来自一个大三开学三周的huster的迷茫与失措

热门文章

  1. JS跑马灯
  2. HDU 4597 Play Game 2013 ACM-ICPC吉林通化全国邀请赛H题
  3. STL学习总结
  4. 算法起步之Prim算法
  5. A Game of Thrones(16) - Edard
  6. JavaEE session机制
  7. select下拉菜单反显不可改动,且submit能够提交数据
  8. oschina jQuery 插件
  9. android之listView定位到指定行同一时候隐藏输入键盘
  10. UpdatePanel Repeater内LinkButton造成页面刷新问题