最近一直在查找機器學習實作之類的問題,今天正好有機會和大家共享一下.
感悟
機器學習,感到就是數值分析等數學課程在盤算機上的一個應用。讓我想起了理查德.費曼說的“數學之于實體就像做愛之于手淫"那句經典的台詞,呵呵。
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
用法如圖所示
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLi0zaHRGcWdUYuVzVa9GczoVdG1mWfVGc5RHLwkzX39GZhh2csATMflHLwEzX4xSZz91ZsADMx8FdsYkRGZkRG9lcvx2bjxSa2EWNhJTW1AlUxEFeVRUUfRHelRHL2EzXlpXazxyayFWbyVGdhd3LcV2Zh1Wa9M3clN2byBXLzN3btg3PnVGcq5iYhdzYzQWYiNTN0EGNkFWN3ImN5gTZkFWNxgDNyUzY58CXzAzLchDMxIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjL2M3Lc9CX6MHc0RHaiojIsJye.jpeg)
每日一道理
我拽着春姑娘的衣裙,春姑娘把我帶到了綠色的世界裡。
随機線性梯度源碼
##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