from lcztools.testing.leela_engine_lc0 import LC0Engine
import numpy as np
import chess.pgn
import time
import json
import collections

# NOTE: Policy values seem to be a tiny bit off from lc0...
#       The reason for this seems to be usage of src/mcts/node.cc::Edge::SetP in lc0
#       This function applies some rounding to policy values
#
# Changing tolerance from 0.00006 to 0.0005
TOLERANCE = 0.0005

engine = LC0Engine()
board = lcztools.LeelaBoard()
# engine.evaluate(board())
net = lcztools.load_network()


def fix_policy_float(policy):
    '''Numpy to normal python float, for json dumps'''
    return collections.OrderedDict((k, float(v)) for k, v in policy.items())


g_max_policy_error = 0
g_max_value_error = 0
g_mse_policy = 0
g_mse_value = 0
g_se_policy_sum = 0
g_se_value_sum = 0
Exemple #2
0
# so training/tf has to be in the Python path
sys.path.append(os.path.expanduser('~/git/leela-chess/training/tf'))

weights_file = os.path.expanduser('~/git/leela-chess/release/weights.txt.gz')

import lcztools

def json_default(obj):
    # Serialize numpy floats
    if isinstance(obj, np.floating):
        return float(obj)
    raise TypeError

print("Test pytorch")
lcz_net = lcztools.load_network('pytorch', weights_file)
lcz_board = lcztools.LeelaBoard()
print(lcz_board)
policy, value = lcz_net.evaluate(lcz_board)
print('Policy: {}'.format(json.dumps(policy, default=json_default, indent=3)))
print('Value: {}'.format(value))

lcz_board.push_uci('e2e4')
print(lcz_board)
policy, value = lcz_net.evaluate(lcz_board)
print('Policy: {}'.format(json.dumps(policy, default=json_default, indent=3)))
print('Value: {}'.format(value))


print("Test tensorflow")
lcz_net = lcztools.load_network('tensorflow', weights_file)
lcz_board = lcztools.LeelaBoard()