File size: 9,091 Bytes
c483ec7 a8711ff c483ec7 a62485e c483ec7 a62485e c483ec7 a62485e c483ec7 a62485e c483ec7 a62485e a8711ff a62485e a8711ff a62485e a8711ff a62485e a8711ff a62485e a8711ff a62485e c483ec7 a62485e c483ec7 |
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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
import torch
import torch.nn as nn
import torch.optim as optim
import chess
import os
import chess.engine as eng
import torch.multiprocessing as mp
import random
from pathlib import Path
# CONFIGURATION
CONFIG = {
"stockfish_path": "/Users/aaronvattay/Downloads/stockfish/stockfish-macos-m1-apple-silicon",
"model_path": "chessy_model.pth",
"backup_model_path": "chessy_modelt-1.pth",
"device": torch.device("mps"),
"learning_rate": 1e-4,
"num_games": 30,
"num_epochs": 10,
"stockfish_time_limit": 1.0,
"search_depth": 1,
"epsilon": 4
}
device = CONFIG["device"]
def board_to_tensor(board):
piece_encoding = {
'P': 1, 'N': 2, 'B': 3, 'R': 4, 'Q': 5, 'K': 6,
'p': 7, 'n': 8, 'b': 9, 'r': 10, 'q': 11, 'k': 12
}
tensor = torch.zeros(64, dtype=torch.long)
for square in chess.SQUARES:
piece = board.piece_at(square)
if piece:
tensor[square] = piece_encoding[piece.symbol()]
else:
tensor[square] = 0
return tensor.unsqueeze(0)
class NN1(nn.Module):
def __init__(self):
super().__init__()
self.embedding = nn.Embedding(13, 64)
self.attention = nn.MultiheadAttention(embed_dim=64, num_heads=16)
self.neu = 512
self.neurons = nn.Sequential(
nn.Linear(4096, self.neu),
nn.ReLU(),
nn.Linear(self.neu, self.neu),
nn.ReLU(),
nn.Linear(self.neu, self.neu),
nn.ReLU(),
nn.Linear(self.neu, self.neu),
nn.ReLU(),
nn.Linear(self.neu, self.neu),
nn.ReLU(),
nn.Linear(self.neu, self.neu),
nn.ReLU(),
nn.Linear(self.neu, self.neu),
nn.ReLU(),
nn.Linear(self.neu, self.neu),
nn.ReLU(),
nn.Linear(self.neu, self.neu),
nn.ReLU(),
nn.Linear(self.neu, self.neu),
nn.ReLU(),
nn.Linear(self.neu, self.neu),
nn.ReLU(),
nn.Linear(self.neu, self.neu),
nn.ReLU(),
nn.Linear(self.neu, self.neu),
nn.ReLU(),
nn.Linear(self.neu, 64),
nn.ReLU(),
nn.Linear(64, 4)
)
def forward(self, x):
x = self.embedding(x)
x = x.permute(1, 0, 2)
attn_output, _ = self.attention(x, x, x)
x = attn_output.permute(1, 0, 2).contiguous()
x = x.view(x.size(0), -1)
x = self.neurons(x)
return x
lass Policy(nn.Module):
def __init__(self):
super().__init__()
self.embedding = nn.Embedding(13, 32)
self.attention = nn.MultiheadAttention(embed_dim=32, num_heads=16)
self.neu = 256
self.neurons = nn.Sequential(
nn.Linear(64*32, self.neu),
nn.ReLU(),
nn.Linear(self.neu, self.neu),
nn.ReLU(),
nn.Linear(self.neu, self.neu),
nn.ReLU(),
nn.Linear(self.neu, self.neu),
nn.ReLU(),
nn.Linear(self.neu, 128),
nn.ReLU(),
nn.Linear(128, 29275),
)
def forward(self, x):
x = chess.Board(x)
color = x.turn
x = board_to_tensor(x)
x = self.embedding(x)
x = x.permute(1, 0, 2)
attn_output, _ = self.attention(x, x, x)
x = attn_output.permute(1, 0, 2).contiguous()
x = x.view(x.size(0), -1)
x = self.neurons(x) * color
return x
model = NN1().to(device)
optimizer = optim.Adam(model.parameters(), lr=CONFIG["learning_rate"])
policy = Policy().to(device)
polweight = torch.load("NeoChess/chessy_policy.pth",map_location=device,weights_only=False)
policy.load_state_dict(polweight)
try:
model.load_state_dict(torch.load(CONFIG["model_path"], map_location=device))
print(f"Loaded model from {CONFIG['model_path']}")
except FileNotFoundError:
try:
model.load_state_dict(torch.load(CONFIG["backup_model_path"], map_location=device))
print(f"Loaded backup model from {CONFIG['backup_model_path']}")
except FileNotFoundError:
print("No model file found, starting from scratch.")
model.train()
criterion = nn.MSELoss()
engine = eng.SimpleEngine.popen_uci(CONFIG["stockfish_path"])
lim = eng.Limit(time=CONFIG["stockfish_time_limit"])
def get_evaluation(board):
"""
Returns the evaluation of the board from the perspective of the current player.
The model's output is from White's perspective.
"""
tensor = board_to_tensor(board).to(device)
with torch.no_grad():
evaluation = model(tensor)[0][0].item()
if board.turn == chess.WHITE:
return evaluation
else:
return -evaluation
with open("/usr/local/python/3.12.1/lib/python3.12/site-packages/torchrl/envs/custom/san_moves.txt", "r") as f:
uci_to_index = {line.strip(): i for i, line in enumerate(f)}
def search(board ,depth ,policy_net=policy, simulations=100, temperature=1.0, device="cpu"):
"""
Monte Carlo search using policy network for move selection
and value network via get_evaluation().
"""
# Convert board FEN to tensor for policy_net
depth
with torch.no_grad():
fen_tensor = torch.tensor([board.fen()], device=device) # Adjust if your FEN encoding differs
logits = policy_net(fen_tensor)["logits"].squeeze(0)
probs = torch.softmax(logits / temperature, dim=-1).cpu().numpy()
move_scores = {move: 0 for move in board.legal_moves}
for move in board.legal_moves:
total_eval = 0
for _ in range(simulations):
board.push(move)
eval_score = get_evaluation(board) # Uses value net
total_eval += eval_score
board.pop()
move_scores[move] = total_eval / simulations
# Weight evaluations by policy prior
for move in move_scores:
move_index = uci_to_index[str(move)]
move_scores[move] *= probs[move_index]
# Pick best move
best_move = max(move_scores, key=move_scores.get)
return best_move, move_scores
def game_gen(engine_side):
data = []
mc = 0
board = chess.Board()
while not board.is_game_over():
is_bot_turn = board.turn != engine_side
if is_bot_turn:
evaling = {}
for move in board.legal_moves:
board.push(move)
evaling[move] = -search(board, depth=CONFIG["search_depth"], alpha=float('-inf'), beta=float('inf'))
board.pop()
if not evaling:
break
keys = list(evaling.keys())
logits = torch.tensor(list(evaling.values())).to(device)
probs = torch.softmax(logits,dim=0)
epsilon = min(CONFIG["epsilon"],len(keys))
bests = torch.multinomial(probs,num_samples=epsilon,replacement=False)
best_idx = bests[torch.argmax(logits[bests])]
move = keys[best_idx.item()]
else:
result = engine.play(board, lim)
move = result.move
if is_bot_turn:
data.append({
'fen': board.fen(),
'move_number': mc,
})
board.push(move)
mc += 1
result = board.result()
c = 0
if result == '1-0':
c = 10.0
elif result == '0-1':
c = -10.0
return data, c, mc
def train(data, c, mc):
for entry in data:
tensor = board_to_tensor(chess.Board(entry['fen'])).to(device)
target = torch.tensor(c * entry['move_number'] / mc, dtype=torch.float32).to(device)
output = model(tensor)[0][0]
loss = criterion(output, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(f"Saving model to {CONFIG['model_path']}")
torch.save(model.state_dict(), CONFIG["model_path"])
return
def main():
for i in range(CONFIG["num_epochs"]):
mp.set_start_method('spawn', force=True)
num_games = CONFIG['num_games']
num_instances = mp.cpu_count()
print(f"Saving backup model to {CONFIG['backup_model_path']}")
torch.save(model.state_dict(), CONFIG["backup_model_path"])
with mp.Pool(processes=num_instances) as pool:
results_self = pool.starmap(game_gen, [(None,) for _ in range(num_games // 3)])
results_white = pool.starmap(game_gen, [(chess.WHITE,) for _ in range(num_games // 3)])
results_black = pool.starmap(game_gen, [(chess.BLACK,) for _ in range(num_games // 3)])
results = []
for s, w, b in zip(results_self, results_white, results_black):
results.extend([s, w, b])
for batch in results:
data, c, mc = batch
print(f"Saving backup model to {CONFIG['backup_model_path']}")
torch.save(model.state_dict(), CONFIG["backup_model_path"])
if data:
train(data, c, mc)
print("Training complete.")
engine.quit()
if __name__ == "__main__":
main() |