How to Create a Chess Engine with PyTorch (Python)

preview_player
Показать описание
🐍 Learn how to parse chess data, and train a neural network using PyTorch.

⚔️ Watch as I play a game against the trained model.

Timelines:
00:00 Intro
00:34 why PyTorch?
00:58 parsing data
01:52 preprocessing data
04:20 class ChessDataset
05:04 class ChessModel
07:37 training loop
09:20 making predictions
11:17 Game 1
14:41 Game 2
15:43 Conclusions

#pytorch #python #chess #chessengine #tensorflow #neuralnetworks #programming #coding #keras #deeplearning
Рекомендации по теме
Комментарии
Автор

What you can do to improve it is follow DeepMind's approach. You trained your supervised model and its pretty decent, It could probably beat any 900 elo player. What you wanna do now is take the model and chuck it into reinforcement learning to roughen up the endgame edges. I believe early game is strong because early game is consistent in training data, but no 2 end games are the same so it's lacking in training material there. I feel reinforcement learning here would greatly improve it.

Vedranation
Автор

I understand the effort of building a chess engine that does not rely at all on position evaluation, but I feel that the problem with captures and the endgame is tightly bound to it: if you train your model just at predicting the next move, how it is going to act on a position that it has never seen? Just randomly. For example I bet that it could easily lose to a completely absurd gambit. It needs to learn some kind of evaluation function, and use it to look more into the future: if not you would need to have infinite learning data, up to possibly thousands of moves (there’s a video on the longest chess game possible). Anyway, I still feel the core problem is to treat chess as a Markov process, in which state solely determines the next best one. While being useful probably for the opening, it is detrimental to learn just to imitate and not to play.

danieledepasquale
Автор

Interesting! I am also making my own chess engine at the moment and am shocked to see what is similar and different in our approaches. I believe I have some stuff that might make your model better.

First off, the 13th channel in your input data might really confuse the model considering it only contains data about where pieces can move TO, not WHICH piece can do that and from where. Instead, you should have the 13th channel completely filled with 1s or 0s based on what side it is playing (since right now it has to also predict if it is playing black or white). Not directly telling the model which side it is playing IS THE REASON why it was previously prone to making many illegal moves since it could not train well (I did the same mistake of having 12 channels also). Sure, it might still want to make illegal moves sometimes, but instead of doing a legal check for each input board position, you can check if the predicted move is legal, and if it is not, choose the next best-predicted move. I saw you made a comment about a 14th dimension, and even though I can buy your reasoning, I do not believe this is the most effective strategy considering the model will have to make many extra predictions along the way when many FROM squares are filled and many TO squares are filled.

Also during training, opening moves are irrelevant considering many of them are interchangeable. Your model will be punished by choosing one opening over another, which is not fair. I was shocked to see that even though my model never trained on opening moves, it could still predict near-perfect theory when getting better at the game as a whole. It also reached better precision after training without opening moves because it was not unfairly punished.

Another big tip is using batch normalization after each convolutional layer. It might make the training process much slower, but each part of the training gets much more efficient, resulting in a much higher precision after the same amount of epochs. For example, my model took about 7000 games of training to reach 2% accuracy without batch normalization and after implementation, it was reached already after 500 games.

lukke
Автор

In future save the tensor data to a file and read it in chunks for training so you can train on a larger dataset without memory issues

YihyunML
Автор

Great Video! Could you share some input what kind of changes you made along the way and what your first outputs looked like?

For my own model, I am now also running into the problem that my engine is generating illegal moves when black is to play.

My first logits outputs had a mean of -13, ... and a std of 3.4~.
what about you? but that's only after training it for 5 epochs due to time constraints.

to make my improvements i will add another channel to indicate which side is to play, as some other comment here suggested.

also your move_to_int dictionary approach is interesting and i tried it the same way. with my dataset, my move dictionary results in about 1950 moves, with about 55% of the moves being white

what would you change in your approach to this project in hindsight?
great work and thanks for the informative video

mfnomad
Автор

That is an awesome video! I have two main questions: 1. Why did you shuffle the dataset, since the games itself might be considered as a series of moves through time (though one game has nothing to do with the other). 2. Why did you choose CNN, I understand that you could frame the input as an image, but it would be fun to see the difference to a regular MLP.

Also I've played against your bot and it seems to be very solid (congrats) it also made the same blunder as it did when playing against you (giving away the rook with a check) - since the bot is actually learning from moves (and there are 10^120) and most games have early and midgames but not so many late games, it may seem that the use of a evaluation calculation might be needed (hence Reinforcement Learning)

robertomartins
Автор

i have been trying to do this exact same thing for the last month. I took the exact same approach without that 13th dimension and never got good results, its impressive how such a little thing can have such a huge impact. Thanks a lot, great video

nmxnunezz
Автор

How come a person who is this passionate in coding and talented have less subscribers than all the cringe people on the internet?

strnx
Автор

The problem is the sample size of your dataset, its wayyy too small. It learns the openings and part of the middlegame almost by heart but then generalizes very poorly once its out of book. A general rule of thumb is 1m positions per hidden layer node, and thats just for nnue and not a policy net as you are trying to train. The community around Leela Chess Zero should be able to help you with those kind of chess bots :)

evildonuts
Автор

Есть какое-то логическое обоснование тому, почему при одинаковых слоях и параметрах модели так по-разному отрабатывают? Или вся разница как раз в 13м канале? Спасибо за видео

ДмитрийВренгер
Автор

really decent bot, it is better than my pure nural network bot, my bot gives evaluation value to a position as output than negamax

ponmuthu..
Автор

i didnt get the action space part as ı understand your action space is the number of unique moves in the dataset, and ı think it leads to the model cant choose any other move that is not in the data set. The move index in the action space depends on the data as well. you can use alpha zeros action representation 56 queen moves (8 direction 7 max length) 8 knight moves and 9 underpromotion(3 pieces 3 direction) for 8*8 board in the and u have 73*8*8 matrix after flattening 4672 length action space.

Neo-
Автор

weak model I train with 10k games but still cant play decent moves.

teamfoxear
Автор

Bro i beat him in 30 seconds and im 1200

Translator
Автор

My minimax with alphabeta of depth 3 even outplays your bot. Maybe I need to train more

minnhnguyenn
join shbcf.ru