Skip to content

Commit

Permalink
Swapped Back To Evaluating Positions Independently
Browse files Browse the repository at this point in the history
  • Loading branch information
silverlightning926 committed Jun 22, 2024
1 parent 1ee01f9 commit 1ebb6a4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 44 deletions.
46 changes: 23 additions & 23 deletions src/training/_build_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from keras.api.optimizers import Adam
from src.utils.path_utils import find_project_directory

from src.training._load_dataset import MAX_MOVES, BATCH_SIZE
from src.training._load_dataset import BATCH_SIZE


def build_model():
Expand All @@ -17,43 +17,43 @@ def build_model():
print('Model not found. Building model...')

model = Sequential([
Input(shape=(MAX_MOVES, 8, 8, 12),
Input(shape=(8, 8, 12),
batch_size=BATCH_SIZE, name='input'),

TimeDistributed(
Conv2D(64, (3, 3), activation='relu', padding='same')),
TimeDistributed(BatchNormalization()),
TimeDistributed(MaxPooling2D((2, 2))),
Conv2D(64, (3, 3), activation='relu', padding='same'),
BatchNormalization(),
MaxPooling2D((2, 2)),

TimeDistributed(
Conv2D(128, (3, 3), activation='relu', padding='same')),
TimeDistributed(BatchNormalization()),
TimeDistributed(MaxPooling2D((2, 2))),

TimeDistributed(
Conv2D(256, (3, 3), activation='relu', padding='same')),
TimeDistributed(BatchNormalization()),
TimeDistributed(MaxPooling2D((2, 2))),
Conv2D(128, (3, 3), activation='relu', padding='same'),
BatchNormalization(),
MaxPooling2D((2, 2)),

TimeDistributed(Flatten()),

LSTM(256, return_sequences=True, dropout=0.5,
recurrent_dropout=0.5, stateful=False),
Conv2D(256, (3, 3), activation='relu', padding='same'),
BatchNormalization(),
MaxPooling2D((2, 2)),

TimeDistributed(Dense(512, activation='relu')),
TimeDistributed(Dropout(0.5)),
Flatten(),

TimeDistributed(Dense(256, activation='relu')),
TimeDistributed(Dropout(0.5)),
Dense(512, activation='relu'),
Dropout(0.5),

GlobalAveragePooling1D(),
Dense(256, activation='relu'),
Dropout(0.5),

Dense(128, activation='relu'),
Dropout(0.5),

Dense(64, activation='relu'),
Dropout(0.5),

Dense(1, activation='linear', name='value'),
])

model.compile(
optimizer=Adam(),
loss='categorical_crossentropy',
loss='mean_squared_error',
metrics=['accuracy']
)

Expand Down
31 changes: 10 additions & 21 deletions src/training/_load_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
DATASET_PATH = 'data/games.csv'
PREPROCESSED_DATA_PATH = 'data/preprocessed_data.npz'

MAX_MOVES = 75

BATCH_SIZE = 16

VALIDATION_SPLIT = 0.2
Expand Down Expand Up @@ -51,53 +49,44 @@ def _generate_game_sequences():
if os.path.exists(PREPROCESSED_DATA_PATH):
print('Preprocessed data found. Loading...')
data = np.load(PREPROCESSED_DATA_PATH)
return data['games'], data['winners']
return data['positions'], data['winners']

print('Creating DataFrame...')
df = _read_data()
print(df.head())

if MAX_MOVES is not None:
print('Removing games that are too long...')
df = df[df['moves'].apply(lambda x: len(x.split())) <= MAX_MOVES]

games = []
positions = []
winners = []

tqdm.pandas(desc='Processing Data')

def process_row(row):
moves = []

board = chess.Board()

winner = encode_winner(row['winner'])

for move in row['moves'].split():

board.push_san(move)
moves.append(encode_board(board))

for _ in range(MAX_MOVES - len(row['moves'].split())):
moves.append(np.zeros((8, 8, 12), dtype=np.float32))

games.append(np.array(moves, dtype=np.float32))
winners.append(encode_winner(row['winner']))
positions.append(encode_board(board))
winners.append(winner)

df.progress_apply(process_row, axis=1)

print(f'Size of games: {getsizeof(games)} bytes')
print(f'Size of positions: {getsizeof(positions)} bytes')
print(f'Size of winners: {getsizeof(winners)} bytes')

print('Saving preprocessed data...')
np.savez_compressed(PREPROCESSED_DATA_PATH, games=games,
np.savez_compressed(PREPROCESSED_DATA_PATH, positions=positions,
winners=winners)
print('Preprocessed data saved.')

print('Creating numpy arrays...')
games = np.array(games, dtype=np.float32)
positions = np.array(positions, dtype=np.float32)
winners = np.array(winners, dtype=np.float32)
print('Numpy arrays created.')

return games, winners
return positions, winners


def _generate_dataset(games, winners, ):
Expand Down

0 comments on commit 1ebb6a4

Please sign in to comment.