-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_batch_fix_lambda.py
165 lines (140 loc) · 6.62 KB
/
benchmark_batch_fix_lambda.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import abc
import torch
import argparse
import numpy as np
from losses import *
from networks import RNNOptimizer
import matplotlib.pyplot as plt
import tree
parser = argparse.ArgumentParser()
parser.add_argument("--n_player", type=int, default=2)
parser.add_argument("--n_action", type=int, default=1)
parser.add_argument("--lamb", type=float, default=1)
parser.add_argument("--lr", type=float, default=0.01)
parser.add_argument("--game_list", type=str)
parser.add_argument("--visualize", action="store_true")
parser.add_argument("--print_update", action="store_true")
parser.add_argument("--type", type=str)
args = parser.parse_args()
def detach(x):
x.detach_()
x.requires_grad = True
return x
n_player = 2
def load_games_list(args):
lines = open(args.game_list).readlines()
array_lines = []
for line in lines:
array_lines.append(np.array(list(map(float, line.strip().split(',')))))
return array_lines
def r(n):
x = torch.cos(torch.FloatTensor([n])).cuda()
y = torch.sin(torch.FloatTensor([n])).cuda()
return [x, y]
from tqdm import tqdm
def main():
lines, lambdas = load_games_list(args)
players_w = [r(np.random.rand() * 1000) for _ in range(10)]
initial_w = list(players_w)
counts = []
for idx, loss_line in tqdm(enumerate(lines)):
ws = []
updates = []
grads_ = []
Ags = []
Sgs = []
loss, _ = loss_quadratic(1, *list(loss_line))
for lr in [args.lr]:
acount = 0
initial_w = [r(2**int(_)) for _ in range(1, 5)]
for wi in initial_w:
players_w = wi
ws.append(list(wi))
losses = []
for w in players_w:
w.requires_grad = True
w.retain_grad()
w.cuda()
count = 0
while count < 1000:
grads = torch.stack(grad(loss, players_w)).cuda()
grads_.append(grads)
S, A = decompose(grads, players_w) # (np * na) x (np * na)
Ate = torch.transpose(A, 0, 1) @ grads
Ss = S @ grads
players_w[0] = players_w[0] - grads[0] * lr - Ate[0] * args.lamb * lr
players_w[1] = players_w[1] - grads[1] * lr - Ate[1] * args.lamb * lr
if args.type == 'conopt':
players_w[0] = players_w[0] - Ss[0] * args.lamb * lr
players_w[1] = players_w[1] - Ss[1] * args.lamb * lr
if args.print_update:
print("Grad norm:", torch.mean(torch.norm(torch.stack(grads_[-10:]))))
if torch.mean(torch.norm(torch.stack(grads_[-10:]))) < 0.001:
break
else:
count += 1
acount += count
counts.append(acount / len(initial_w))
if args.visualize:
ws = tree.map_structure(lambda x: float(x), ws)
# print(ws)
ws = np.array(ws)
losses = tree.map_structure(lambda x: x.detach().cpu().numpy(), losses)
plt.figure(figsize=(4, 4))
for i in range(len(ws) - 1):
plt.arrow(ws[i, 0], ws[i, 1], ws[i + 1, 0] - ws[i, 0], ws[i+1, 1]-ws[i, 1], shape='full', color='b', lw=1, head_length=0.01, head_width=0.01)
plt.title(f"{args.type},S={len(ws)}")
plt.savefig(f"{args.type}_batch/{idx}_{args.type}_path.png", bbox_inches="tight")
plt.close()
plt.figure(figsize=(4, 4))
plt.plot(list(range(len(losses) // 2)), losses[::2])
plt.plot(list(range(len(losses) // 2)), losses[1::2])
plt.savefig(f"{args.type}_batch/{idx}_{args.type}_loss.png", bbox_inches="tight")
plt.close()
updates = torch.stack(updates).cpu().detach().numpy()
grads_ = torch.stack(grads_).cpu().detach().numpy()
Ags = torch.stack(Ags).cpu().detach().numpy()
Sgs = torch.stack(Sgs).cpu().detach().numpy()
plt.figure(figsize=(4, 4))
plt.plot(range(updates.shape[0]), updates[:, 0,0], label='P1')
plt.plot(range(updates.shape[0]), updates[:, 1,0], label='P2')
plt.legend()
plt.savefig(f"{args.type}_batch/{idx}_{args.type}_update_grad.png", bbox_inches="tight")
plt.close()
plt.figure(figsize=(4, 4))
plt.plot(range(updates.shape[0]), updates[:, 0, 1], label='P1')
plt.plot(range(updates.shape[0]), updates[:, 1, 1], label='P2')
plt.legend()
plt.savefig(f"{args.type}_batch/{idx}_{args.type}_update_A.png", bbox_inches="tight")
plt.close()
plt.figure(figsize=(4, 4))
plt.plot(range(updates.shape[0]), updates[:, 0, 2], label='P1')
plt.plot(range(updates.shape[0]), updates[:, 1, 2], label='P2')
plt.legend()
plt.savefig(f"{args.type}_batch/{idx}_{args.type}_update_S.png", bbox_inches="tight")
plt.close()
plt.figure(figsize=(4, 4))
plt.plot(range(grads_.shape[0]), grads_[:, 0], label='P1')
plt.plot(range(grads_.shape[0]), grads_[:, 1], label='P2')
plt.legend()
plt.savefig(f"{args.type}_batch/{idx}_{args.type}_grads.png", bbox_inches="tight")
plt.close()
plt.figure(figsize=(4, 4))
plt.plot(range(Ags.shape[0]), Ags[:, 0], label='P1')
plt.plot(range(Ags.shape[0]), Ags[:, 1], label='P2')
plt.legend()
plt.savefig(f"{args.type}_batch/{idx}_{args.type}_ags.png", bbox_inches="tight")
plt.close()
plt.figure(figsize=(4, 4))
plt.plot(range(Sgs.shape[0]), Sgs[:, 0], label='P1')
plt.plot(range(Sgs.shape[0]), Sgs[:, 1], label='P2')
plt.legend()
plt.savefig(f"{args.type}_batch/{idx}_{args.type}_sgs.png", bbox_inches="tight")
plt.close()
plt.figure(figsize=(4, 4))
plt.plot(range(Sgs.shape[0]), np.sqrt(grads_[:, 0] ** 2 + grads_[:, 1] ** 2).reshape(-1))
plt.savefig(f"{args.type}_batch/{idx}_{args.type}_grads_norm.png", bbox_inches="tight")
plt.close()
print('\t'.join(list(map(str, counts))))
if __name__ == "__main__":
main()