zach1502 / BetaChess

BetaChess is an MCTS-based Chess Engine with Deep Neural Networks inspired by AlphaZero, featuring customizable DNN architecture, self-play training with MCTS, and UCI support.
3 stars 0 forks source link

How to use model after save? #1

Open letho1608 opened 9 months ago

zach1502 commented 9 months ago

Once you have a trained model, you can use encoding.py to encode some board state, give that to the model. Then use encoding.py again to decode to get the resulting move. It should look somewhat similar to what I did in MCTS_self_play in MCTS.py.

letho1608 commented 9 months ago

Once you have a trained model, you can use encoding.py to encode some board state, give that to the model. Then use encoding.py again to decode to get the resulting move. It should look somewhat similar to what I did in MCTS_self_play in MCTS.py.

Actually, I still don't understand your project very well, I just ran the pipeline and saw a model in .tar format. I'm not much of an expert in coding so can you show me the process of using your code and how to use that .tar format model?

zach1502 commented 9 months ago

So, here is a high level overview of my project. pipeline.py: This is the file you run to continuously swap between performing MCTS and training. MCTS.py: This file implements the Monte Carlo Tree Search Algorithm. Basically sampling random moves and checking the result of doing such moves. beta_chess.py: Contains the full model architecture along with the training functions. This is where the model is built and trained. arena.py: This file compares the performance of all the trained models. (If you need a reference, this should be a good start) encoding.py: This file can convert a chess board into a format that the model can read and understand.

So, regarding that question of yours on how to use a model.

  1. Loading the Model: First load the model you have saved. There is a function called load_model in the arena.py file that does exactly this and you can reference.
  2. Set up a board: Just like in a real game, you have to set up a chess board. I used a chess python library, it was simply called chess. On line 99 in arena.py, you can see how to set up a chess board.
  3. Find the Best Move: Once the model is ready and the board is set, you use something called UCT_search from MCTS.py. This is like asking the model to think about the best move in the current game situation. The model will then suggest what it thinks is the best move.
  4. Translate the Move: The move suggested by the model is in a special format that only the model understands, but not in a way you would move pieces on a chessboard. So, we need to translate the format into an actual chess move. This is done using decode_action from encoding.py.
  5. Making the Move on the Board: Finally, you take the translated move and apply it to the chessboard. This is how the model's suggestion becomes an actual move in the game.

Hopefully this is clear enough for you, I am making a few assumptions to what you know as a programmer. Let me know if there is something you don't fully grasp or understand. I also want to emphasize looking into arena.py it likely has similar code to what you're looking for!