<jupyter_start><jupyter_text>Download & unzip a file from herehttps://database.lichess.org/<jupyter_code>pgn = open("lichess_db_standard_rated_2018-10.pgn")
g = chess.pgn.read_game(pgn)
g.headers["Result"]
board = g.board()

board
import re<jupyter_output><empty_output><jupyter_text>increase/decrease the number of samples by changing the range of the for loop<jupyter_code>%%time
moves = []

for i in range(10000):
    g = chess.pgn.read_game(pgn)
    board = chess.Board()
    for move in g.mainline_moves():
        board.push(move)
        s = board.fen()
        list(s)
        s = re.sub(r'\s.*$','',s)
        s = re.sub(r'8','11111111',s)
        s = re.sub(r'7','1111111',s)
        s = re.sub(r'6','111111',s)
        s = re.sub(r'5','11111',s)
        s = re.sub(r'4','1111',s)
        s = re.sub(r'3','111',s)
        s = re.sub(r'2','11',s)
        s = s.split('/')
        s = [list(i) for i in s]
#         t = re.sub(r'r','2',s)
#         u = re.sub(r'R','3',t)
#         v = re.sub(r'n','4',u)
#         w = re.sub(r'N','5',v)
#         x = re.sub(r'b','6',w)
#         y = re.sub(r'B','7',x)
#         z = re.sub(r'q','8',y)
#         a = re.sub(r'Q','9',z)
#         b = re.sub(r'k','10',a)
#         c = re.sub(r'K','11',b)
#         d = re.sub(r'p','12',c)
#         e = re.sub(r'P','13',d)
        


        moves.append((s,g.headers["WhiteElo"],g.headers["BlackElo"],g.headers["Result"]))<jupyter_output>Wall time: 1min 17s<jupyter_text>Put everything in a dataframe. Fix results to be in the perspective of white.<jupyter_code>df = pd.DataFrame(moves,columns=["fen_list","white_elo","black_elo","result"])
df["result"] =df["result"].replace("1-0",1.0)
df["result"] =df["result"].replace("0-1",0.0)
df["result"] =df["result"].replace("1/2-1/2",0.5)
targets = df[["white_elo","black_elo","result"]]<jupyter_output><empty_output><jupyter_text>Convert fen_list to a np.array()<jupyter_code>import numpy as np
fen = np.vstack(df["fen_list"])
l = len(moves)<jupyter_output><empty_output><jupyter_text>reshape the fen to a shape of 8,8 (board size) and the number of samples<jupyter_code>fen = fen.reshape(l,8,8)
fen = fen[:100000]
targets =df["result"][:100000]
fen.shape<jupyter_output><empty_output><jupyter_text>vectorize convert 1 to 0 and all letter to numbers.This is in order to work with the to_categorical function<jupyter_code>fen[fen=='1'] = 0
fen[fen=='r'] = 1
fen[fen=='R'] = 2
fen[fen=='n'] = 3
fen[fen=='N'] = 4
fen[fen=='b'] = 5
fen[fen=='B'] = 6
fen[fen=='q'] = 7
fen[fen=='Q'] = 8
fen[fen=='k'] = 9
fen[fen=='K'] = 10
fen[fen=='p'] = 11
fen[fen=='P'] = 12
import keras
from keras.utils import to_categorical
from keras.utils import np_utils
encoded_fen = np_utils.to_categorical(fen,13,)
encoded_fen.shape
from keras.models import Sequential
from keras import layers<jupyter_output><empty_output><jupyter_text>Structure, compile, train and validate the training of the model.<jupyter_code>!pip install tensorflowjs
import os
model = Sequential()
model.add(layers.Conv2D(32,(3,3),activation='relu',
                        input_shape=(8,8,13)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Flatten())
model.add(layers.Dense(512,activation='relu'))
model.add(layers.Dense(1,activation='sigmoid'))

model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics =['acc'])
blackbox = model.fit(encoded_fen, targets, epochs= 10, batch_size = 64, validation_split=0.2)

tfjs.converters.save_keras_model(model, os.path)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19657142857142856, 'fraction_numerical': 0.041428571428571426, 'mean_word_length': 4.304545454545455, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '241122', 'license': 'MIT', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1383, 'n_words': 248, 'path': 'lichess games.ipynb', 'repo_name': 'wel51x/ItsOver9000-DS', 'size': '3500'}