最小二乘学习法

写于

机器学习

最小二乘学习法

使用随机梯度算法对高斯核模型进行最小二乘学习法的Matlab程序

% 使用随机梯度算法对高斯核模型进行最小二乘学习法的实例
% 训练样本数n为50,高斯核的带宽h为0.3
n=50;
N=500;
x=linspace(-3,3,n)';    %生成线性间距向量,生成-3到3之间的n个等间距点的行向量
X=linspace(-3,3,N)';
pix=pi*x;
y=sin(pix)./(pix)+0.1*x+0.05*randn(n,1);  %随机生成y点

hh=2*0.3^2;
t0=randn(n,1);  %产生一个n行*1列符合正态分布的随机矩阵
e=0.1;   %梯度下降幅度
for o=1:n*1000   %设置迭代次数
    i=ceil(rand*n);  %ceil取不小于x的整数,rand返回区间(0,1)的随机数
    ki=exp(-(x-x(i)).^2/hh);
    t=t0-e*ki*(ki'*t0-y(i));
    if norm(t-t0)<1e-6  %收敛结果精度要求
        break;
    end
    t0=t;
end   
K=exp(-(repmat(X.^2,1,n)+repmat(x.^2',N,1)-2*X*x')/hh);
F=K*t;

figure(1);
clf;
hold on;
axis([-2.8 2.8 -0.5 1.21]);
plot(X,F,'g-');
plot(x,y,'bo');


运行结果:

高斯核模型的L2约束的最小二乘学习法的Matlab程序

% 使用随机梯度算法对高斯核模型进行最小二乘学习法的实例
% 训练样本数n为50,高斯核的带宽h为0.3
n=50;
N=1000;
x=linspace(-3,3,n)';    %生成线性间距向量,生成-3到3之间的n个等间距点的行向量
X=linspace(-3,3,N)';
pix=pi*x;
y=sin(pix)./(pix)+0.1*x+0.05*randn(n,1);  %随机生成y点

x2=x.^2;
X2=X.^2;
hh=2*0.3^2;  % 0.3为带宽h
k=exp(-(repmat(x2,1,n)+repmat(x2',n,1)-2*x*x')/hh);
K=exp(-(repmat(X2,1,n)+repmat(x2',N,1)-2*X*x')/hh);
t1=k\y;
F1=K*t1;
t2=(k^2+0.1*eye(n))\(k*y);  % 系数0.1为约束条件lamda
F2=K*t2;

figure(1);
clf;
hold on;
axis([-2.8 2.8 -0.8 1.2]);
plot(X,F1,'g-');
plot(X,F2,'r--');
plot(x,y,'bo');
legend('LS','L2-Constrained LS');

运行结果

对使用高斯核模型的L2约束的最小二乘学习法进行交叉验证的实例

% 使用随机梯度算法对高斯核模型进行最小二乘学习法的实例
% 训练样本数n为50,高斯核的带宽h为0.3
n=50;
N=1000;
x=linspace(-3,3,n)';    %生成线性间距向量,生成-3到3之间的n个等间距点的行向量
X=linspace(-3,3,N)';
pix=pi*x;
y=sin(pix)./(pix)+0.1*x+0.05*randn(n,1);  %随机生成y点

x2=x.^2;
xx=repmat(x2,1,n)+repmat(x2',n,1)-2*x*x';
hhs=2*[0.03 0.3 3].^2;
ls=[0.0001 0.1 100];
m=5;
u=floor(m*[0:n-1]/n)+1;
u=u(randperm(n)); % randperm(n) ,返回包含1~n间没有重复元素的行向量
for hk=1:length(hhs)
    hh=hhs(hk);
    k=exp(-xx/hh);
    for i=1:m
        ki=k(u~=i,:);
        kc=k(u==i,:);
        yi=y(u~=i,:);
        yc=y(u==i,:);
        for lk=1:length(ls)
            l=ls(lk);
            t=(ki'*ki+1*eye(n))\(ki'*yi);
            fc=kc*t;
            g(hk,lk,i)=mean((fc-yc).^2);
        end
    end
end
[g1,gg1]=min(mean(g,3),[],2);
[gh1,ggh1]=min(g1);
L=ls(gg1(ggh1));
HH=hhs(ggh1);

K=exp(-(repmat(X.^2,1,n)+repmat(x2',N,1)-2*X*x')/HH);
k=exp(-xx/HH);
t=(k^2+L*eye(n))\(k*y);
F=K*t;   


figure(1);
clf;
hold on;
axis([-2.8 2.8 -0.7 1.7]);
plot(X,F,'g-');
plot(x,y,'bo');

高斯核模型的L1约束的最小二乘学习法

% 高斯核模型的最小二乘学习法
% 带宽h为0.3,正则化参数为0.1
n=50;
N=1000;
x=linspace(-3,3,n)';    %生成线性间距向量,生成-3到3之间的n个等间距点的行向量
X=linspace(-3,3,N)';
pix=pi*x;
y=sin(pix)./(pix)+0.1*x+0.2*randn(n,1);  %随机生成y点

hh=2*0.3^2;
l=0.1;
t0=randn(n,1);
x2=x.^2;
k=exp(-(repmat(x2,1,n)+repmat(x2',n,1)-2*x*x')/hh);
k2=k^2;
ky=k*y;
for o=1:1000
    t=(k2+1*pinv(diag(abs(t0))))\ky;
    if norm(t-t0)<0.01,
        break,
    end
end

K=exp(-(repmat(X.^2,1,n)+repmat(x2',N,1)-2*X*x')/hh);
F=K*t;   

figure(1);
clf;
hold on;
axis([-2.8 2.8 -1 1.5]);
plot(X,F,'g-');
plot(x,y,'bo');

TensorFlow

TensorFlow is an open source software library for numerical computation using data flow graphs.

  • 分布式功能

  • 软件套件