Skip to content

Commit

Permalink
Added matrix kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
noblec04 committed Jul 12, 2024
1 parent 6b26047 commit 708793a
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 21 deletions.
40 changes: 40 additions & 0 deletions MatlabGP/+kernels/EQ_matrix.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
classdef EQ_matrix<kernels.Kernel

properties
theta
end

methods

function obj = EQ_matrix(scale,theta)
obj.scale = scale;
obj.scales{1} = scale;
obj.theta = theta;
obj.thetas{1} = theta;
obj.kernels{1} = obj;
obj.w.map = 'none';
obj.warping{1} = obj.w;
end

function [K] = forward_(~,x1,x2,theta)

n=0;
for i = 1:size(x1,2)
for j = 1:size(x2,2)
n=n+1;
M(i,j) = theta(n);
end
end

for i = 1:size(x1,1)
for j = 1:size(x2,1)
d(i,j) = x1(i,:)*M*x2(j,:)';
end
end

K = exp(-d.^2);

end

end
end
42 changes: 21 additions & 21 deletions MatlabGP/GP.m
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@
tk0 = obj.kernel.getHPs();

tklb = 0*tk0 + 0.001;
tkub = 0*tk0 + 4;
tkub = 0*tk0 + 10;

tlb = [tmlb tklb];
tub = [tmub tkub];
Expand All @@ -259,30 +259,30 @@

func = @(x) obj.LL(x,regress,ntm);

% xxt = tlb + (tub - tlb).*lhsdesign(500*length(tlb),length(tlb));
%
% for ii = 1:size(xxt,1)
% LL(ii) = func(xxt(ii,:));
% end
%
% LL = exp(1 + LL - max(LL));
%
% theta = sum(xxt.*LL')/sum(LL);

for i = 1:2
tx0 = tlb + (tub - tlb).*rand(1,length(tlb));

opts = optimoptions('fmincon','SpecifyObjectiveGradient',true,'Display','none');

[theta{i},val(i)] = fmincon(func,tx0,[],[],[],[],tlb,tub,[],opts);
%[theta{i},val(i)] = bads(func,tx0,tlb,tub);
%[theta{i},val(i)] = VSGD(func,tx0,'lr',0.02,'lb',tlb,'ub',tub,'gamma',0.0001,'iters',400,'tol',1*10^(-4));
xxt = tlb + (tub - tlb).*lhsdesign(500*length(tlb),length(tlb));

for ii = 1:size(xxt,1)
LL(ii) = func(xxt(ii,:));
end

[mval,i] = min(val);
LL = exp(1 + LL - max(LL));

theta = sum(xxt.*LL')/sum(LL);

theta = theta{i};
% for i = 1:2
% tx0 = tlb + (tub - tlb).*rand(1,length(tlb));
%
% opts = optimoptions('fmincon','SpecifyObjectiveGradient',true,'Display','none');
%
% [theta{i},val(i)] = fmincon(func,tx0,[],[],[],[],tlb,tub,[],opts);
% %[theta{i},val(i)] = bads(func,tx0,tlb,tub);
% %[theta{i},val(i)] = VSGD(func,tx0,'lr',0.02,'lb',tlb,'ub',tub,'gamma',0.0001,'iters',400,'tol',1*10^(-4));
%
% end
%
% [mval,i] = min(val);
%
% theta = theta{i};

if regress
obj.kernel.signn = theta(end);
Expand Down
43 changes: 43 additions & 0 deletions MatlabGP/testMatrixKernel.asv
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@


clear all
close all
clc

xx = lhsdesign(100,2);

q = kernels.EQ_matrix(1,[2 0 0 3]);

q.signn = 0;

[K, dK] = q.build(xx,xx);
%%
f1 = @(x) (6*x(:,1)-2).^2.*sin(12*x(:,2)-4);%.*sin(24*x-1);

x1 = lhsdesign(50,2);
y1 = f1(x1)+normrnd(0*x1(:,1),0*x1(:,1));

%%
a = means.const(1);

q = kernels.EQ_matrix(1,[2 -0.5 0.5 3]);

q.signn = 0.01;

Z = GP(a,q);

Z1 = Z.condition(x1,y1);

figure(1)
clf(1)
utils.plotSurf(Z1,2,1)

%%
tic
[Z2] = Z1.train();
toc
%%
figure
hold on
utils.plotSurf(Z2,2,1)
view(20,20)
43 changes: 43 additions & 0 deletions MatlabGP/testMatrixKernel.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@


clear all
close all
clc

xx = lhsdesign(100,2);

q = kernels.EQ_matrix(1,[2 0 0 3]);

q.signn = 0;

[K, dK] = q.build(xx,xx);
%%
f1 = @(x) (6*x(:,1)-2).^2.*sin(12*x(:,2)-4);%.*sin(24*x-1);

x1 = lhsdesign(50,2);
y1 = f1(x1)+normrnd(0*x1(:,1),0*x1(:,1));

%%
a = means.const(1);

q = kernels.EQ_matrix(1,[1 -1 1 1]);

q.signn = 0.01;

Z = GP(a,q);

Z1 = Z.condition(x1,y1);

figure(1)
clf(1)
utils.plotSurf(Z1,2,1)

%%
tic
[Z2] = Z1.train();
toc
%%
figure
hold on
utils.plotSurf(Z2,2,1)
view(20,20)

0 comments on commit 708793a

Please sign in to comment.