-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
145 lines (139 loc) · 4.67 KB
/
train.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
from LITSclass import *
from LITSnode import LITSnode
import torch
from torch.nn import Linear, ReLU
import torch.optim as optim
def train(its, opt, model, lfunc, example, cut):
# Trains model to approximate the smallest possible value of example after one move on a given board
# If example is an opponent objective function, model eventually approximates the current player's objective function
test = []
testres = []
for i in range(50):
a = LITSboard()
while len(a.moves) > 0:
test.append(a.vector())
pos = [a.vector(guy) for guy in a.moves]
vals = example(torch.tensor(pos))
sml = float('inf')
move = None
for j in range(len(pos)):
if vals[j].item() < sml:
sml = vals[j].item()
move = a.moves[j]
testres.append([-sml])
a.play(move)
for j in range(cut):
test.pop()
testres.pop()
test = torch.tensor(test)
testres = torch.tensor(testres)
for i in range(its):
if i % 500 == 0:
print(i, lfunc(model(test), testres), round(time.time()))
real = []
true = []
for j in range(8):
a = LITSboard()
while len(a.moves) > 0:
real.append(a.vector())
pos = [a.vector(guy) for guy in a.moves]
vals = example(torch.tensor(pos))
sml = float('inf')
move = None
for j in range(len(pos)):
if vals[j].item() < sml:
sml = vals[j].item()
move = a.moves[j]
true.append([-sml])
a.play(move)
for k in range(cut):
real.pop()
true.pop()
opt.zero_grad()
pred = model(torch.tensor(real))
loss = lfunc(pred,torch.tensor(true))
loss.backward()
opt.step()
print(i, lfunc(model(test), testres), round(time.time()))
def deepen(old_name, new_name, layers, its, depth):
# Creates model to approximate the value of the position after depth optimal moves are played
# requires model which approximates value of position after depth-1 optimal moves are played.
old_model = torch.load(old_name + '.pt')
new_model = torch.nn.Sequential(*layers)
lfunc = torch.nn.MSELoss()
optimizer = optim.Adam(new_model.parameters(), lr = .002)
train(its, optimizer, new_model, lfunc, old_model, depth - 1)
torch.save(new_model, new_name + '.pt')
'''
Example usage:
layers = [Linear(200,200),ReLU(),Linear(200,200),ReLU(),Linear(200,200),ReLU(),Linear(200,200),ReLU(),Linear(200,200),ReLU(),Linear(200,200),ReLU(),Linear(200,1)]
deepen('model5', 'model6', layers, 10000, 6)
'''
# Code for training the first model (not based on an example model, but rather on the true value of the position):
#test=[]
#testres=[]
#for i in range(50):
# a=LITSboard()
# while len(a.moves)>0:
# test.append(a.vector())
# d=a.diff()
# val=2*(len(a.played)%2)-1
# best=-float("inf")
# move=None
# for guy in a.moves:
# change=0
# for place in guy.locs:
# if place in a.X:
# change+=val
# if place in a.O:
# change-=val
# if change>best:
# best=change
# move=guy
# a.play(move)
# testres.append([float(d+best)])
#test=torch.tensor(test)
#testres=torch.tensor(testres)
#
#xs=[]
#losses=[]
#
#for i in range(50000):
# if i%10==0 and i>1990:
# xs.append(i)
# losses.append(lfunc(model(test),testres))
# if i%5000==0:
# plt.plot(xs,losses)
# plt.show()
# if i%500==0:
# print(i)
# print(lfunc(model(test),testres))
# real=[]
# true=[]
# for j in range(8):
# a=LITSboard()
# while len(a.moves)>0:
# real.append(a.vector())
# d=a.diff()
# val=2*(len(a.played)%2)-1
# best=-float("inf")
# move=None
# for guy in a.moves:
# change=0
# for place in guy.locs:
# if place in a.X:
# change+=val
# if place in a.O:
# change-=val
# if change>best:
# best=change
# move=guy
# a.play(move)
# true.append([float(d+best)])
# optimizer.zero_grad()
# predictions=model(torch.tensor(real))
# loss=lfunc(predictions,torch.tensor(true))
# loss.backward()
# optimizer.step()
#
#torch.save(model,"model1.pt")