-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleps_on.py
229 lines (179 loc) · 6.24 KB
/
cleps_on.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import copy
from torch_geometric.data import Data
from torch_geometric import nn
from torch.nn.functional import one_hot
from torch_geometric.data import Dataset
from torch_geometric.transforms import BaseTransform
from torch_geometric.loader import DataLoader
from torch_geometric.nn import global_mean_pool
import numpy as np
import matplotlib.pyplot as plt
import torch
import torchvision
import pandas as pd
import chess
import chess.pgn as PGN
import io
from random import shuffle
import json
import os
import os.path as osp
import fileinput
from math import tanh
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("Using gpu: " + str(torch.cuda.is_available()))
def line_to_graph(line):
board = chess.Board(line["fen"])
x = torch.zeros(64, 26)
for k, v in board.piece_map().items():
x[k, :7] = v.piece_type * one_hot(torch.tensor(v.piece_type), 7)
x[k, 7:9] = one_hot(torch.tensor(v.color).long(), 2)
x[:, 9:11] = one_hot(torch.tensor(board.turn).long(), 2)
x[:, 11:13] = one_hot(torch.tensor(board.has_castling_rights(0)).long(), 2)
x[:, 13:15] = one_hot(torch.tensor(board.has_castling_rights(0)).long(), 2)
x[:, 15:17] = one_hot(torch.tensor(board.has_castling_rights(1)).long(), 2)
x[:, 17:19] = one_hot(torch.tensor(board.has_castling_rights(1)).long(), 2)
# x[:, 19:21] = one_hot(torch.tensor(board.is_repetition(2)).long(), 2)
# x[:, 21:23] = one_hot(torch.tensor(board.is_repetition(3)).long(), 2)
# x[:, 23] = board.fullmove_number
# x[:, 24] = board.halfmove_clock
x[:, 25] = -1 if board.ep_square is None else board.ep_square % 8
edge_list = torch.tensor(list(
map(lambda move: [move.from_square, move.to_square], board.legal_moves))).long()
# print(line['evals'])
cp = line['evals']['cp']
"""
if -200 <= cp <= -50:
y = torch.tensor(0)
elif -50 < cp <= -25:
y = torch.tensor(1)
elif -25 < cp <= -10:
y = torch.tensor(2)
elif -10 < cp <= -5:
y = torch.tensor(3)
elif -5 < cp <= 0:
y = torch.tensor(4)
elif 0 < cp <= 5:
y = torch.tensor(5)
elif 5 < cp <= 10:
y = torch.tensor(6)
elif 10 < cp <= 25:
y = torch.tensor(7)
elif 25 < cp <= 50:
y = torch.tensor(8)
elif 50 < cp <= 200:
y = torch.tensor(9)
else:
print("Error, cp is: "+str(cp))
assert False
"""
y = torch.tensor(cp >= 0).long()
y = one_hot(y, num_classes=2).float()
return Data(x=x, edge_index=edge_list, y=y)
# 10 epoch | 100_000 20min
#
#
# 90/10 Train/Test
# 1 Job avec 1_000_000
# RAM 20Go - 32Go
# Disk: 20Go
# Conda
class Model(torch.nn.Module):
def __init__(self, heads_nb=1, num_nodes=64, in_channels=26, hidden_channels=64, out_channels=10):
super().__init__()
self.heads_nb = heads_nb
self.in_channels = in_channels
self.hidden_channels = hidden_channels
self.out_channels = out_channels
self.lin0 = nn.Linear(in_channels, hidden_channels)
self.gat = nn.GATv2Conv(
in_channels=hidden_channels, out_channels=hidden_channels, heads=heads_nb)
self.lin1 = nn.Linear(num_nodes * hidden_channels *
heads_nb, num_nodes * hidden_channels)
self.lin2 = nn.Linear(num_nodes * hidden_channels, hidden_channels)
self.lin3 = nn.Linear(hidden_channels, out_channels)
def forward(self, g):
y = self.lin0(g.x)
y = y.relu()
y = self.gat(x=y, edge_index=g.edge_index.T, edge_attr=g.edge_attr)
y = y.reshape(-1)
y = self.lin1(y)
y = y.relu()
y = y.reshape(-1)
y = self.lin2(y)
y = y.relu()
y = y.reshape(-1)
y = self.lin3(y)
y = y.reshape(-1)
return y
def load(line):
kept = json.loads(line)
to_save = {'fen': kept['fen'], 'evals': kept['evals'][0]['pvs'][0]}
if not 'cp' in to_save['evals']: # means the position is evaluated as a mate
if to_save['evals']['mate'] > 0:
to_save['evals']['cp'] = 20_000
else:
to_save['evals']['cp'] = -20_000
to_save['evals']['cp'] = to_save['evals']['cp'] / 100
return to_save
heads_nb = 16
model = Model(heads_nb=heads_nb, out_channels=2)
print("Model created")
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
model.to(device)
model.train()
data_size = 1e7
batch_size = 1024
num_epochs = 2
losses = []
accs = []
for epoch in range(num_epochs):
print("Epoch: " + str(epoch))
epoch_loss = 0.
epoch_acc = 0.
num_ones = 0
num_zeros = 0
with open('lichess_db_eval.jsonl') as f:
batch_loss = 0.
batch_acc = 0.
for i, line in enumerate(f):
line = load(line)
g = line_to_graph(line).to(device)
output = model(g)
loss = criterion(output, g.y)
if torch.argmax(g.y):
num_ones += 1
else:
num_zeros += 1
batch_loss += loss / batch_size
batch_acc += (torch.argmax(output) == torch.argmax(g.y)
).detach().item() / batch_size
epoch_loss += loss.detach().item() / data_size
epoch_acc += (torch.argmax(output) == torch.argmax(g.y)
).detach().item() / data_size
if i % batch_size == 0 and i != 0:
optimizer.zero_grad()
batch_loss.backward()
optimizer.step()
batch_loss = batch_loss.detach().item()
print("Currently at i=" + str(i))
print("\t Batch loss is: " + str(batch_loss))
print("\t Batch acc is: " + str(batch_acc))
batch_loss = 0.
batch_acc = 0.
if i > data_size:
break
print("Epoch loss is: " + str(epoch_loss))
print("Epoch acc is: " + str(epoch_acc))
print("Num zeros: " + str(num_zeros))
print("Num ones: " + str(num_ones))
losses.append(epoch_loss)
accs.append(epoch_acc)
torch.save(model.state_dict(), 'model.pth')
fig, ax = plt.subplots(2)
ax[0].plot(losses)
ax[0].set_title('Losses')
ax[1].plot(accs)
ax[1].set_title('Accuracies')
plt.savefig('losses.png')