Skip to content

Commit

Permalink
new stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
noblec04 committed Aug 28, 2024
1 parent b70f185 commit 1fb4229
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 8 deletions.
35 changes: 35 additions & 0 deletions MatlabGP/+NN/C2.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
classdef C2

properties
filter
bias
end

methods

function obj = C2(F)
obj.filter = normrnd(zeros(F,F),sqrt(1/F));
end

function [y] = forward(obj,x)

y = utils.conv2D(x,obj.filter,obj.bias);

end

function V = getHPs(obj)

V = [obj.filter(:);obj.bias(:)];
end

function obj = setHPs(obj,V)

nT = numel(obj.filter(:));

obj.filter = reshape(V(1:nT),size(obj.filter));

obj.bias = reshape(V(nT+1:end),size(obj.bias));

end
end
end
30 changes: 30 additions & 0 deletions MatlabGP/+NN/reshape.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
classdef reshape

properties
bias
end

methods

function obj = reshape()

end

function [y] = forward(obj,x)

y = x(:) + obj.bias;

end

function V = getHPs(obj)

V = [obj.bias(:)];
end

function obj = setHPs(obj,V)

obj.bias = reshape(V,size(obj.bias));

end
end
end
14 changes: 14 additions & 0 deletions MatlabGP/+utils/conv2D.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function [B] = conv2D(A,F,b)

[m,~] = size(F);
[y, x] = size(A);
y = y - m + 1;
x = x - m + 1;
B = zeros(y,x,'like',F);
for i=1:y
for j=1:x
B(i,j) = sum(sum(A(i:i+m-1, j:j+m-1)*F)) + b;
end
end

end
62 changes: 62 additions & 0 deletions MatlabGP/+utils/isoplotclasses.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function isoplotclasses(kr,dim1,dim2,varargin)

% dim1 - Vary this dimension
% dim2 - plot along this dimension
% f - plot this fidelity

input=inputParser;
input.KeepUnmatched=true;
input.addOptional('new_fig',false,@islogical); % Create a new figure
input.addOptional('lb',kr.lb_x,@isnumeric); % Lower bound of plot
input.addOptional('ub',kr.ub_x,@isnumeric); % Upper bound of plot
input.addOptional('cmap','thermal');
input.addOptional('LS','-');
input.addOptional('nL',50);
input.addOptional('mb',[]);
input.addOptional('CI',true);
input.addOptional('color','b');
input.parse(varargin{:})
in=input.Results;

if in.new_fig
figure
end

a1=in.lb(dim1);
b1=in.ub(dim1);


a2=in.lb(dim2);
b2=in.ub(dim2);

n = in.nL;

xx1 = linspace(a1,b1,n);
xx2 = linspace(a2,b2,n);

if isempty(in.mb)
mb = in.lb + 0.5*(in.ub - in.lb);
else
mb = in.mb;
end

for i = 1:n
for j = 1:n

XX = mb;
XX(dim1) = xx1(i);
XX(dim2) = xx2(j);

Yj = kr.eval([XX;XX]);
YY(i,j,:) = exp(Yj(1,:))/sum(Yj(1,:),2);
end
end

cmap = utils.cmocean('thermal',size(YY,3));

for i = 1:size(YY,3)
contour(xx2,xx1,YY(:,:,i),[0.5 0.5],'color',cmap(i,:));
hold on
end

end
183 changes: 183 additions & 0 deletions MatlabGP/examples/TestAppliedAviationProblem.asv
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@

clear all
clc

lb = [0.5 0.5 2.5*10^(-3)];
ub = [1.5 1.5 7.5*10^(-3)];

xx = lb + (ub - lb).*lhsdesign(50000,3);
yy = testFuncs.StressedPlate(xx,1);

x1 = lb + (ub - lb).*lhsdesign(5,3);
y1 = testFuncs.StressedPlate(x1,1);

x2 = [lb + (ub - lb).*lhsdesign(20,3)];
y2 = testFuncs.StressedPlate(x2,2);

x{1} = x1;
x{2} = x2;

y{1} = y1;
y{2} = y2;

%%
% ma = means.linear([1 1 1 1]);
% mb = means.linear([1 1 1]);

ma = means.zero([1 1 1 1]);
mb = means.zero();

