深度学习 Deep Learning UFLDL 最新 Tutorial 学习笔记 1:Linear Regression
1 前言
Andrew Ng的UFLDL在2014年9月底更新了。
对于開始研究Deep Learning的童鞋们来说这真的是极大的好消息!
新的Tutorial相比旧的Tutorial添加了Convolutional Neural Network的内容。了解的童鞋都知道CNN在Computer Vision的重大影响。
而且从新编排了内容及exercises。
新的UFLDL网址为:
http://ufldl.stanford.edu/tutorial/
2 Linear Regression 理论简述
对于线性回归Linear Regression,恐怕大部分童鞋都了解。简单的说
线性回归问题就是一个目标值y取决于一组输入值x。我们要寻找一个最合适的如果Hypothesis来描写叙述这个y与x的关系。然后利用这个Hypothesis来预測新的输入x相应的y。
这是个简单的最优化问题。我们须要一个代价函数cost function来描写叙述在training set样本中的y与通过h函数预測的y之间的差距,从而利用这个cost function通过Gradient Decent梯度下降法来计算h的最优參数从而得到最优的h。
由于是通过样本让计算机“学习”合适的參数theta,因此这是一个最主要的机器学习算法。
cost function:
J(θ)=12∑i(hθ(x(i))−y(i))2=12∑i(θ⊤x(i)−y(i))2
对theta做偏导:
Differentiating the cost function J(θ) as given above with respect to a particular parameter θj gives us:
3 Linear Regression 练习
3.1 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'); % housing data 506x14
data=data'; % put examples in columns 14x506 一般这里将每一个样本放在每一列 % Include a row of 1s as an additional intercept feature.
data = [ ones(1,size(data,2)); data ]; % 15x506 添加intercept term % Shuffle examples. 乱序 目的在于之后可以随机选取training set和test sets
data = data(:, randperm(size(data,2))); %randperm(n)用于随机生成1到n的排列 % Split into train and test sets
% The last row of 'data' is the median home price.
train.X = data(1:end-1,1:400); %选择前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); %随机生成初始theta 每一个值在(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; %Start a stopwatch timer. 開始计时
options = struct('MaxIter', 200);
theta = minFunc(@linear_regression, theta, options, train.X, train.y);
fprintf('Optimization took %f seconds.\n', toc); %toc Read the stopwatch timer % Run minFunc with linear_regression_vec.m as the objective.
%
% TODO: Implement linear regression in linear_regression_vec.m
% using MATLAB's vectorization features to speed up your code.
% Compare the running time for your linear_regression.m and
% linear_regression_vec.m implementations.
%
% Uncomment the lines below to run your vectorized code.
%Re-initialize parameters
%theta = rand(n,1);
%tic;
%theta = minFunc(@linear_regression_vec, theta, options, train.X, train.y);
%fprintf('Optimization took %f seconds.\n', toc); % Plot predicted prices and actual prices from training set.
actual_prices = train.y;
predicted_prices = theta'*train.X; % Print out root-mean-squared (RMS) training error.平方根误差
train_rms=sqrt(mean((predicted_prices - actual_prices).^2));
fprintf('RMS training error: %f\n', train_rms); % Print out test RMS error
actual_prices = test.y;
predicted_prices = theta'*test.X;
test_rms=sqrt(mean((predicted_prices - actual_prices).^2));
fprintf('RMS testing error: %f\n', test_rms); % Plot predictions on test data.
plot_prices=true;
if (plot_prices)
[actual_prices,I] = sort(actual_prices); %从小到大排序价格。I为index
predicted_prices=predicted_prices(I);
plot(actual_prices, 'rx');
hold on;
plot(predicted_prices,'bx');
legend('Actual Price', 'Predicted Price');
xlabel('House #');
ylabel('House price ($1000s)');
end
3.2 linear_regression.m code
function [f,g] = linear_regression(theta, X,y)
%
% Arguments:
% theta - A vector containing the parameter values to optimize.
% X - The examples stored in a matrix.
% X(i,j) is the i'th coordinate of the j'th example.
% y - The target value for each example. y(j) is the target for example j.
% m=size(X,2);
n=size(X,1); f=0;
g=zeros(size(theta)); %
% TODO: Compute the linear regression objective by looping over the examples in X.
% Store the objective function value in 'f'.
%
% TODO: Compute the gradient of the objective with respect to theta by looping over
% the examples in X and adding up the gradient for each example. Store the
% computed gradient in 'g'. %%% YOUR CODE HERE %%% % Step 1 : Compute f cost function
for i = 1:m
f = f + (theta' * X(:,i) - y(i))^2;
end f = 1/2*f; % Step 2: Compute gradient for j = 1:n
for i = 1:m
g(j) = g(j) + X(j,i)*(theta' * X(:,i) - y(i));
end end
3.3 Result
RMS training error: 4.679871
RMS testing error: 4.865463
【本文为原创文章,转载请注明出处:blog.csdn.net/songrotek】
深度学习 Deep Learning UFLDL 最新 Tutorial 学习笔记 1:Linear Regression的更多相关文章
- 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 5:Softmax Regression
Softmax Regression Tutorial地址:http://ufldl.stanford.edu/tutorial/supervised/SoftmaxRegression/ 从本节開始 ...
- 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 3:Vectorization
1 Vectorization 简述 Vectorization 翻译过来就是向量化,各简单的理解就是实现矩阵计算. 为什么MATLAB叫MATLAB?大概就是Matrix Lab,最根本的差别于其它 ...
- 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 4:Debugging: Gradient Checking
1 Gradient Checking 说明 前面我们已经实现了Linear Regression和Logistic Regression.关键在于代价函数Cost Function和其梯度Gradi ...
- 【深度学习Deep Learning】资料大全
最近在学深度学习相关的东西,在网上搜集到了一些不错的资料,现在汇总一下: Free Online Books by Yoshua Bengio, Ian Goodfellow and Aaron C ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料【转】
转自:机器学习(Machine Learning)&深度学习(Deep Learning)资料 <Brief History of Machine Learning> 介绍:这是一 ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料
机器学习(Machine Learning)&深度学习(Deep Learning)资料 機器學習.深度學習方面不錯的資料,轉載. 原作:https://github.com/ty4z2008 ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)
##机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)---#####注:机器学习资料[篇目一](https://github.co ...
- 机器学习——深度学习(Deep Learning)
Deep Learning是机器学习中一个非常接近AI的领域,其动机在于建立.模拟人脑进行分析学习的神经网络,近期研究了机器学习中一些深度学习的相关知识,本文给出一些非常实用的资料和心得. Key W ...
- (转)深度学习(Deep Learning, DL)的相关资料总结
from:http://blog.sciencenet.cn/blog-830496-679604.html 深度学习(Deep Learning,DL)的相关资料总结 有人认为DL是人工智能的一场革 ...
随机推荐
- Linux学习之输入输出重定向
转自:http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html 多谢分享 在了解重定向之前,我们先来看看linux 的文件描述符. ...
- 关于Python网络爬虫实战笔记③
Python网络爬虫实战笔记③如何下载韩寒博客文章 Python网络爬虫实战笔记③如何下载韩寒博客文章 target:下载全部的文章 1. 博客列表页面规则 也就是, http://blog.sina ...
- 5.7.1.4 window对象
ECMAScript虽然没有指出如何直接访问Global对象,但web浏览器都是将这个全局对象作为window对象的一部分加以实现的.因此,在全局作用域中声明的所有变量和函数,就都成为了window对 ...
- HTTP 错误 401.3 - Unauthorized由于 Web 服务器上此资源的访问控制列表(ACL)解决办法
对应站点目录的IUSR的权限没设造成的...在属性——>安全——> 高级 中把IUSR用户找出来添加好就OK了 注:IUSR(匿名访问 Internet 信息服务的内置帐户)
- 使用VisualVM分析tomcat运行状况(1)
VisualVM是一款java程序性能分析与调优工具,而且还是jdk中自带的工具之一. tomcat也是一个java程序,自然也可以用它来进行监控.不过这里还是会有些问题,tomcat有两种常用的期待 ...
- android小知识之多个listview在同一界面只有item高亮
我的工程里面一个activity有两个有圆角的listview,就是 自定义的 CornerListView继承ListView, 然后 我想圆角的listview A点击之后一个item会高亮 ...
- listview 的onitemlongclick阿和onitemclick冲突,item中还有button的点击事件
listview里面item有button的,button要设置 android:focusable="false" ,listview里面如果设置了 onitemlongcli ...
- CF R303 div2 C. Woodcutters
C. Woodcutters time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Mybatis上路_06-使用Java自动生成[转]
Mybatis上路_06-使用Java自动生成 11人收藏此文章, 我要收藏发表于1个月前(2013-04-24 23:05) , 已有151次阅读 ,共0个评论 目录:[ - ] 1.编写Gener ...
- iOS设计模式——单例模式
单例模式用于当一个类只能有一个实例的时候, 通常情况下这个“单例”代表的是某一个物理设备比如打印机,或是某种不可以有多个实例同时存在的虚拟资源或是系统属性比如一个程序的某个引擎或是数据.用单例模式加以 ...