-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from eboatwright/dev
v3.1 (Patch 3)
- Loading branch information
Showing
17 changed files
with
819 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
GAMES_PER_MATCH = 1_500 | ||
EPOCHS_PER_TRAIN = 2 # ? | ||
MINIBATCH_SIZE = 10_000 | ||
LEARNING_RATE = 0.0009 | ||
|
||
DEPTH_PER_MOVE = 10 | ||
PERC_CHANCE_FOR_RANDOM_MOVE = 2 | ||
CONCURRENT_GAMES = 4 | ||
MAX_MOVES = 120 | ||
|
||
INPUT_NODES = 768 | ||
HIDDEN_NODES = 64 # 256? | ||
OUTPUT_NODES = 1 | ||
|
||
BUCKETS = 1 # 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# The developer of the Weiawaga engine's NNUE trainer: Mimir, was very very helpful in making this! | ||
# https://github.com/Heiaha/Mimir/ | ||
|
||
import random | ||
|
||
import chess | ||
import chess.engine | ||
import chess.polyglot | ||
import asyncio | ||
|
||
import config | ||
from neural_network import NeuralNetwork | ||
|
||
|
||
class TrainingResults: | ||
def __init__(self): | ||
self.games = 0 | ||
self.positions = 0 | ||
|
||
|
||
class DataPoint: | ||
def __init__(self, fen, outcome): | ||
self.fen = fen | ||
self.outcome = outcome | ||
|
||
|
||
training_results = TrainingResults() | ||
nn = NeuralNetwork() | ||
data_points = [] | ||
|
||
|
||
async def play_game(): | ||
transport, maxwell_engine = await chess.engine.popen_uci(["./../target/release/maxwell", "debug_output=false"]) | ||
board = chess.Board() | ||
|
||
with chess.polyglot.open_reader("Perfect2021.bin") as reader: | ||
number_of_book_moves = random.randint(1, 10) | ||
|
||
for i in range(number_of_book_moves): | ||
board.push(reader.choice(board).move) | ||
|
||
fen_strings = [] | ||
|
||
while not board.is_game_over(claim_draw=True): | ||
if random.randint(0, 100) < config.PERC_CHANCE_FOR_RANDOM_MOVE: | ||
board.push(random.choice(list(board.legal_moves))) | ||
else: | ||
result = await maxwell_engine.play(board, chess.engine.Limit(depth=config.DEPTH_PER_MOVE)) | ||
board.push(result.move) | ||
|
||
fen_strings.append(board.fen()) | ||
|
||
if board.fullmove_number >= config.MAX_MOVES: | ||
break | ||
|
||
game_outcome = 0.0 | ||
|
||
# For some reason when it detects a threefold-repetition or a 50 move draw, it returns None instead of a draw :P | ||
if outcome := board.outcome(): | ||
if outcome.winner == chess.WHITE: | ||
game_outcome = 1.0 | ||
elif outcome.winner == chess.BLACK: | ||
game_outcome = -1.0 | ||
|
||
for fen in fen_strings: | ||
data_points.append(DataPoint(fen, game_outcome)) | ||
|
||
await maxwell_engine.quit() | ||
|
||
|
||
async def play_games(): | ||
games_completed = 0 | ||
|
||
pending = {asyncio.create_task(play_game()) for _ in range(config.CONCURRENT_GAMES)} | ||
|
||
while len(pending) > 0: | ||
print(f"Playing self-play games... {games_completed}/{config.GAMES_PER_MATCH}", end="\r", flush=True) | ||
|
||
completed, pending = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED) | ||
|
||
for completed_task in completed: | ||
games_completed += 1 | ||
|
||
if games_completed + len(pending) < config.GAMES_PER_MATCH: | ||
pending.add(asyncio.create_task(play_game())) | ||
|
||
|
||
if __name__ == "__main__": | ||
print("### MAXWELL NNUE TRAINER ###\n") | ||
|
||
training_cycle = 0 | ||
# nn.save_weights() # Save the initial randomized weights so that the program has the same weights as the trainer | ||
|
||
while True: | ||
data_points = [] | ||
|
||
print(f"Training cycle {training_cycle + 1}:") | ||
|
||
asyncio.run(play_games()) | ||
|
||
training_results.games += config.GAMES_PER_MATCH | ||
training_results.positions += len(data_points) | ||
|
||
print("\nSelf-play done!\n") | ||
print("Training network...") | ||
|
||
for epoch in range(config.EPOCHS_PER_TRAIN): | ||
print(f"Epoch {epoch + 1}...") | ||
|
||
random.shuffle(data_points) | ||
|
||
data_point_index = 0 | ||
|
||
while data_point_index < len(data_points): | ||
next_index = min(data_point_index + config.MINIBATCH_SIZE, len(data_points)) | ||
|
||
nn.back_prop(data_point_index, next_index, data_points[data_point_index:next_index]) | ||
|
||
data_point_index = next_index | ||
|
||
print("Done training!") | ||
# print("Calculating total error...") | ||
# print(f"Total error on data set: {nn.get_total_error(data_points)}") | ||
# print("Done!\n") | ||
|
||
print(f"Total games played: {training_results.games}") | ||
print(f"Total positions trained on: {training_results.positions}\n") | ||
|
||
training_cycle += 1 | ||
nn.save_weights() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import random | ||
|
||
class Matrix: | ||
def __init__(self, rows, cols): | ||
self.rows = rows | ||
self.cols = cols | ||
|
||
self.data = [] | ||
for row in range(rows): | ||
new_row = [] | ||
for col in range(cols): | ||
new_row.append(0.0) | ||
self.data.append(new_row) | ||
|
||
def from_2d_list(input_2d_list): | ||
result = Matrix(len(input_2d_list), len(input_2d_list[0])) | ||
result.data = input_2d_list | ||
return result | ||
|
||
def fill_zeros(self): | ||
for row in range(self.rows): | ||
for col in range(self.cols): | ||
self.data[row][col] = 0.0 | ||
|
||
def flatten(self): | ||
result = [] | ||
|
||
for row in range(self.rows): | ||
for col in range(self.cols): | ||
result.append(self.data[row][col]) | ||
|
||
return result | ||
|
||
def transpose(a): | ||
result = Matrix(a.cols, a.rows) | ||
|
||
for row in range(a.rows): | ||
for col in range(a.cols): | ||
result.data[col][row] = a.data[row][col] | ||
|
||
return result | ||
|
||
def random(rows, cols): | ||
result = Matrix(rows, cols) | ||
|
||
for row in range(rows): | ||
for col in range(cols): | ||
result.data[row][col] = random.uniform(-0.8, 0.8) | ||
|
||
return result | ||
|
||
def add(a, b): | ||
result = Matrix(a.rows, a.cols) | ||
|
||
for row in range(a.rows): | ||
for col in range(a.cols): | ||
result.data[row][col] = a.data[row][col] + b.data[row][col] | ||
|
||
return result | ||
|
||
def subtract(a, b): | ||
result = Matrix(a.rows, a.cols) | ||
|
||
for row in range(a.rows): | ||
for col in range(a.cols): | ||
result.data[row][col] = a.data[row][col] - b.data[row][col] | ||
|
||
return result | ||
|
||
def multiply(a, b): | ||
result = Matrix(a.rows, a.cols) | ||
|
||
for row in range(a.rows): | ||
for col in range(a.cols): | ||
result.data[row][col] = a.data[row][col] * b.data[row][col] | ||
|
||
return result | ||
|
||
def divide(a, b): | ||
result = Matrix(a.rows, a.cols) | ||
|
||
for row in range(a.rows): | ||
for col in range(a.cols): | ||
result.data[row][col] = a.data[row][col] / b.data[row][col] | ||
|
||
return result | ||
|
||
def divide_by_num(mat, num): | ||
result = Matrix(mat.rows, mat.cols) | ||
|
||
for row in range(mat.rows): | ||
for col in range(mat.cols): | ||
result.data[row][col] = mat.data[row][col] / num | ||
|
||
return result | ||
|
||
def dot(a, b): | ||
result = Matrix(a.rows, b.cols) | ||
|
||
for row in range(result.rows): | ||
for col in range(result.cols): | ||
sum_of_column = 0 | ||
|
||
for offset in range(a.cols): | ||
sum_of_column += a.data[row][offset] * b.data[offset][col] | ||
|
||
result.data[row][col] = sum_of_column | ||
|
||
return result | ||
|
||
def scale(m, s): | ||
result = Matrix(m.rows, m.cols) | ||
|
||
for row in range(result.rows): | ||
for col in range(result.cols): | ||
result.data[row][col] = m.data[row][col] * s | ||
|
||
return result | ||
|
||
def pow(m, e): | ||
result = Matrix(m.rows, m.cols) | ||
|
||
for row in range(result.rows): | ||
for col in range(result.cols): | ||
result.data[row][col] = m.data[row][col] ** e | ||
|
||
return result | ||
|
||
def map(m, fn): | ||
for row in range(m.rows): | ||
for col in range(m.cols): | ||
m.data[row][col] = fn(m.data[row][col]) | ||
return m |
Oops, something went wrong.