深度学习 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是人工智能的一场革 ...
随机推荐
- jQuery数据缓存data(name, value)详解及实现
一. jQuery数据缓存的作用 jQuery数据缓存的作用在中文API中是这样描述的:“用于在一个元素上存取数据而避免了循环引用的风险”.如何理解这句话呢,看看我下面的举例,不知道合不合适,如果你有 ...
- cocos2dx 字体BMFont,Atlas
为了更加个性化,系统提供的字体,有时候没法满足我们的要求,所以cocos2dx提供了自定义字体控件. 分别是CCLabelBMFont和CCLabelAtlas,先看BMFont的效果 CCLabel ...
- zombie-phantom
zombie-phantom zombie-phantom Provides a Zombie.js shim around the PhantomJS Headless Browser. npm i ...
- VIPS:基于视觉的页面分割算法[微软下一代搜索引擎核心分页算法]
VIPS:基于视觉的页面分割算法[微软下一代搜索引擎核心分页算法] - tingya的专栏 - 博客频道 - CSDN.NET VIPS:基于视觉的页面分割算法[微软下一代搜索引擎核心分页算法] 分类 ...
- 基于 dbms_redefinition 在线重定义表
Oracle 支持在线重定义表,也就是说我们可以在修改表结构(DDL)的同时进行相关的DQL.DML操作,使得前端的DML根本感觉不到表结构实际上已经发生了变化,对于用户而言是完全透明的.当然在线重定 ...
- 【iOS知识学习】_如何判断手机是否为静音模式
目前我涉及的app要获取系统是否为静音模式,到网上搜了一下,千篇一律的都是一样的,而且都是iOS5.0以前才适应的知识,这个大家去搜一下就会很容易发现,找了很久终于找到一个5.0以后适用的,那位大神的 ...
- java实现 阿拉伯数字转换为汉字数字 算法
package test; public class NumberFormatTest { static String[] units = { "", "十", ...
- 设计模式入门之桥接模式Bridge
Abstraction:抽象部分的父类,定义须要实现的接口.维护对实现部分的引用,从而把实现桥接到Implementor中去 Implementor:实现部分的接口 RefinedAbstractio ...
- ios sdk的制作
制作sdk的主要目的是将自己的code通过接口提供给其他应用使用.接下来介绍.a 静态库的制作注意事项: 1.首先.a文件的静态库要进行随时的测试,因此需要将其放入应用中(创建一个应用,再创建一个.a ...
- ##DAY5 UIControl及其子类
##DAY5 UIControl及其子类 #pragma mark ———————UIControl——————————— UIControl初识: 1)UIControl是有控制功能的视图(比如UI ...