深度学习 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是人工智能的一场革 ...
随机推荐
- 为什么Java项目前会出现一个红色感叹号!
先看看问题,如下图所示: 造成这个问题的原因是,我把一个 jar 包删除了,然后又配了个新的进去,然后就一直有这个错误,刚开始很郁闷,怎么已经配置过儿,还出现这个问题?关键是代码里面没有报错的.郁闷的 ...
- Acegi
https://blogs.oracle.com/darcy/entry/properties_via_annotation_processing http://www.oschina.net/que ...
- [置顶] SpecDD(混合的敏捷方法模型)主要过程概述
敏捷已成为当今使用最广泛的开发方法.有趣的是,敏捷方法的流行性并不是因为它取代了其他开发方法,相反它与这些方法进行了更好地融合.现实世界众多敏捷项目的成功,也证明了敏捷将走向杂化的未来. SpecDD ...
- 一步一步学android之控件篇——ScrollView
一个手机的屏幕大小是有限的,那么我要显示的东西显示不下怎么办?这就会使用到ScrollView来进行滚动显示,他的定义如下: 可以看到ScrollView是继承于FrameLayout的,所以Scro ...
- CodeForces 22C System Administrator
把v和2结点交换, 1和v连,其它点和v之间能够互相连. #include <iostream> #include <cstdlib> #include <cstring ...
- CreateFileMapping使用方法
CreateFileMapping的MSDN翻译和使用心得 測试创建和打开文件映射的时候老是得到"句柄无效"的错误, 细致看了MSDN以后才发觉是函数认识不透, 这里把相关的解 ...
- Jquery遍历数组之$.inArray()方法介绍
$.inArray()函数用于在数组中搜索指定的值,并返回其索引值.如果数组中不存在该值,则返回-1; $.inArray(value,array) --value是要查找的值,array是被查 ...
- ExtJs目录说明
Ext开发包目录结构说明builds目录为ExtJS压缩后的代码docs目录为ExtJS的文档examples目录中是官方的演示示例locale是多国语言的资源文件, 其中ext - lang - z ...
- 下 面 这 条 语 句 一 共 创 建 了 多 少 个 对 象 : String s="a"+"b"+"c"+"d";
javac 编译可以对字符串常量直接相加的表达式进行优化, 不必要等到运行期去进行加法运算处理, 而是在编译时去掉其中的加号, 直接将其编译成一个这些常量相连的结果.题目中的第一行代码被编译器在编译时 ...
- 深入理解C/C++数组和指针
C语言中数组和指针是一种很特别的关系,首先本质上肯定是不同的,本文从各个角度论述数组和指针. 一.数组与指针的关系数组和指针是两种不同的类型,数组具有确定数量的元素,而指针只是一个标量值.数组可以在某 ...