深度学习实现象棋
When Gary Kasparov was dethroned by IBM’s Deep Blue chess algorithm, the algorithm did not use Machine Learning, or at least in the way that we define Machine Learning today.
当加里·卡斯帕罗夫(Gary Kasparov)被IBM的Deep Blue国际象棋算法废th时,该算法未使用机器学习,或者至少没有采用我们今天定义的机器学习方式。
This article aims to use Neural Networks to create a successful chess AI, by using Neural Networks, a newer form of machine learning algorithms.
本文旨在通过使用神经网络(一种新形式的机器学习算法),使用神经网络创建成功的国际象棋AI。
概念: (Concept:)
Using a chess dataset with over 20,000 instances (contact at victorwtsim@gmail.com for dataset), the Neural Network should output a move, when given a chess-board.
使用具有20,000多个实例的国际象棋数据集(有关数据集,请联系victorwtsim@gmail.com与我们联系),当获得国际象棋棋盘时,神经网络应输出一个动作。
代码: (The Code:)
步骤1 | 制备: (Step 1| Preparation:)
import os
import chess
import numpy as np
import pandas as pd
from tensorflow import keras
from tensorflow.keras import layers
These libraries are the prerequisites to create the program: os and pandas are to access the dataset, python-chess is an “instant” chess-board to test the neural network. Numpy is necessary to perform matrix manipulation. Keras is to create the Neural Network.
这些库是创建程序的先决条件:os和pandas将访问数据集,python-chess是测试神经网络的“即时”棋盘。 要执行矩阵操作,必须要有个Numpy。 Keras将创建神经网络。
步骤2 | 访问数据: (Step 2| Access Data:)
os.chdir('XXXXXXXXXXX')
df = pd.read_csv('chess_normalized.csv')
data = df['moves'].tolist()[:500]
split_data = []
indice = 500
All we need for this project is the pgn of each chessgame from the dataset. Please change the directory path for the os.chdir function to access the directory that the dataset is in.
该项目所需的只是数据集中每个国际象棋游戏的pgn。 请更改os.chdir函数的目录路径以访问数据集所在的目录。
步骤3 | 一键式词典: (Step 3| One-hot dictionaries:)
chess_dict = {
'p' : [1,0,0,0,0,0,0,0,0,0,0,0],
'P' : [0,0,0,0,0,0,1,0,0,0,0,0],
'n' : [0,1,0,0,0,0,0,0,0,0,0,0],
'N' : [0,0,0,0,0,0,0,1,0,0,0,0],
'b' : [0,0,1,0,0,0,0,0,0,0,0,0],
'B' : [0,0,0,0,0,0,0,0,1,0,0,0],
'r' : [0,0,0,1,0,0,0,0,0,0,0,0],
'R' : [0,0,0,0,0,0,0,0,0,1,0,0],
'q' : [0,0,0,0,1,0,0,0,0,0,0,0],
'Q' : [0,0,0,0,0,0,0,0,0,0,1,0],
'k' : [0,0,0,0,0,1,0,0,0,0,0,0],
'K' : [0,0,0,0,0,0,0,0,0,0,0,