Dragjon / Chess-Ai-Training

Chess ai training using tensorflow based on IAmEthanMai's code on github https://github.com/iAmEthanMai/chess-engine-model
MIT License
2 stars 0 forks source link
chess-ai chess-ai-engine chess-bot chess-training deep-neural-networks neural-network-chess

Chess-Ai-Training

Chess ai training using tensorflow

Install Jupyter notebook

  1. 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/
  2. Then run pip install notebook in your terminal, for windows, it is the Command Prompt.
  3. To open the notebook, run python -m notebook

    Download

  4. Download all the files in this github repository by clicking Code and the Download zip
  5. Then unzip the all the files
  6. After that, open terminal and run pip install -r requirements.txt to install all required modules and dependencies.

    Playing the default model (morphy)

  7. I have already trained the ai model on games played by Paul Morphy
  8. 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
  9. 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
  10. Remember to replace the backward slash (\) with the forward slash (/)
  11. Go into the terminal
  12. Run cd [path of the unzipped files]
  13. And run python chess_ai.py
  14. To play against a lower depth version, which I recommend, run python chess_ai_lowerdepth.py

    Playing against other models

  15. Navigate into /scripts for normal depth or /scripts-for-lowerdepth for lowerdepth version
  16. 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
  17. Remember to replace the backward slash (\) with the forward slash (/)
  18. Go into the terminal
  19. Run cd [path of the unzipped files]/scripts or cd [path of the unzipped files]/scripts-for lowerdepth
  20. And run python [NAME OF PLAYER].py

    Playing against the completed model using google colaboratory

  21. Sign in to google colaboratory with your google account in https://colab.research.google.com/
  22. Then access this colaboratory link https://colab.research.google.com/drive/1nxjoaTGyaluC8SDsb9VZEeLL5U3hXGP0?usp=sharing
  23. And just run all the code cells and you are good!

    Creating a custom ai (Splitting pgn data)

  24. Navigate into raw_pgn_data and paste the pgn containing all the games played by a player
  25. You can find many games played by Grandmasters here https://www.pgnmentor.com/files.html
  26. Then in the terminal, run python -m notebook to start jupyter notebook
  27. Navigate to the directory of this repository
  28. Open split_pgn_data.ipynb
  29. 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.
  30. Create the dir split_pgn_data/[NAME OF NEW PLAYER]
  31. Replace output_directory = 'split_pgn_data/morphy' with output_directory = 'split_pgn_data/[NAME OF NEW PLAYER]' for the output after splitting the pgn files.
  32. Then hit shift + enter to run the cell
  33. 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.

    Creating a custom ai (Processing data into csvs)

  34. After splitting the pgn data, you will now need to open the process_data.ipynb file
  35. Similarly, replace this line of code for dirname, _, filenames in os.walk('C:/Users/dragon/Documents/Chess-Ai-Training/split_pgn_data/morphy'): with for dirname, _, filenames in os.walk('[PATH OF THE REPOSITORY]/split_pgn_data/[NEW PLAYER NAME]'):
  36. Create the dir processed_pgn_data/[NAME OF NEW PLAYER]
  37. Also replace new_dirname = 'C:/Users/dragon/Documents/Chess-Ai-Training/processed_pgn_data/morphy', which is where the csvs will be located with new_dirname = '[PATH OF THE REPOSITORY]/processed_pgn_data/[NEW PLAYER NAME]'
  38. Now, you can just hit shift + enter to run the cell
  39. 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.

    Creating a custom ai (Training time)

  40. After finish proccessing the games you can open engine_train.ipynb for the REAL training.
  41. First, you must run the first code block containing all the import's necessary with shift + enter
  42. Then, replace this line path_csv = 'C:/Users/dragon/Documents/Chess-Ai-Training/processed_pgn_data/morphy' with path_csv = '[PATH OF THE REPOSITORY]/processed_pgn_data/[NEW PLAYER NAME]'
  43. Create the dir estimator/[NAME OF NEW PLAYER]
  44. 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 with linear_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!
  45. 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]'
  46. In your file system, you may need to navigate into estimator and create a new folder called: [NEW PLAYER NAME]
  47. 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.
  48. After the code runs, you can find your saved_model.pb file in one of the folders, eg. 1698915549 in your estimator folder.
  49. Now you can move the folder into the latest_model folder and change the model folder name to [NEW PLAYER NAME]

    Creating a custom ai (Running the custom model)

  50. Open the chess_ai.py file with notepad
  51. Change path_to_model = 'C:/Users/dragon/Documents/Chess-Ai-Training/latest_model/morphy' to path_to_model = '[PATH OF THE REPOSITORY]/latest_model/[NEW PLAYER NAME]'
  52. Save the updated python file
  53. Run it with python chess_ai.py and play against your newly created ai!

    Games it had played

  54. White: Chess Ai trained with Marshall's games at lowerdepth, Black: Me, rated 2000 on chess.com (https://lichess.org/8uxS4c1G/)
  55. White: Chess Ai trained with Morphy's games at lowerdepth, Black: Me, rated 2000 on chess.com (https://lichess.org/o3g4hr4T)
  56. White: Chess Ai trained with Morphy's games, Black: Me, rated 2000 on chess.com (https://lichess.org/NFxZZP5g)
  57. White: Chess Ai trained with Dragjon's games at lowerdepth, Black: Me, rated 2000 on chess.com (https://lichess.org/79YMFl9c)
  58. White: Chess Ai trained with Marshall's games at lowerdepth, Black: Ofish Depth 4 (https://lichess.org/LYrQLW3D)