-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_self_play.py
More file actions
206 lines (173 loc) · 6.67 KB
/
Copy pathtrain_self_play.py
File metadata and controls
206 lines (173 loc) · 6.67 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import os
os.environ["MKL_NUM_THREADS"] = "1"
os.environ["NUMEXPR_NUM_THREADS"] = "1"
os.environ["OMP_NUM_THREADS"] = "1"
import glob
import os
import time
from functools import lru_cache
import numpy as np
import torch
import torch.optim as optim
from tqdm import tqdm
from utils.board import *
from utils.mcts import *
class Dataset:
def __init__(self, batch_size=64):
self.files = glob.glob("/d/dlgo/tree*")
print("num data", len(self.files))
self.batch_size = batch_size
@lru_cache(maxsize=100000)
def get_datax(self, filename):
data_path = filename.replace("tree", "data")
if os.path.exists(data_path):
policy, value, features = torch.load(data_path)
else:
board = filename.replace("/tree", "/game")
board = torch.load(board)
tree = torch.load(filename)
policy, value, features = self.get_data(board, tree)
chunk = Chunk()
chunk.inputs = features
chunk.policy = policy
chunk.value = value
chunk.value = 0 # doesn't matter
return chunk
def __getitem__(self, idx):
if idx % 1000 == 0:
self.files = glob.glob("/d/dlgo/tree*")
print("num data", len(self.files))
return self.get_batch()
def get_batch(self):
idxs = np.random.choice(len(self.files), self.batch_size)
datas = []
for idx in idxs:
try:
filename = self.files[idx]
datas.append(self.get_datax(filename))
except Exception as e:
pass
inputs_batch = []
policy_batch = []
value_batch = []
for chunk_ in datas:
import copy
for sym_idx in range(8):
chunk = copy.deepcopy(chunk_)
chunk.do_symmetry(sym_idx)
inputs_batch.append(chunk.inputs)
policy_batch.append(chunk.policy)
value_batch.append([chunk.value])
return (
torch.tensor(inputs_batch).float(),
torch.tensor(policy_batch).float(),
torch.tensor(value_batch).float(),
)
def get_data(self, board: Board, root_node: Node, explore_th=500):
policy = np.zeros(81 + 1) # additional for "PASS"
N_visits = root_node.visits
value = root_node.values / root_node.visits
more_to_explore = []
for vtx, node in root_node.children.items():
if node.visits > explore_th:
more_to_explore.append(node)
if vtx == -1:
policy[-1] = node.visits / N_visits # pass
else:
idx = board.vertex_to_index(vtx)
policy[idx] = node.visits / N_visits
features = board.get_features()
return policy, value, features
def __len__(self):
return len(self.files) * 4
def symmetry_board(symm, plane):
use_flip = False
if symm // 4 != 0:
use_flip = True
symm = symm % 4
transformed = np.rot90(plane, symm)
if use_flip:
transformed = np.flip(transformed, 1)
return transformed
class Chunk:
def __init__(self):
self.inputs = None
self.policy = None
self.value = None
def __str__(self):
out = str()
out += "policy: {p} | value: {v}\n".format(p=self.policy, v=self.value)
return out
def do_symmetry(self, symm=None):
assert self.policy is not None, ""
if symm == None:
symm = int(np.random.choice(8, 1)[0])
for i in range(INPUT_CHANNELS - 2): # last 2 channels is side to move.
p = self.inputs[i]
self.inputs[i][:][:] = symmetry_board(symm, p)[:][:]
buf = self.policy[:NUM_INTESECTIONS].copy()
buf = symmetry_board(symm, np.reshape(buf, (BOARD_SIZE, BOARD_SIZE)))
self.policy[:NUM_INTESECTIONS] = buf.reshape(-1)
def cross_entropy(outputs, targets):
"""Calculate crossentropy between two distribution """
# import ipdb; ipdb.set_trace()
outputs = torch.nn.functional.log_softmax(outputs, dim=1) # normalize input
acc = (targets.max(1)[1] == outputs.max(1)[1]).sum().item() / (targets.shape[0])
return -torch.sum(targets * outputs) / targets.size()[0], acc
class TrainingPipe:
def __init__(self):
self.network = Network(BOARD_SIZE)
self.network.trainable()
# Prepare the data set from sgf files.
self.data_set = Dataset()
self.dataloader = torch.utils.data.DataLoader(
self.data_set,
batch_size=1,
shuffle=False,
collate_fn=lambda x: x[0],
num_workers=16,
)
def running(self, steps=1000000, verbose_step=1000, learning_rate=1e-5):
optimizer = optim.Adam(
self.network.parameters(), lr=learning_rate, weight_decay=1e-4
)
policy_running_loss = 0
accuracy = 0
start_time = time.time()
for step, data in tqdm(enumerate(self.dataloader)):
for _ in range(2):
inputs, target_p, _ = data # self.data_set.get_batch(batch_size)
inputs = inputs.cuda()
target_p = target_p.cuda()
policy_output, _ = self.network(inputs)
p_loss, acc = cross_entropy(policy_output, target_p)
loss = p_loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
policy_running_loss += p_loss.item()
accuracy += acc
if (step + 1) % verbose_step == 0:
elapsed = time.time() - start_time
rate = verbose_step / elapsed
remaining_step = steps - step
estimate_remaining_time = int(remaining_step / rate)
print(
f"{time.strftime('%H:%M:%S')} steps: {step + 1}/{steps}, {100 * ((step + 1) / steps):.2f}% -> policy loss: {policy_running_loss / verbose_step:.4f}, accuracy: {accuracy / verbose_step:.4f} | rate: {rate:.2f}(step/sec), estimate: {estimate_remaining_time}(sec)"
)
policy_running_loss = 0
accuracy = 0
start_time = time.time()
if (step + 1) % 1000 == 0:
self.save_weights("selfplay_w_" + str(step))
if (step + 1) % 2500 == 0:
for g in optimizer.param_groups:
g["lr"] *= 0.1
def save_weights(self, name):
self.network.save_ckpt(name)
def load_weights(self, name):
if name != None:
self.network.load_ckpt(name)
pipe = TrainingPipe()
pipe.load_weights("./behaviour_clone_model_weights") # best behaviour cloning model
pipe.running(12)