UFLDL 教程学习笔记(二)
课程链接:http://ufldl.stanford.edu/tutorial/supervised/LogisticRegression/
这一节主要讲的是梯度的概念,在实验部分,比较之前的线性回归的梯度与通过定义来计算的梯度,统计二者之间的误差。
线性回归得到的是一个连续值,有时我们想得到0或者1这样的预测值,这就要用到logistic regression。因为要得到的是概率值,
之前的表示函数显然已经不合适了,这时需要用到新的函数来表示:

我们的目标就是对theta做优化,当x属于1时,概率值为1的概率越大越好,反之越小越好。
目标函数当然也得用新的啦(关于这个函数,可参考台大的机器学习基石:http://beader.me/mlnotebook/section3/logistic-regression.html):

作业部分就是训练识别手写0和1,需要注意的仍然是要分清各个变量的维数。跑了下训练准确率和
测试准确率都是100%
参考:http://blog.csdn.net/lingerlanlan/article/details/38390955
代码我加了点注释:
第一段代码改自ex1a_linreg.m,主要就是为了得到训练数据和测试数据,以及它们的标签。
%
%This exercise uses a data from the UCI repository:
% Bache, K. & Lichman, M. (2013). UCI Machine Learning Repository
% http://archive.ics.uci.edu/ml
% Irvine, CA: University of California, School of Information and Computer Science.
%
%Data created by:
% Harrison, D. and Rubinfeld, D.L.
% ''Hedonic prices and the demand for clean air''
% J. Environ. Economics & Management, vol.5, 81-102, 1978.
%
addpath ../common
addpath ../common/minFunc_2012/minFunc
addpath ../common/minFunc_2012/minFunc/compiled % Load housing data from file.
data = load('housing.data');
data=data'; % put examples in columns % Include a row of 1s as an additional intercept feature.
data = [ ones(1,size(data,2)); data ]; % Shuffle examples.
data = data(:, randperm(size(data,2)));%返回data的一列数据 % Split into train and test sets取得训练数据和测试数据,并取得相应的标签
% The last row of 'data' is the median home price.
train.X = data(1:end-1,1:400);
train.y = data(end,1:400); test.X = data(1:end-1,401:end);
test.y = data(end,401:end); m=size(train.X,2);
n=size(train.X,1); % Initialize the coefficient vector theta to random values.
theta = rand(n,1);%产生n行1列的在0到1之间的数字 % Run the minFunc optimizer with linear_regression.m as the objective.
%
% TODO: Implement the linear regression objective and gradient computations
% in linear_regression.m
%
tic;
% options = struct('MaxIter', 200);
% theta = minFunc(@linear_regression, theta, options, train.X, train.y);
% fprintf('Optimization took %f seconds.\n', toc); grad_check(@linear_regression,theta,200,train.X,train.y)
第二段代码是grad_check.m函数
function average_error = grad_check(fun, theta0, num_checks, varargin) delta=1e-3;
sum_error=0; fprintf(' Iter i err');
fprintf(' g_est g f\n') for i=1:num_checks
T = theta0;
j = randsample(numel(T),1);%从1~numel(T)中随机返回一个数
T0=T; T0(j) = T0(j)-delta;
T1=T; T1(j) = T1(j)+delta; [f,g] = fun(T, varargin{:});%T为目标函数,varargin为目标函数梯度
f0 = fun(T0, varargin{:});
f1 = fun(T1, varargin{:}); g_est = (f1-f0) / (2*delta);
error = abs(g(j) - g_est); fprintf('% 5d % 6d % 15g % 15f % 15f % 15f\n', ...
i,j,error,g(j),g_est,f); sum_error = sum_error + error;
end average=sum_error/num_checks;
UFLDL 教程学习笔记(二)的更多相关文章
- UFLDL 教程学习笔记(二)反向传导算法
UFLDL(Unsupervised Feature Learning and Deep Learning)Tutorial 是由 Stanford 大学的 Andrew Ng 教授及其团队编写的一套 ...
- UFLDL 教程学习笔记(四)主成分分析
UFLDL(Unsupervised Feature Learning and Deep Learning)Tutorial 是由 Stanford 大学的 Andrew Ng 教授及其团队编写的一套 ...
- UFLDL 教程学习笔记(三)自编码与稀疏性
UFLDL(Unsupervised Feature Learning and Deep Learning)Tutorial 是由 Stanford 大学的 Andrew Ng 教授及其团队编写的一套 ...
- UFLDL 教程学习笔记(一)神经网络
UFLDL(Unsupervised Feature Learning and Deep Learning)Tutorial 是由 Stanford 大学的 Andrew Ng 教授及其团队编写的一套 ...
- UFLDL 教程学习笔记(三)
教程地址:http://ufldl.stanford.edu/tutorial/supervised/SoftmaxRegression/ logstic regression是二分类的问题,如果想要 ...
- UFLDL 教程学习笔记(四)
课程地址:http://ufldl.stanford.edu/tutorial/supervised/FeatureExtractionUsingConvolution/ 在之前的练习中,图片比较小, ...
- UFLDL 教程学习笔记(六)主成分分析
教程:http://ufldl.stanford.edu/tutorial/supervised/MultiLayerNeuralNetworks/ 以及这篇博文,写的很清楚:http://blog. ...
- UFLDL 教程学习笔记(一)
ufdl的新教程,从基础学起.第一节讲的是线性回归.主要目的是熟悉目标函数,计算梯度和优化. 按着教程写完代码后,总是编译出错,一查是mex的原因,实在不想整了. 这位博主用的是向量,比较简洁:htt ...
- jfinal框架教程-学习笔记(二)
上一节介绍了jfinal框架的简单搭建,这节通过一个小例子了解jfinal的结构和特点 先上图 1.建数据库(我用的是oracle数据库,其他的相对也差不多) -- Create table crea ...
随机推荐
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- C#线程篇---解答线程之惑(2)
我们都知道,在这个行业,追求的就是用最少的时间学最多的知识,这是我写这个系列最想达到的目标,在最快的时间内,帮助更多的人学习更多的线程知识. 前一篇,讲述了线程基础,给大家铺垫了一个基础,这一篇着重介 ...
- Chapter 7(图)
1.Prim算法生成最小生成树 //Prim算法生成最小生成树 void MiniSpanTree_Prim(MGraph G) { int min,i,j,k; int adjvex[MAXVEX] ...
- mysqldump参数详细说明(转)
Mysqldump参数大全(参数来源于mysql5.5.19源码) 参数 参数说明 --all-databases , -A 导出全部数据库. mysqldump -uroot -p --al ...
- nginx 代理tcp长连接短连接配置
https://blog.csdn.net/tayinyinyueyue/article/details/78932697 nginx使用ngx_stream_core_module模块代理tcp长连 ...
- 利用SHELL的PROMPT_COMMAND添加日志审计功能,实时记录任何用户的操作到日志文件中
利用 PROMPT_COMMAND 实现命令审计功能:记录什么用户,在什么时间,做了什么操作,然后将查到的信息记录到一个文件里. 具体操作: 将以下内容追加到/etc/profile: ####### ...
- python 启航
first = 1while first<=9: sec = 1 while sec <= first: print( str(sec)+"*&qu ...
- 笔记 oracle 创建主键自增长
笔记 (1) 创建表 create table test( id number(18,2) primary key, -- 主键(unique+not null) name varchar2(100) ...
- Git记录-Git版本控制介绍
git config命令用于获取并设置存储库或全局选项.这些变量可以控制Git的外观和操作的各个方面. 如果在使用Git时需要帮助,有三种方法可以获得任何git命令的手册页(manpage)帮助信息: ...
- 已经菜到不行了 PAT 1010. Radix (25)
https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...