-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadam.py
More file actions
78 lines (54 loc) · 1.57 KB
/
Copy pathadam.py
File metadata and controls
78 lines (54 loc) · 1.57 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
from collections.abc import Callable
from typing import TypeAlias
import numpy as np
Real: TypeAlias = float
Theta: TypeAlias = np.ndarray
ObjectiveFunction: TypeAlias = Callable[[Theta], Real]
Counter = 0
#MAX_COUNTER = 3500
MAX_COUNTER = 2694
def is_converged(theta_t: Theta) -> bool:
global Counter
if Counter > MAX_COUNTER:
return True
else:
Counter += 1
return False
def gradient(
func: ObjectiveFunction,
theta_t: Theta
) -> Theta:
dx = 0.001
return (
func(theta_t + dx) - func(theta_t)
) / (theta_t + dx - theta_t)
def adam(
objective_function: ObjectiveFunction,
theta_0: Theta,
alpha: Real = 0.001,
beta_1: Real = 0.9,
beta_2: Real = 0.999,
step_size: Real = 1e-8,
) -> Theta:
m_t: np.ndarray = np.ndarray(theta_0.shape)
v_t: np.ndarray = np.ndarray(theta_0.shape)
t = 0
theta_t = theta_0
while not is_converged(theta_t):
t = t + 1
g_t = gradient(objective_function, theta_t)
m_t = beta_1 * m_t + (1 - beta_1) * g_t
v_t = beta_2 * v_t + (1 - beta_2) * (g_t ** 2)
m_hat_t = m_t / (1 - beta_1 ** t)
v_hat_t = v_t / (1 - beta_2 ** t)
theta_t = theta_t - (
alpha * m_hat_t / (np.sqrt(v_hat_t) + step_size)
)
print(f"theta_t = {theta_t}, g_t = {g_t} t = {t} m_t = {m_t}")
return theta_t
def objective_function(theta: Theta) -> Real:
return theta[0] ** 2 + theta[1] ** 2 + theta[2] ** 2
adam(
objective_function=objective_function,
theta_0=np.random.rand(3),
)