a = kernels.RQ(2,1,[0.1 0.2 0.1 0.3]);%.periodic(1,10);
b = kernels.RQ(2,1,[0.2 0.2 0.2]);
a.signn = eps;
b.signn = 0.1;

%%
tic
for i = 1:2
Z{i} = GP(mb,b);
Z{i} = Z{i}.condition(x{i},y{i},lb,ub);
Z{i} = Z{i}.train();
end
toc

%%
tic
MF = NLMFGP(Z,ma,a);
MF = MF.condition();
MF = MF.train();
toc

%%

% layers{1} = NN.FF(3,6);
% layers{2} = NN.FF(6,6);
% layers{3} = NN.FF(6,2);
%
% acts{1} = NN.SNAKE(1);
% acts{2} = NN.SNAKE(1);
%
% lss = NN.CE();
%
% nnc = NN.NN(layers,acts,lss);

%%

% clear delta
%
% delta(:,1) = double(log(abs(MF.LOO))>median(log(abs(MF.LOO))));
% delta(:,2) = double(log(abs(MF.LOO))<=median(log(abs(MF.LOO))));
%
% tic
% nnc = nnc.train(x{1},delta);
% toc
%
% %%
% figure
% utils.isoplotclasses(nnc,1,3)


%%

% mc = means.linear(ones(1,3));%*means.sine(1,10,0,1);
% c = kernels.RQ(2,1,ones(1,3));
% c.signn = eps;
%
% LOOZ = GP(mc,c);
% LOOZ = LOOZ.condition(x{1},MF.LOO,lb,ub);
% LOOZ = LOOZ.train();


%%
figure
hold on
utils.plotLineOut(Z{1},(lb+ub)/2,1,'color','r')
utils.plotLineOut(Z{2},(lb+ub)/2,1,'color','b')

%%

figure
hold on
utils.plotLineOut(MF,(lb+ub)/2,1)

%%

1 - mean((yy - Z{1}.eval_mu(xx)).^2)./var(yy)
max(abs(yy - Z{1}.eval_mu(xx)))./std(yy)

1 - mean((yy - MF.eval_mu(xx)).^2)./var(yy)
max(abs(yy - MF.eval_mu(xx)))./std(yy)

%%

C = [50 1];

%%
for jj = 1:60

[xn,Rn] = BO.argmax(@BO.MFSFDelta,MF);
%[xn,Rn] = BO.argmax(@BO.UCB,LOOZ);

sign(1) = Z{1}.eval_var(xn)/C(1);
sign(2) = Z{2}.eval_var(xn)/C(2);

[~,in] = max(sign);

if in==1
x{1} = [x{1}; xn];
end
x{2} = [x{2}; xn];

if in==1
y{1} = [y{1}; testFuncs.StressedPlate(xn,1)];
end
y{2} = [y{2}; testFuncs.StressedPlate(xn,2)];

for ii = 1:2
Z{ii} = Z{ii}.condition(x{ii},y{ii},lb,ub);
end

MF.GPs = Z;
MF = MF.condition();

%LOOZ = LOOZ.condition(x{1},MF.LOO,lb,ub);

pc(jj,1) = size(x{1},1);
pc(jj,2) = size(x{2},1);

R2z(jj) = 1 - mean((yy - Z{1}.eval_mu(xx)).^2)./var(yy);
RMAEz(jj) = max(abs(yy - Z{1}.eval_mu(xx)))./std(yy);

R2MF(jj) = 1 - mean((yy - MF.eval_mu(xx)).^2)./var(yy);
RMAEMF(jj) = max(abs(yy - MF.eval_mu(xx)))./std(yy);

figure(3)
clf(3)
hold on
plot(pc(1:jj,1),R2z)
plot(pc(1:jj,1),R2MF)

figure(4)
clf(4)
hold on
plot(pc(1:jj,1),RMAEz)
plot(pc(1:jj,1),RMAEMF)

drawnow

end


%%

xi = lb + (ub - lb).*lhsdesign(size(x{1},1),3);
yi = testFuncs.StressedPlate(xi,1);

Zi = GP(mb,b);
Zi = Zi.condition(xi,yi,lb,ub);
Zi = Zi.train();

%%

1 - mean((yy - Zi.eval_mu(xx)).^2)./var(yy)
max(abs(yy - Zi.eval_mu(xx)))./std(yy)
Loading

0 comments on commit 1fb4229

Please sign in to comment.