机器学习实现线性梯度算实现octave
2013-04-26 19:43
176 查看
最近一直在查找机器学习实现之类的问题,今天正好有机会和大家共享一下.
感悟
机器学习,感到就是数值分析等数学课程在盘算机上的一个应用。让我想起了理查德.费曼说的“数学之于物理就像做爱之于手淫"那句经典的台词,呵呵。
Octave, scilab,matlab这三种数学具工,编程风格兼容,而前两者是开源,后一是要收费的,对于机器学习说来Octave已够用,所以还是选择Octave来实现吧。
这里不对机器学习的识知做过多释解,因为有个哥们讲的真是太好了:Andrew Ng。课程义讲等(Handouts and Materials)。
用法如图所示
每日一道理
我拽着春姑娘的衣裙,春姑娘把我带到了绿色的世界里。
[2]C++实现 http://blog.sina.com.cn/s/blog_69821363010156rs.html
文章结束给大家分享下程序员的一些笑话语录:
Bphone之你们聊,我先走了!移动说:我在phone前加o,我叫o缝;苹果说:我在phone前i,我是i缝;微软说:我在phone前加w,我叫w缝;三星说:你们聊,我先走了!
将来王建宙写回忆录的时候,一定要有一句“常小兵为中国移动的发展做出了不可磨灭的贡献”。
感悟
机器学习,感到就是数值分析等数学课程在盘算机上的一个应用。让我想起了理查德.费曼说的“数学之于物理就像做爱之于手淫"那句经典的台词,呵呵。Octave, scilab,matlab这三种数学具工,编程风格兼容,而前两者是开源,后一是要收费的,对于机器学习说来Octave已够用,所以还是选择Octave来实现吧。
这里不对机器学习的识知做过多释解,因为有个哥们讲的真是太好了:Andrew Ng。课程义讲等(Handouts and Materials)。
批量线性规划代码
##batch_gradient.m ## -*- texinfo -*- ## @deftypefn {Function File} {} [ theta ] = batch_gradient ( x, y) ## Return the parameter of linear founction where y = theta[2:n+1]*x + theta(1). ## where n is the row of matrix x. ## It use batch gradient algorithm obviously. ## For example: ## ## @example ## @group ## x=[1 4;2 5;5 1; 4 2] y = [ 19 26 19 20] ## batch_gradient (x, y) ## @result{} [0.0060406 2.9990063 3.9990063] ## @end group ## @end example ## @seealso{stichastic_gradient} ## @end deftypefn ## Author: xiuleili <xiuleili@XIULEILI> ## Created: 2013-04-26 function [ theta ] = batch_gradient ( x, y) [n,m]=size(x); [my,ny]=size(y); theta = rand(1, m+1); if(ny ~= n | my!= 1) error("Error: x should be a matrix with(n,m) and y must be (1,n), where n is the count of training samples."); end; one = ones(n,1); X = [one x]'; learning_rate = 0.01; error = 1; threshold = 0.000001; times = 0; start_time = clock (); while error > threshold theta += learning_rate * (y - theta*X) *X'; error = sum((theta * X - y).^2) / 2; times += 1; printf("[%d] the current err is: %f", times, error); disp(theta); if(times > 10000000000) break; end; end; end_time = clock (); disp( seconds(end_time - start_time)); endfunction
用法如图所示
每日一道理
我拽着春姑娘的衣裙,春姑娘把我带到了绿色的世界里。
随机线性梯度源码
##stochastic_gradient.m ### -*- texinfo -*- ## @deftypefn {Function File} {} [ theta ] = stochastic_gradient ( x, y) ## Return the parameter of linear founction where y = theta[2:n+1]*x + theta(1). ## where n is the row of matrix x. ## It use stochastic gradient algorithm obviously. ## For example: ## ## @example ## @group ## x=[1 4;2 5;5 1; 4 2] y = [ 19 26 19 20] ## batch_gradient (x, y) ## @result{} [0.0060406 2.9990063 3.9990063] ## @end group ## @end example ## @seealso{batch_gradient} ## @end deftypefn ## Author: xiuleili <xiuleili@XIULEILI> ## Created: 2013-04-26 function [ theta ] = stochastic_gradient (x,y) [n,m] = size(x); [my,ny] = size(y); if ny!=n | my != 1 error("Error: x should be a matrix with(n,m) and y must be (1,n), where n is the count of training samples."); end X = [ones(n,1) x]'; theta = rand(1, m+1); learning_rate = 0.01; errors = 1; threshold=0.000001; times = 0; start_time = clock (); while errors > threshold for k=[1:n] xx = X(:,k); theta += learning_rate * (y(k)-theta*xx)*xx'; end errors = sum((y-theta*X).^2); times ++; printf("[%d] errors = %f", times, errors); disp(theta); if(times > 10000000000) break; end end end_time = clock (); disp( seconds(end_time - start_time)); endfunction
备注
seconds是一自定义函数:## seconds ## Author: xiuleili <xiuleili@XIULEILI> ## Created: 2013-04-26 function [ ret ] = seconds (t) t=round(t); ret = t(6) + t(5)*60 + t(4)*3600+t(3)*3600*24; endfunction
考参:
[1]易网公开课, 机器学习 http://v.163.com/special/opencourse/machinelearning.html[2]C++实现 http://blog.sina.com.cn/s/blog_69821363010156rs.html
文章结束给大家分享下程序员的一些笑话语录:
Bphone之你们聊,我先走了!移动说:我在phone前加o,我叫o缝;苹果说:我在phone前i,我是i缝;微软说:我在phone前加w,我叫w缝;三星说:你们聊,我先走了!
将来王建宙写回忆录的时候,一定要有一句“常小兵为中国移动的发展做出了不可磨灭的贡献”。
相关文章推荐
- 线性梯度算实现octave
- 斯坦福机器学习3:线性回归、梯度下降和正规方程组的matlab实现
- 运用TensorFlow进行简单实现线性回归、梯度下降示例
- 【机器学习详解】线性回归、梯度下降、最小二乘的几何和概率解释
- 机器学习 —— 基础整理(五)线性回归;二项Logistic回归;Softmax回归及其梯度推导;广义线性模型
- 【机器学习】线性回归的梯度下降法 续
- 机器学习-梯度下降法实现线性回归
- 机器学习深度学习基础笔记(2)——梯度下降之手写数字识别算法实现
- [置顶] 【算法 机器学习】MATLAB、R、python三种编程语言实现简单线性回归算法比较
- 机器学习实践系列 1 线性代数计算的python实现
- 机器学习理论篇之线性回归(python实现)
- 线性单元和梯度下降--代码实现
- 机器学习入门:线性回归及梯度下降
- 斯坦福大学机器学习笔记--第一周(5.线性回归的梯度下降)
- Machine Learning(Stanford)| 斯坦福大学机器学习笔记--第一周(5.线性回归的梯度下降)
- 吴恩达 机器学习第二周 logistic_regression 单层网络梯度下降法实现
- 机器学习(4)-理解SVM的损失函数和梯度表达式的实现+编程总结
- 吴恩达网易公开课_机器学习【第2课】笔记:线性回归,梯度,正规方程组
- 机器学习之线性回归python实现
- 线性回归及梯度下降-机器学习1