-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adam.py
34 lines (31 loc) · 1.21 KB
/
Adam.py
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
import numpy as np
class AdamOptim():
def __init__(self, eta=0.01, beta1=0.9, beta2=0.999, epsilon=1e-8):
self.m_dw, self.v_dw = 0, 0
self.m_db, self.v_db = 0, 0
self.beta1 = beta1
self.beta2 = beta2
self.epsilon = epsilon
self.eta = eta
def update(self, t, w, b, dw, db):
## dw, db are from current minibatch
## momentum beta 1
# *** weights *** #
self.m_dw = self.beta1*self.m_dw + (1-self.beta1)*dw
# *** biases *** #
self.m_db = self.beta1*self.m_db + (1-self.beta1)*db
## rms beta 2
# *** weights *** #
self.v_dw = self.beta2*self.v_dw + (1-self.beta2)*(dw**2)
# *** biases *** #
self.v_db = self.beta2*self.v_db + (1-self.beta2)*(db**2)
## bias correction
m_dw_corr = self.m_dw/(1-self.beta1**t)
m_db_corr = self.m_db/(1-self.beta1**t)
v_dw_corr = self.v_dw/(1-self.beta2**t)
v_db_corr = self.v_db/(1-self.beta2**t)
# print(v_db_corr)
## update weights and biases
w = w - self.eta*(m_dw_corr/(np.sqrt(v_dw_corr)+self.epsilon))
b = b - self.eta*(m_db_corr/(np.sqrt(v_db_corr)+self.epsilon))
return w, b