Chess ai training using tensorflow
- You must have python downloaded, if not, go to the official python downloads page https://www.python.org/downloads/ and select the suitable one for your operating system. You can follow the guide on https://www.geeksforgeeks.org/how-to-install-python-on-windows/ or https://www.geeksforgeeks.org/how-to-download-and-install-python-latest-version-on-linux/
- Then run
pip install notebook
in your terminal, for windows, it is the Command Prompt. - To open the notebook, run
python -m notebook
- Download all the files in this github repository by clicking Code and the Download zip
- Then unzip the all the files
- After that, open terminal and run
pip install -r requirements.txt
to install all required modules and dependencies.
- I have already trained the ai model on games played by Paul Morphy
- To run the ai, navigate into \latest_model\morphy and copy the file path, or you can do it for any other pretrained models there too
- Then edit chess_ai.py using notepad replacing
path_to_model = 'C:/Users/dragon/Documents/Chess-Ai-Training/latest_model/morphy'
with the actual file path you copied - Remember to replace the backward slash (\) with the forward slash (/)
- Go into the terminal
- Run
cd [path of the unzipped files]
- And run
python chess_ai.py
- To play against a lower depth version, which I recommend, run
python chess_ai_lowerdepth.py
- Navigate into /scripts for normal depth or /scripts-for-lowerdepth for lowerdepth version
- Then edit [NAME OF PLAYER].py using notepad replacing
path_to_model = 'C:/Users/dragon/Documents/Chess-Ai-Training/latest_model/morphy'
with the actual file path you copied - Remember to replace the backward slash (\) with the forward slash (/)
- Go into the terminal
- Run
cd [path of the unzipped files]/scripts
orcd [path of the unzipped files]/scripts-for lowerdepth
- And run
python [NAME OF PLAYER].py
- Sign in to google colaboratory with your google account in https://colab.research.google.com/
- Then access this colaboratory link https://colab.research.google.com/drive/1nxjoaTGyaluC8SDsb9VZEeLL5U3hXGP0?usp=sharing
- And just run all the code cells and you are good!
- Navigate into raw_pgn_data and paste the pgn containing all the games played by a player
- You can find many games played by Grandmasters here https://www.pgnmentor.com/files.html
- Then in the terminal, run
python -m notebook
to start jupyter notebook - Navigate to the directory of this repository
- Open split_pgn_data.ipynb
- Replace
input_pgn_file = 'C:/Users/dragon/Documents/Chess-Ai-Training/raw_pgn_data/Morphy.pgn'
with the file path for the pgn file containing your new pgn file. - Create the dir split_pgn_data/[NAME OF NEW PLAYER]
- Replace
output_directory = 'split_pgn_data/morphy'
withoutput_directory = 'split_pgn_data/[NAME OF NEW PLAYER]'
for the output after splitting the pgn files. - Then hit shift + enter to run the cell
- Check in your local file system that there is a new directory called
split_pgn_data/[NAME OF NEW PLAYER]
and it consist of the splitted pgn data and wait for the execution to complete.
- After splitting the pgn data, you will now need to open the process_data.ipynb file
- Similarly, replace this line of code
for dirname, _, filenames in os.walk('C:/Users/dragon/Documents/Chess-Ai-Training/split_pgn_data/morphy'):
withfor dirname, _, filenames in os.walk('[PATH OF THE REPOSITORY]/split_pgn_data/[NEW PLAYER NAME]'):
- Create the dir processed_pgn_data/[NAME OF NEW PLAYER]
- Also replace
new_dirname = 'C:/Users/dragon/Documents/Chess-Ai-Training/processed_pgn_data/morphy'
, which is where the csvs will be located withnew_dirname = '[PATH OF THE REPOSITORY]/processed_pgn_data/[NEW PLAYER NAME]'
- Now, you can just hit shift + enter to run the cell
- Check in your local file system there is a new directory named
[PATH OF THE REPOSITORY]/processed_pgn_data/[new player name]
, note that this execution may take some time, depending on the number of games in your pgn.
- After finish proccessing the games you can open engine_train.ipynb for the REAL training.
- First, you must run the first code block containing all the import's necessary with shift + enter
- Then, replace this line
path_csv = 'C:/Users/dragon/Documents/Chess-Ai-Training/processed_pgn_data/morphy'
withpath_csv = '[PATH OF THE REPOSITORY]/processed_pgn_data/[NEW PLAYER NAME]'
- Create the dir estimator/[NAME OF NEW PLAYER]
- Then find this line of code below
linear_est = tf.estimator.LinearClassifier(feature_columns = feature_columns, model_dir='C:\\Users\\dragon\\Documents\\Chess-Ai-Training\\estimator\\morphy')
and replace it withlinear_est = tf.estimator.LinearClassifier(feature_columns = feature_columns, model_dir='[PATH OF THE REPOSITORY]\\estimator\\[NEW PLAYER NAME]')
!THE DOUBLE BACKWARD SLASH IS IMPORTANT HERE! - Then replace the estimator base path
estimator_base_path = 'C:\\Users\\dragon\\Documents\\Chess-Ai-Training\\estimator\\morphy'
with[PATH OF THE REPOSITORY]\\estimator\\[NEW PLAYER NAME]'
- In your file system, you may need to navigate into estimator and create a new folder called: [NEW PLAYER NAME]
- You are all set, run this notebook from start to finish! This is going to take a few hours so why not take a rest. All your progress will be saved in the estimator/[NEW PLAYER NAME] folder.
- After the code runs, you can find your saved_model.pb file in one of the folders, eg. 1698915549 in your estimator folder.
- Now you can move the folder into the latest_model folder and change the model folder name to [NEW PLAYER NAME]
- Open the chess_ai.py file with notepad
- Change
path_to_model = 'C:/Users/dragon/Documents/Chess-Ai-Training/latest_model/morphy'
topath_to_model = '[PATH OF THE REPOSITORY]/latest_model/[NEW PLAYER NAME]'
- Save the updated python file
- Run it with
python chess_ai.py
and play against your newly created ai!
- White: Chess Ai trained with Marshall's games at lowerdepth, Black: Me, rated 2000 on chess.com (https://lichess.org/8uxS4c1G/)
- White: Chess Ai trained with Morphy's games at lowerdepth, Black: Me, rated 2000 on chess.com (https://lichess.org/o3g4hr4T)
- White: Chess Ai trained with Morphy's games, Black: Me, rated 2000 on chess.com (https://lichess.org/NFxZZP5g)
- White: Chess Ai trained with Dragjon's games at lowerdepth, Black: Me, rated 2000 on chess.com (https://lichess.org/79YMFl9c)
- White: Chess Ai trained with Marshall's games at lowerdepth, Black: Ofish Depth 4 (https://lichess.org/LYrQLW3D)