Skip to content

Commit

Permalink
adding some __init__ files, address a todo and typo fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Matuiss2 committed Jan 13, 2019
1 parent ccb570d commit 6304e3b
Show file tree
Hide file tree
Showing 24 changed files with 58 additions and 93 deletions.
2 changes: 1 addition & 1 deletion DeepCrazyhouse/src/domain/abstract_cls/_Agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _apply_temperature_to_policy(self, p_vec_small):
else:
# apply exponential scaling
p_vec_small = p_vec_small ** (1 / self.temperature_current)
# renormalize the values to probabilities again
# re-normalize the values to probabilities again
p_vec_small /= p_vec_small.sum()

return p_vec_small
2 changes: 1 addition & 1 deletion DeepCrazyhouse/src/domain/agent/player/MCTSAgent.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def __init__(
piece in their pocket. The number of legal-moves is higher when drop
moves are available.
:param cpuct: CPUCT-value which weights the balance between the policy/action and value term.
The playstyle depends strongly on this value.
The play style depends strongly on this value.
:param dirichlet_epsilon: Weigh value for the dirichlet noise. If 0. -> no noise. If 1. -> complete noise.
The dirichlet noise ensures that unlikely nodes can be explored
:param dirichlet_alpha: Alpha parameter of the dirichlet noise which is applied to the prior policy for the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(
Each threads has it's own channel.
:param batch_policy_results: Shared numpy memory in which the policy results of all threads are stored.
Each threads has it's own channel.
#:param enable_timeout: Decides wether to enable a timout if a batch didn't occur under 1 second.
#:param enable_timeout: Decides whether to enable a timeout if a batch didn't occur under 1 second.
"""
self.net = net
self.my_pipe_endings = pipe_endings
Expand Down
2 changes: 1 addition & 1 deletion DeepCrazyhouse/src/domain/agent/player/util/Node.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def get_mcts_policy(self, q_value_weight=0.65, clip_low_visit_nodes=True): # ,
value[thresh_idces] = 0
value[value < 0] = 0

# renormalize ot 1
# re-normalize ot 1
visit /= visit.sum()

value /= value.sum()
Expand Down
2 changes: 1 addition & 1 deletion DeepCrazyhouse/src/domain/crazyhouse/GameState.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def is_check(self):

def are_pocket_empty(self):
"""
Checks wether at least one player has a piece available in their pocket
Checks if at least one player has a piece available in their pocket
:return:
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def board_to_planes(board, board_occ=0, normalize=True):
for idx, color in enumerate(chess.COLORS):
# the PIECE_TYPE is an integer list in python-chess
for piece_type in chess.PIECE_TYPES:
# define the channel by the piecetype (the input representation uses the same ordering as python-chess)
# define the channel by the piece_type (the input representation uses the same ordering as python-chess)
# we add an offset for the black pieces
# note that we subtract 1 because in python chess the PAWN has index 1 and not 0
channel = (piece_type - 1) + idx * len(chess.PIECE_TYPES)
Expand Down
10 changes: 5 additions & 5 deletions DeepCrazyhouse/src/domain/crazyhouse/output_representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def policy_to_move(policy_vec_clean, is_white_to_move=True):
:param policy_vec_clean: Numpy array which represents the moves
:param is_white_to_move: Define the current player turn
:return: sinlge move - Python chess move object
:return: single move - Python chess move object
"""

mv_idx = np.argmax(policy_vec_clean)
Expand Down Expand Up @@ -98,13 +98,13 @@ def policy_to_moves(board: chess.variant.CrazyhouseBoard, policy_vec, normalize=
:param policy_vec: 1 Dimensional array representing all possible Crazyhouse moves
:param normalize: normalizes the policy vector in the cleaning step
:return: selected_moves - List of all python chess move objects from best to worst
move_probabilites - Corresponding probabilities ordered descending
move_probabilities - Corresponding probabilities ordered descending
"""

policy_vec_clean, nb_legal_moves = set_illegal_moves_to_zero(board, policy_vec, normalize)

sorted_move_value_indices = np.argsort(policy_vec_clean)[::-1]
move_probabilites = np.sort(policy_vec_clean)[::-1]
move_probabilities = np.sort(policy_vec_clean)[::-1]

selected_moves = []

Expand All @@ -120,7 +120,7 @@ def policy_to_moves(board: chess.variant.CrazyhouseBoard, policy_vec, normalize=
move = chess.Move.from_uci(mv_uci)
selected_moves.append(move)

return selected_moves, move_probabilites
return selected_moves, move_probabilities


def set_illegal_moves_to_zero(board: chess.variant.CrazyhouseBoard, policy_vec, normalize=True):
Expand Down Expand Up @@ -216,7 +216,7 @@ def value_to_centipawn(value):
if np.absolute(value) >= 1.0:
# return a constant if the given value is 1 (otherwise log will result in infinity)
return np.sign(value) * 9999
# use logaritmic scaling with basis 1.1 as a pseudo centipawn conversion
# use logarithmic scaling with basis 1.1 as a pseudo centipawn conversion
return -(np.sign(value) * np.log(1.0 - np.absolute(value)) / np.log(1.2)) * 100


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
Description of the alpha zero architecture for gluon:
Mastering the game of Go without
human knowledge, Silver et al.
Mastering the game of Go without human knowledge, Silver et al.
'The input features st are processed by a residual tower that consists of a single
convolutional block followed by either 19 or 39 residual blocks.
Expand All @@ -29,7 +28,7 @@
(2) Batch normalization
(3) A rectifier nonlinearity
(4) A fully connected linear layer that outputs a vector of size 192 +​ 1 =​ 362,
corresponding to logit probabilities for all intersections and the pass move
corresponding to logic probabilities for all intersections and the pass move
The value head applies the following modules:
(1) A convolution of 1 filter of kernel size 1 ×​1 with stride 1
(2) Batch normalization
Expand All @@ -38,20 +37,16 @@
(5) A rectifier nonlinearity
(6) A fully connected linear layer to a scalar
(7) A tanh nonlinearity outputting a scalar in the range [−​1, 1]
The overall network depth, in the 20- or 40-block network, is 39 or 79 param­
eterized layers, respectively, for the residual tower, plus an additional 2 layers for
the policy head and 3 layers for the value head.
The overall network depth, in the 20- or 40-block network, is 39 or 79 parameterized layers, respectively,
for the residual tower, plus an additional 2 layers for the policy head and 3 layers for the value head.
We note that a different variant of residual networks was simultaneously applied
to computer Go33 and achieved an amateur dan-level performance; however, this
was restricted to a single-headed policy network trained solely by supervised
learning.
was restricted to a single-headed policy network trained solely by supervised learning.
Neural network architecture comparison. Figure 4 shows the results of a com­
parison between network architectures. Specifically, we compared four different
neural networks:
Neural network architecture comparison. Figure 4 shows the results of a comparison between network architectures.
Specifically, we compared four different neural networks:
(1) dual–res: the network contains a 20-block residual tower, as described above,
followed by both a policy head and a value head. This is the architecture used in
AlphaGo Zero.
followed by both a policy head and a value head. This is the architecture used in AlphaGo Zero.
(2) sep–res: the network contains two 20-block residual towers. The first tower
is followed by a policy head and the second tower is followed by a value head.
(3) dual–conv: the network contains a non-residual tower of 12 convolutional
Expand All @@ -61,10 +56,9 @@
by a value head. This is the architecture used in AlphaGo Lee.
Each network was trained on a fixed dataset containing the final 2 million
games of self-play data generated by a previous run of AlphaGo Zero, using
stochastic gradient descent with the annealing rate, momentum and regulariza­
tion hyperparameters described for the supervised learning experiment; however,
cross-entropy and MSE components were weighted equally, since more data was
available.'
stochastic gradient descent with the annealing rate, momentum and regularization hyperparameters described for
the supervised learning experiment; however, cross-entropy and MSE components were weighted equally,
since more data was available.'
"""

from mxnet.gluon.nn import HybridSequential, Conv2D, BatchNorm, Activation, Flatten, Dense
Expand Down Expand Up @@ -104,7 +98,7 @@ def __init__(self, channels, bn_mom, act_type, unit_name):

def hybrid_forward(self, F, x):
"""
Implemntation of the forward pass of the residual block.
Implementation of the forward pass of the residual block.
Uses a broadcast add operation for the shortcut and the output of the residual block
:param F: Abstract Function handle which works for gluon & mxnet
:param x: Input to the ResidualBlock
Expand Down Expand Up @@ -259,7 +253,7 @@ def __init__(

def hybrid_forward(self, F, x):
"""
Implemntation of the forward pass of the full network
Implementation of the forward pass of the full network
Uses a broadcast add operation for the shortcut and the output of the residual block
:param F: Abstract Function handle which works for gluon & mxnet
:param x: Input to the ResidualBlock
Expand Down
4 changes: 2 additions & 2 deletions DeepCrazyhouse/src/domain/neural_net/architectures/Rise.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(self, unit_name, cardinality, channels, bn_mom, act_type, res_scale

def hybrid_forward(self, F, x):
"""
Implemntation of the forward pass of the residual block.
Implementation of the forward pass of the residual block.
Uses a broadcast add operation for the shortcut and the output of the residual block
:param F: Abstract Function handle which works for gluon & mxnet
:param x: Input to the ResidualBlock
Expand Down Expand Up @@ -307,7 +307,7 @@ def __init__(

def hybrid_forward(self, F, x):
"""
Implemntation of the forward pass of the full network
Implementation of the forward pass of the full network
Uses a broadcast add operation for the shortcut and the output of the residual block
:param F: Abstract Function handle which works for gluon & mxnet
:param x: Input to the ResidualBlock
Expand Down
10 changes: 5 additions & 5 deletions DeepCrazyhouse/src/domain/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def get_dic_sorted_by_key(dic):
Returns the values of a dictionary based on its sorted keys ordering
:param dic: dictionary with typically an integer as key type
:return: list: sorted values based on key orderung
:return: list: sorted values by key ordering
"""

return [dic[key] for key in sorted(dic)]
Expand Down Expand Up @@ -197,12 +197,12 @@ def customize_input_planes(x):
def mult_axis_by_vec(mat, vec):
# https://stackoverflow.com/questions/30031828/multiply-numpy-ndarray-with-1d-array-along-a-given-axis
"""
Multiplies a matrix by a given vectory elementwise along a given axis
Multiplies a matrix by a given vector element-wise along a given axis
:param mat: Numpy matrix to perform the operation on
:param vec: Numpy array which is a single vector (must have same dim as desired axis)
:return Elementwise multiplied matrix across axis
:return Element-wise multiplied matrix across axis
"""
# Given axis along which elementwise multiplication with broadcasting
# Given axis along which element-wise multiplication with broadcasting
# is to be performed
axis = 0

Expand All @@ -212,7 +212,7 @@ def mult_axis_by_vec(mat, vec):
dim_array = np.ones((1, mat.ndim), np.int16).ravel()
dim_array[axis] = -1

# Reshape b with dim_array and perform elementwise multiplication with
# Reshape b with dim_array and perform element-wise multiplication with
# broadcasting along the singleton dimensions for the final output
b_reshaped = vec.reshape(dim_array)
return mat * b_reshaped
6 changes: 3 additions & 3 deletions DeepCrazyhouse/src/preprocessing/PGN2PlanesConverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@project: DeepCrazyhouse
@author: queensgambit
Loads the pgn-file and calls the pgn_converter functions to create a plane represenation.
Loads the pgn-file and calls the pgn_converter functions to create a plane representation.
Multiprocessing is used for loading, computing and saving.
"""
import datetime
Expand Down Expand Up @@ -160,10 +160,10 @@ def filter_pgn(self):

def _filter_pgn_thread(self, queue, pgn):
"""
Selects the pgn which fullfill the given conditions
Selects the pgn which fulfill the given conditions
:param queue: Stores the result/return variables
:param pgn: PGN file
:return: Queue filled with the following iterms:
:return: Queue filled with the following items:
- all_pgn_sel: List of the selected pgn files
- nb_games_sel: Number of games which have been selected
- batch_white_won: list of number of games which have been won by the white player in this batch
Expand Down
2 changes: 1 addition & 1 deletion DeepCrazyhouse/src/preprocessing/PGNRecordDataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self, dataset_type, input_shape, normalize=True):
def __getitem__(self, idx):
"""
Overwrites the __getitem__ method from RecordFileDataset
Each threads loads an individual datasample from the .rec file
Each threads loads an individual data sample from the .rec file
:param idx: String buffer index to load
:return: x - plane representation
Expand Down
33 changes: 2 additions & 31 deletions DeepCrazyhouse/src/preprocessing/dataset_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,10 @@
"""
import glob
import logging
import chess
import numpy as np
import zarr
from DeepCrazyhouse.configs.main_config import main_config
from DeepCrazyhouse.src.domain.crazyhouse.constants import (
CHANNEL_MAPPING_CONST,
CHANNEL_MAPPING_POS,
MAX_NB_MOVES,
MAX_NB_NO_PROGRESS,
MAX_NB_PRISONERS,
NB_CHANNELS_POS,
POCKETS_SIZE_PIECE_TYPE,
)
from DeepCrazyhouse.src.domain.util import normalize_input_planes
from DeepCrazyhouse.src.domain.util import get_numpy_arrays


Expand Down Expand Up @@ -100,29 +91,9 @@ def load_pgn_dataset(

if normalize is True:
x = x.astype(np.float32)

# the y-vectors need to be casted as well in order to be accepted by the network
y_value = y_value.astype(np.float32)
y_policy = y_policy.astype(np.float32)

# !TODO replace this by function normalize_input_planes()
mat_pos = x[:, :NB_CHANNELS_POS, :, :]
mat_const = x[:, NB_CHANNELS_POS:, :, :]

# iterate over all pieces except the king
for p_type in chess.PIECE_TYPES[:-1]:
# p_type -1 because p_type starts with 1
channel = CHANNEL_MAPPING_POS["prisoners"] + p_type - 1

mat_pos[:, channel, :, :] /= MAX_NB_PRISONERS
# the prison for black begins 5 channels later
mat_pos[:, channel + POCKETS_SIZE_PIECE_TYPE, :, :] /= MAX_NB_PRISONERS

### Total Move Count
# 500 was set as the max number of total moves
mat_const[:, CHANNEL_MAPPING_CONST["total_mv_cnt"], :, :] /= MAX_NB_MOVES
### No progress count
# after 40 moves of no progress the 40 moves rule for draw applies
mat_const[:, CHANNEL_MAPPING_CONST["no_progress_cnt"], :, :] /= MAX_NB_NO_PROGRESS
normalize_input_planes(x)

return starting_idx, x, y_value, y_policy, pgn_dataset
2 changes: 1 addition & 1 deletion DeepCrazyhouse/src/runtime/ColorLogger.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
@file: Colorer.py
Script which allows are colored logging output multiplattform.
Script which allows are colored logging output multi-platform.
The script is based on this post and was slightly adjusted:
# https://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output
Expand Down
4 changes: 2 additions & 2 deletions DeepCrazyhouse/src/tests/FullRoundTripTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def board_single_game(params_inp):
Iterates through a game based on the pgn file and compares the board of the pgn file
with reconvert plane representation.
:param params_inp: Tuple containin the pgn, plane_representation and the start index of the game
:param params_inp: Tuple containing the pgn, plane_representation and the start index of the game
:return: Boolean indicating if the all board states have been equal and the start_idx of the pgn
"""
pgn, x_test, start_idx = params_inp
Expand Down Expand Up @@ -63,7 +63,7 @@ def board_single_game(params_inp):

def moves_single_game(params_inp):
"""
Iterates over all moves of a given pgn file and comparse the reconverted policy representation with
Iterates over all moves of a given pgn file and compares the reconverted policy representation with
the move stored in the pgn file
:param params_inp: pgn file, policy vector, starting index of the game
Expand Down
12 changes: 6 additions & 6 deletions DeepCrazyhouse/src/tests/MoveRoundTripTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ def test_loaded_dataset_black_move(self):

self.assertEqual(prob, 1, msg="The policy vector has to be one hot encoded.")

selected_moves, move_probabilites = policy_to_moves(board, yp_val[1])
selected_moves, move_probabilities = policy_to_moves(board, yp_val[1])
mv_conv2 = selected_moves[0]

self.assertGreater(move_probabilites[0], 0, msg="The move probability must be greater 0")
self.assertEqual(move_probabilites[0], 1, msg="The policy vector has to be one hot encoded.")
self.assertGreater(move_probabilities[0], 0, msg="The move probability must be greater 0")
self.assertEqual(move_probabilities[0], 1, msg="The policy vector has to be one hot encoded.")

converted_moves = [mv_conv0, mv_conv1, mv_conv2]

Expand Down Expand Up @@ -120,11 +120,11 @@ def test_loaded_dataset_white_move(self):

self.assertEqual(prob, 1, msg="The policy vector has to be one hot encoded.")

selected_moves, move_probabilites = policy_to_moves(board, yp_val[1])
selected_moves, move_probabilities = policy_to_moves(board, yp_val[1])
mv_conv2 = selected_moves[0]

self.assertGreater(move_probabilites[0], 0, msg="The move probability must be greater 0")
self.assertEqual(move_probabilites[0], 1, msg="The policy vector has to be one hot encoded.")
self.assertGreater(move_probabilities[0], 0, msg="The move probability must be greater 0")
self.assertEqual(move_probabilities[0], 1, msg="The policy vector has to be one hot encoded.")

converted_moves = [mv_conv0, mv_conv1, mv_conv2]

Expand Down
Empty file.
8 changes: 4 additions & 4 deletions DeepCrazyhouse/src/tests/move_plane_round_trip_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MovePlaneRoundTripTests(unittest.TestCase):
Tests the conversion from moves to planes and back
Queen moves | 56 -> 0..55
Knight moves | 8 -> 56..63
Underpromotions | 9 -> 64..72
Under promotions | 9 -> 64..72
Drop | 5 -> 73..77
"""

Expand Down Expand Up @@ -79,13 +79,13 @@ def test_move_planes_round_trip_given_promotion_expect_round_trip(self):
board = CrazyhouseBoard()

move = construct_move_from_positions([6, 4], [7, 4], promotion=3)
assert_round_trip(self, board, move, comment="underpromotion (straight)")
assert_round_trip(self, board, move, comment="under promotion (straight)")

move = construct_move_from_positions([6, 4], [7, 3], promotion=3)
assert_round_trip(self, board, move, comment="underpromotion (left)")
assert_round_trip(self, board, move, comment="under promotion (left)")

move = construct_move_from_positions([6, 4], [7, 5], promotion=3)
assert_round_trip(self, board, move, comment="underpromotion (right)")
assert_round_trip(self, board, move, comment="under promotion (right)")

def test_move_planes_round_trip_given_drop_expect_round_trip(self):
board = CrazyhouseBoard()
Expand Down
Loading

0 comments on commit 6304e3b

Please sign in to comment.