-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural_nets.py
144 lines (97 loc) · 4.17 KB
/
neural_nets.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from tqdm.notebook import trange
import numpy as np
import pickle
class ValueNet(torch.nn.Module):
def __init__(self):
super(ValueNet, self).__init__()
self.l1 = nn.Linear(9,64)
self.l2 = nn.Linear(64,64)
self.l3 = nn.Linear(64,1)
def forward(self, x):
x = self.l1(x)
x = F.relu(x)
x = self.l2(x)
x = F.relu(x)
x = self.l3(x)
return torch.tanh(x)
def predict(self, state):
state = torch.FloatTensor(state)
self.eval()
with torch.no_grad():
p = self.forward(state)
return p.cpu().numpy()
class PolicyNet(torch.nn.Module):
def __init__(self):
super(PolicyNet, self).__init__()
self.l1 = nn.Linear(9,32)
self.l2 = nn.Linear(32,9)
self.sm = nn.Softmax(dim=1)
def forward(self, x):
x = self.l1(x)
x = F.relu(x)
x = self.l2(x)
x = self.sm(x)
return x
def predict(self, state):
self.eval()
state = state.view(1, len(state))
with torch.no_grad():
p = self.forward(state)
return p.cpu().numpy()
class Training():
def __init__(self):
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Device=", self.device)
def train_policy(self, state_log, mcts_log, win_log, version, parameter_path, lr=0.02, batchsize=32, epochs=10):
model = PolicyNet().to(self.device)
if version > 0: # load parameters from previous versions
model.load_state_dict(torch.load(parameter_path + "pnet_v{}".format(version-1)))
loss_function = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=lr, momentum=0, weight_decay=1e-4)
losses = []
for i in trange( int(len(win_log)*epochs / batchsize) ):
idx = np.random.randint(0, high=len(win_log), size=batchsize)
x = torch.tensor(state_log[idx,:]).float().requires_grad_()
y = torch.tensor(mcts_log[idx,:]).float().requires_grad_()
x, y = x.to(self.device), y.to(self.device)
optimizer.zero_grad()
output = model(x)
loss = self.CrossEntropy(output, y).mean()
losses.append(loss.item())
loss.backward()
optimizer.step()
self.save_param_loss(version, parameter_path, model, losses, "pnet")
return model, losses
def CrossEntropy(self, output, y):
return -(y * torch.log(output)).sum(dim=0)
def train_value(self, state_log, mcts_log, win_log, version, parameter_path, lr=0.02, batchsize=32, epochs=10):
model = ValueNet().to(self.device)
if version > 0: # load parameters from previous versions
model.load_state_dict(torch.load(parameter_path + "vnet_v{}".format(version-1)))
loss_function = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=lr, momentum=0, weight_decay=1e-4)
losses = []
for i in trange( int((len(win_log) / batchsize)*epochs) ):
idx = np.random.randint(0, high=len(win_log), size=batchsize)
x = torch.tensor(state_log[idx,:]).float().requires_grad_()
y = torch.tensor(win_log[idx]).float().requires_grad_()
x, y = x.to(self.device), y.to(self.device)
optimizer.zero_grad()
output = model(x)
loss = loss_function(output, y)
losses.append(loss.item())
loss.backward()
optimizer.step()
self.save_param_loss(version, parameter_path, model, losses, "vnet")
return model, losses
def save_param_loss(self, version, parameter_path, model, losses, net_prefix):
# Save losses
loss_fn = parameter_path + net_prefix + "_loss_v{}".format(version) + ".data"
with open(loss_fn, "wb") as f:
pickle.dump(losses, f)
# Save parameters
p_fn = parameter_path + net_prefix + "_v{}".format(version)
torch.save(model.state_dict(), p_fn)