-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.m
58 lines (52 loc) · 1.61 KB
/
demo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
% Simple demo file for using LIBSPLINE library.
%
% Learns the decision function x^2 + y^2 < 1 for points uniformly
% distributed on a unit square.
%
% Three models are trained (1) Cubic Spline with D_1 regularization (2)
% Trigonometric embedding with D_1 regularization (3) Hermite embedding
% with D_1 regularization
%
% Author: Subhransu Maji ([email protected])
% Generate training data
r = 20;
fprintf('Learning the function x^2 + y^2 < 1\n');
[XX,YY] = meshgrid(-1:1/r:1,-1:1/r:1);
X = [XX(:) YY(:)];
Y = 2*(sqrt(X(:,1).^2 + X(:,2).^2) < 1)-1 ;
% Plot training data
figure;hold on;
set(gca,'FontSize',16);
for i = 1:length(Y)
if(Y(i) > 0)
plot(X(i,1),X(i,2),'g.');
else
plot(X(i,1),X(i,2),'r.');
end
end
% Draw the decision boundary
theta = linspace(0,2*pi,100);
plot(cos(theta),sin(theta),'k-');
xlabel('x');
ylabel('y')
title(sprintf('Training data (%i points)',length(Y)));
axis equal tight; box on;
% Learn various additive models and draw the decision boundary%
encoding = [0 1 2];
linecolor = 'rgbk';
figure; hold on;
set(gca,'FontSize',16);
fprintf('Training additive models (%i points)\n', length(Y));
for j = 1:length(encoding),
tic;
paramstr = sprintf('-t %i -d 3 -r 1 -n 10 -B 1 -c 10',encoding(j));
model = train(Y,X,paramstr);
[l,a,d] = predict(Y,X,model);
plot(d(1:2*r+1),[linecolor(j) '-'],'LineWidth',2);
fprintf('Training : %s , %.2fs elapsed, Accuracy : %.2f%%\n', paramstr,toc,a(1));
end
xlabel('x');
ylabel('f(x,y)');
title('Learned f(x,y) using various embeddings');
axis tight; box on;
legend('Cubic B-Spline','Fourier', 'Hermite', 'Location', 'South');