Attachment 'sheet04.m'
Download 1 function sheet04
2
3 % get the data
4 [X, Y] = twomoon(1000);
5
6 % get a subset of the data
7 figure(1)
8 NS = 10;
9 [XS, YS] = subset(NS, X, Y);
10 plot2ddata(X, Y);
11 hold on
12 plot(XS(:, 1), XS(:, 2), 'kx', 'MarkerSize', 10, 'LineWidth', 3);
13 hold off
14 title('data and example points')
15
16 % train KRR
17 figure(2);
18 K = rbfkern(1, XS, XS);
19 alpha = inv(K + 1e-6*eye(NS))*YS;
20
21 YH = rbfkern(1, X, XS) * alpha;
22
23 plot2ddata(X, YH);
24 hold on;
25 plot(XS(:,1), XS(:, 2), 'kx', 'MarkerSize', 10, 'LineWidth', 3);
26 hold off
27
28 title('learned only using sample points')
29
30 % train with KRR + Graph Laplacian regularization
31 figure(3)
32 alpha = trainGLKRR(1, 0.001, 0.1, 1, XS, YS, X);
33 YH = rbfkern(1, X, X) * alpha;
34 plot2ddata(X, YH);
35 hold on;
36 plot(XS(:,1), XS(:, 2), 'kx', 'MarkerSize', 10, 'LineWidth', 3);
37 hold off
38
39 title('learned with graph Laplacian regularizer')
40
41 function K = rbfkern(w, X, Y)
42 N = size(X, 1);
43 M = size(Y, 1);
44 XX = sum(X.*X, 2);
45 YY = sum(Y.*Y, 2);
46 D = repmat(XX, 1, M) + repmat(YY', N, 1) - 2 * X * Y';
47 K = exp(-D/(2*w));
48
49 function plot2ddata(X, Y)
50 P = (sign(Y) == 1);
51 N = (sign(Y) == -1);
52 plot(X(P, 1), X(P, 2), 'r+', X(N, 1), X(N, 2), 'bo');
53
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 %
56 % your solution below
57 %
58
59 % 1. Generate a two-moon data set with N points and some noise. Make sure
60 % that classes are not linearly seprable.
61 function [X, Y] = twomoon(N)
62 % ...
63
64 % 2. Choose random subset of size N from a data set X, Y.
65 function [X, Y] = subset(N, X, Y)
66 % ...
67
68 % 3. Compute the graph-Laplacian for Gaussian weights with width w
69 function L = graphLaplacian(w, X)
70 % ...
71
72 % 4. Train KRR with Graph-Laplacian regularization on the data
73 %
74 % Parameters:
75 % w - width for the rbf kernel
76 % g - width for the graph-Laplacian
77 % lambda - regularization constant for general smoothness
78 % tau - regularization constant for GL-regularizer
79 % XS, YS - data set with labels
80 % X - unlabeled data
81 function alpha = trainGLKRR(w, g, lambda, tau, XS, YS, X)
82 % ...
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.