-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSGD.py
33 lines (23 loc) · 752 Bytes
/
SGD.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
import numpy as np
import time
def sgd_direction(M, w, size):
ind = np.random.choice(M.n, size, replace=False)
_, mean_grad = M.first_oracle(w, ind)
return -mean_grad
def sgd_step_size(k, nu=50, theta=0.6):
return nu / (k + 1) ** theta
def sgd(M, e, iter_num, batch_size):
M.total_time = time.time()
w = M.w_0
func_0 = M.null_oracle(w)
M.r_k.append(func_0)
for _ in range(iter_num):
direction = sgd_direction(M, w, batch_size)
step_size = sgd_step_size(M.iter_num)
w += step_size * direction
M.iter_num += 1
if M.iter_num % 5 == 0:
func_k, _ = M.first_oracle(w)
M.r_k.append(func_k)
M.total_time = time.time() - M.total_time
return w