-
Notifications
You must be signed in to change notification settings - Fork 0
/
adam_optimization.m
55 lines (39 loc) · 1.53 KB
/
adam_optimization.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
function [optimal_x, min_loss, approximated_function, x_values, cost_values, error_values, y_values_original, y_values_approximated] = adam_optimization()
alpha = 0.01;
beta1 = 0.9;
beta2 = 0.999;
epsilon = 1e-8;
num_iterations = 1e4;
x = 0.5;
x_values = [];
y_values_original = [];
y_values_approximated = [];
cost_values = zeros(1, num_iterations);
error_values = zeros(1, num_iterations);
for i = 1:num_iterations
gradient = nonSmoothGradient(@nonSmoothLoss, x, epsilon);
m = 0;
v = 0;
m = beta1 * m + (1 - beta1) * gradient;
v = beta2 * v + (1 - beta2) * (gradient.^2);
m_hat = m / (1 - beta1^i);
v_hat = v / (1 - beta2^i);
x = x - alpha * m_hat ./ (sqrt(v_hat) + epsilon);
x_values = [x_values, x];
y_values_original = [y_values_original, nonSmoothLoss(x)];
y_values_approximated = [y_values_approximated, nonSmoothLoss(x)];
cost_values(i) = sum(y_values_approximated.^2) / i;
error_values(i) = sqrt(cost_values(i));
end
optimal_x = x;
min_loss = nonSmoothLoss(x);
approximated_function = y_values_approximated;
end
function loss = nonSmoothLoss(x)
loss = abs(x);
end
function gradient = nonSmoothGradient(f, x, epsilon)
f_plus = f(x + epsilon);
f_minus = f(x - epsilon);
gradient = (f_plus - f_minus) / (2 * epsilon);
end