forked from bhansconnect/fast-alphazero-general
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNNetWrapper.py
More file actions
155 lines (130 loc) · 5.24 KB
/
Copy pathNNetWrapper.py
File metadata and controls
155 lines (130 loc) · 5.24 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
from NNetArchitecture import NNetArchitecture as nnetarch
import torch.optim as optim
import torch
from time import time
from pytorch_classification.utils import Bar, AverageMeter
from utils import *
import os
import numpy as np
import math
import sys
sys.path.append('../../')
args = dotdict({
'lr': 0.001,
'cuda': torch.cuda.is_available(),
'num_channels': 128,
'depth': 5,
})
class NNetWrapper():
def __init__(self, game):
self.nnet = nnetarch(game, args)
self.board_x, self.board_y = game.getBoardSize()
self.action_size = game.getActionSize()
self.optimizer = optim.SGD(
self.nnet.parameters(), lr=args.lr, momentum=0.9, weight_decay=1e-3)
# self.scheduler = optim.lr_scheduler.MultiStepLR(
# self.optimizer, milestones=[200,400], gamma=0.1)
self.scheduler = optim.lr_scheduler.ReduceLROnPlateau(
self.optimizer, cooldown=10)
if args.cuda:
self.nnet.cuda()
def train(self, batches, train_steps):
self.nnet.train()
data_time = AverageMeter()
batch_time = AverageMeter()
pi_losses = AverageMeter()
v_losses = AverageMeter()
end = time()
#print(f'Current LR: {self.scheduler.get_lr()[0]}')
bar = Bar(f'Training Net', max=train_steps)
current_step = 0
while current_step < train_steps:
for batch_idx, batch in enumerate(batches):
if current_step == train_steps:
break
current_step += 1
boards, target_pis, target_vs = batch
# predict
if args.cuda:
boards, target_pis, target_vs = boards.contiguous().cuda(
), target_pis.contiguous().cuda(), target_vs.contiguous().cuda()
# measure data loading time
data_time.update(time() - end)
# compute output
out_pi, out_v = self.nnet(boards)
l_pi = self.loss_pi(target_pis, out_pi)
l_v = self.loss_v(target_vs, out_v)
total_loss = l_pi + l_v
# record loss
pi_losses.update(l_pi.item(), boards.size(0))
v_losses.update(l_v.item(), boards.size(0))
# compute gradient and do SGD step
self.optimizer.zero_grad()
total_loss.backward()
self.optimizer.step()
# measure elapsed time
batch_time.update(time() - end)
end = time()
# plot progress
bar.suffix = '({step}/{size}) Data: {data:.3f}s | Batch: {bt:.3f}s | Total: {total:} | ETA: {eta:} | Loss_pi: {lpi:.4f} | Loss_v: {lv:.3f}'.format(
step=current_step,
size=train_steps,
data=data_time.avg,
bt=batch_time.avg,
total=bar.elapsed_td,
eta=bar.eta_td,
lpi=pi_losses.avg,
lv=v_losses.avg,
)
bar.next()
self.scheduler.step(pi_losses.avg+v_losses.avg)
bar.finish()
print()
return pi_losses.avg, v_losses.avg
def predict(self, board):
"""
board: np array with board
"""
# timing
# start = time.time()
# preparing input
board = torch.FloatTensor(board.astype(np.float64))
if args.cuda:
board = board.contiguous().cuda()
with torch.no_grad():
board = board.view(1, self.board_x, self.board_y)
self.nnet.eval()
pi, v = self.nnet(board)
# print('PREDICTION TIME TAKEN : {0:03f}'.format(time.time()-start))
return torch.exp(pi).data.cpu().numpy()[0], v.data.cpu().numpy()[0]
def process(self, batch):
if args.cuda:
batch = batch.cuda()
self.nnet.eval()
with torch.no_grad():
pi, v = self.nnet(batch)
return torch.exp(pi), v
def loss_pi(self, targets, outputs):
return -torch.sum(targets * outputs) / targets.size()[0]
def loss_v(self, targets, outputs):
return torch.sum((targets - outputs) ** 2) / targets.size()[0]
def save_checkpoint(self, folder='checkpoint', filename='checkpoint.pth.tar'):
filepath = os.path.join(folder, filename)
if not os.path.exists(folder):
os.mkdir(folder)
torch.save({
'state_dict': self.nnet.state_dict(),
'opt_state': self.optimizer.state_dict(),
'sch_state': self.scheduler.state_dict()
}, filepath)
def load_checkpoint(self, folder='checkpoint', filename='checkpoint.pth.tar'):
# https://github.com/pytorch/examples/blob/master/imagenet/main.py#L98
filepath = os.path.join(folder, filename)
if not os.path.exists(filepath):
raise ("No model in path {}".format(filepath))
checkpoint = torch.load(filepath)
self.nnet.load_state_dict(checkpoint['state_dict'])
if 'opt_state' in checkpoint:
self.optimizer.load_state_dict(checkpoint['opt_state'])
if 'sch_state' in checkpoint:
self.scheduler.load_state_dict(checkpoint['sch_state'])