-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoy.py
More file actions
56 lines (44 loc) · 1.21 KB
/
toy.py
File metadata and controls
56 lines (44 loc) · 1.21 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
from tqdm import tqdm
import torch
from metrics import *
from problems import Toy
### Define the problem ###
F = Toy()
maps = {
"sgd": mean_grad,
"cagrad": cagrad,
"mgd": mgd,
"pcgrad": pcgrad,
}
### Start experiments ###
def run_all():
all_traj = {}
# the initial positions
inits = [
torch.Tensor([-8.5, 7.5]),
torch.Tensor([-8.5, -5.]),
torch.Tensor([9., 9.]),
]
for i, init in enumerate(inits):
for m in tqdm(["sgd", "mgd", "pcgrad", "cagrad"]):
all_traj[m] = None
traj = []
solver = maps[m]
x = init.clone()
x.requires_grad = True
n_iter = 100000
opt = torch.optim.Adam([x], lr=0.001)
for it in range(n_iter):
traj.append(x.detach().numpy().copy())
f, grads = F(x, True)
if m== "cagrad":
g = solver(grads, c=0.5)
else:
g = solver(grads)
opt.zero_grad()
x.grad = g
opt.step()
all_traj[m] = torch.tensor(traj)
torch.save(all_traj, f"toy{i}.pt")
if __name__ == "__main__":
run_all()