import argparse import multiprocessing as mp import random import numpy as np import ray from core.network import ChessNetwork from core.environment import ChessEnvironment from core.agent import ChessAgent from core.mcts import MCTS from core.dzlogging import Logger from core.utils import dotdict logger = Logger.get_logger('evaluation') ray.init() @ray.remote class Runner(object): def __init__(self, net_name, iteration, ckpt=None, cur_white=True): self.net_name = net_name self.iteration = iteration self.ckpt = ckpt self.game_name = f'Game {self.iteration + 1}' self.cur_white = cur_white def run_evaluation(self): current_network = ChessNetwork(self.net_name) try:
import sys import os import unittest sys.path.append(os.path.join(os.path.pardir, os.path.dirname(__file__))) from training.generators import ExampleGenerator from core.dzlogging import Logger logger = Logger.get_logger('unit-test') class ExampleGeneratorUnitTest(unittest.TestCase): def setUp(self): self.gen = ExampleGenerator('testgen', 'KingBaseLite2019-C60-C99.pgn') self.batch_size = 64 def test_get_gen(self): for i, exs in enumerate(self.gen.get(batch_size=self.batch_size)): logger.info( f'Batch {i} delivered, shapes: {exs[0].shape}, {exs[1][0].shape}, {exs[1][1].shape}' ) if i == self.batch_size: break if __name__ == '__main__': unittest.main()
import io import os import chess.pgn as pgn import numpy as np from engine import Stockfish from core.environment import ChessEnvironment from core.utils import labels from core.dzlogging import Logger logger = Logger.get_logger('ExampleGenerator') GENERATORS = { 'lichess': 'lichess_db_standard_rated_2013-07.pgn', 'kingbase': 'KingBaseLite2019-C60-C99.pgn' } class ExampleGenerator(object): ''' A wrapper class providing support for mini-batch training of the neural network. This class handles the batch preprocessing steps - namely reading the PGN files for supervised learning and generating training examples from the games. ''' def __init__(self, tag): self.tag = tag self.floc = GENERATORS[self.tag] Stockfish.ENGINE.start()
import argparse import multiprocessing as mp import os from random import shuffle import numpy as np import ray from core.network import ChessNetwork from core.environment import ChessEnvironment from core.agent import ChessAgent from core.mcts import MCTS from core.dzlogging import Logger logger = Logger.get_logger('selfplay') ray.init() @ray.remote class Runner(object): ''' A `ray` actor responsible for executing a single session of self-play. ''' def __init__(self, net_name, iteration, version='current'): ''' Params ------ net_name (str): the name of the network to use iteration (int): the game number in this self-play session
import argparse import os import sys sys.path.append(os.path.join(os.path.pardir, os.path.dirname(__file__))) from generators import ExampleGenerator from core.neuralnets.kerasnet import KerasNetwork from core.dzlogging import Logger logger = Logger.get_logger('supervised-learning') def train(net_name, generator_tag, version='current', ckpt=None): net = KerasNetwork(net_name) generator = ExampleGenerator(generator_tag) if version not in ('current', 'nextgen'): logger.fatal(f'Invalid version type: {version}. ' 'Must be "nextgen" or "current"') raise ValueError('Invalid version type') try: net.load(version, ckpt) except RuntimeError as e: logger.warn('There was an error loading the model... ' 'You are training a fresh model!') net.train_generator(generator)
from keras.engine import * from keras.layers import * from keras.models import Model, load_model from keras.optimizers import SGD import numpy as np import tensorflow as tf from tqdm import tqdm from .neuralnet import NeuralNetwork, Config from core.utils import labels, dotdict, tfsession from core.dzlogging import Logger logger = Logger.get_logger('KerasNet') class KerasNetwork(NeuralNetwork): ''' A Keras implementation of the neural network architecture. Architectural decisions follow very closely the approach outlined in the paper, with minor deviations. Once a stable architecutre is found it will be described in a markdown file in this directory. ''' def __init__(self, name='delta_zero', config=Config.default_config()): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.9, allow_growth=True) config_proto = tf.ConfigProto(gpu_options=gpu_options) self.graph = tf.Graph() self.session = tf.Session(graph=self.graph, config=config_proto)
import asyncio import os import platform import chess.engine as engine import numpy as np from core.dzlogging import Logger logger = Logger.get_logger('Stockfish') class Stockfish(object): '''Utility class for performing board evaluations.''' class _Stockfish(object): '''Singleton object''' def __init__(self): self.t = None self.e = None asyncio.set_event_loop_policy(engine.EventLoopPolicy()) self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) def start(self): async def establish(): current_os = platform.system() ep = os.path.join( os.path.pardir, 'data', f'stockfish-10-{"win" if current_os == "Windows" else "linux"}', f'{current_os}', f'stockfish_10_x64{".exe" if current_os == "Windows" else ""}'
import sys import os import unittest sys.path.append(os.path.join(os.path.pardir, os.path.dirname(__file__))) from core.oomcts import MCTS from core.neuralnets.kerasnet import KerasNetwork from core.environment import ChessEnvironment from core.dzlogging import Logger logger = Logger.get_logger('mcts-unittest') class MCTSUnitTest(unittest.TestCase): def setUp(self): self.env = ChessEnvironment() self.net = KerasNetwork() self.mcts = MCTS(self.net) def test_gameplay(self): while not self.env.is_game_over: pi = self.mcts.pi(self.env) action = pi['a'] self.env.push_action(action) self.mcts.update(action) print(self.env) if __name__ == '__main__': unittest.main()
from abc import ABCMeta, abstractmethod import pickle from core.dzlogging import Logger config_logger = Logger.get_logger('Config') class NeuralNetwork(object, metaclass=ABCMeta): '''Abstract base class for neural networks in the context of DeltaZero. This provides a common interface for all potential neural network implementations, independent of framework choice. ''' def __init__(self, name, config): '''Constructs a neural network. Params ------ name (str): The name of the network. Used for IO operations. config (Config): The network configuration, containing hyperparameters and metadata. ''' self.name = name self.config = config self.model = self.build() @abstractmethod def build(self): ''' Builds the network model. '''