-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexampleLS_lossSurface.m
More file actions
143 lines (117 loc) · 3.65 KB
/
Copy pathexampleLS_lossSurface.m
File metadata and controls
143 lines (117 loc) · 3.65 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
% example of LS loss surface with and without the bias term for 1D
% and without bias term for 2D
%% 1D data
% solution without bias term is equivalent to setting
% w(2) to zero or x_i(2) to zero
rng(0)
% dimension of data
d = 1;
% weight vector (unknown)
w = rand(d+1,1)-0.5;
% set up
xrange = [-0.5:0.1:0.5];
N = length(xrange);
% create noisy measurements
noise = 0.01*randn(1,N);
yrange = w(1)*xrange + w(2) + noise;
% plot data
figure(1); clf;
hold on;
scatter(xrange, yrange)
xlabel('x')
ylabel('y');
% title(sprintf('true model = %3.4g x + %3.4g + noise \n recovered weights: w(1) = %3.4g, w(2) = %3.4g',w(1), w(2), wh(1),wh(2)))
set(gca,'FontSize',16,'FontWeight','bold')
% no bias/constant term
X = [xrange(:)];
y = yrange(:);
wh = X\y;
plot(xrange, wh(1)*xrange)
fprintf('recovery without constant term: w(1) = %3.4g\n',wh);
% with constant term
X = [xrange(:) ones(N,1)];
y = yrange(:);
wh = X\y;
fprintf('recovery without constant term: w(1) = %3.4g, w(2) = %3.4g\n',wh(1),wh(2));
plot(xrange, wh(1)*xrange+wh(2))
title(sprintf('true model = %3.4g x + %3.4g + noise \n recovered weights: w(1) = %3.4g, w(2) = %3.4g',w(1), w(2), wh(1),wh(2)))
set(gca,'FontSize',16,'FontWeight','bold')
legend('observed data','no constant term','with constant term')
pause
%% loss surface: y-w*x (1d)
[w1 w2] = meshgrid([-1:0.1:1]);
lossSurface = nan(size(w1));
for ii = 1:size(w1,1)
for jj = 1:size(w2,2);
figure(2);
lossSurface(ii,jj) = norm(yrange - [w1(ii,jj)*xrange]).^2;
surfc(w1,w2,lossSurface);
xlabel('w1')
ylabel('w2');
title('loss surface without constant term: $L(w) = \sum_i (w(1) x_i(1) - y_i)^2$','interpreter','latex')
end
drawnow
% pause(1/60);
end
% view(0,0);
pause
%% loss surface: y-w*x+b (1d + bias)
[w1 w2] = meshgrid([-1:0.2:1]);
lossSurface = nan(size(w1));
for ii = 1:size(w1,1)
for jj = 1:size(w2,2);
h3 = figure(3);
lossSurface(ii,jj) = norm(yrange - [w1(ii,jj)*xrange+w2(ii,jj)]).^2;
h4 = surfc(w1,w2,lossSurface);
xlabel('w1')
ylabel('w2');
title('loss surface with constant term: $L(w) = \sum_i (w(1) x_i(1) + w(2) - y_i)^2$','interpreter','latex')
end
drawnow
% pause(1/60);
end
pause
%% 2D data
% dimension of data
d = 2;
% weight vector (unknown)
w = rand(d,1)-0.5;
% set up
[xx1, xx2] = meshgrid([-0.25:0.05:0.25]);
N = numel(xx1);
% create noisy measurements
noise = 0.01*randn(N,1);
X = [xx1(:) xx2(:)];
yrange = X*w+ noise;
figure(1); clf;
hold on;
scatter3(X(:,1),X(:,2), yrange,'filled')
xlabel('x(1)')
ylabel('x(2)');
zlabel('y');
% title(sprintf('true model = %3.4g x + %3.4g + noise \n recovered weights: w(1) = %3.4g, w(2) = %3.4g',w(1), w(2), wh(1),wh(2)))
set(gca,'FontSize',16,'FontWeight','bold')
% without constant term
wh = X\yrange;
yh = X*wh;
fprintf('recovery without constant term: w(1) = %3.4g, w(2) = %3.4g\n',wh(1),wh(2));
surf(xx1, xx2, reshape(yh,sqrt(N),[]),'FaceAlpha', 0.5000);
view(60,20)
title(sprintf('true model = %3.4g x(1) + %3.4g x(2) + noise \n recovered weights: w(1) = %3.4g, w(2) = %3.4g',w(1), w(2), wh(1),wh(2)))
set(gca,'FontSize',16,'FontWeight','bold')
% loss surface y = w1x1+w2x2 (2d)
[w1 w2] = meshgrid([-0.5:0.05:0.5]);
lossSurface = nan(size(w1));
% yrange =
for ii = 1:size(w1,1)
for jj = 1:size(w2,2);
h3 = figure(3);
lossSurface(ii,jj) = norm(yrange - [w1(ii,jj)*xx1(:)+w2(ii,jj)*xx2(:)]).^2;
h4 = surfc(w1,w2,lossSurface);
xlabel('w1')
ylabel('w2');
title('loss surface without constant term: $L(w) = \sum_i (w(1) x_i(1) + w(2)x_i(2) - y_i)^2$','interpreter','latex')
end
drawnow
% pause(1/60);
end