Can Convolutional Neural Networks Crack Sudoku Puzzles?

May 16, 2021 · View on GitHub

Sudoku is a popular number puzzle that requires you to fill blanks in a 9X9 grid with digits so that each column, each row, and each of the nine 3×3 subgrids contains all of the digits from 1 to 9. There have been various approaches to solving that, including computational ones. In this project, I show that simple convolutional neural networks have the potential to crack Sudoku without any rule-based postprocessing.

Requirements

  • NumPy >= 1.11.1
  • TensorFlow == 1.1

Background

Dataset

  • 1M games were generated using generate_sudoku.py for training. I've uploaded them on the Kaggle dataset storage. They are available here.
  • 30 authentic games were collected from http://1sudoku.com.

Model description

  • 10 blocks of convolution layers of kernel size 3.

File description

  • generate_sudoku.py create sudoku games. You don't have to run this. Instead, download pre-generated games.
  • hyperparams.py includes all adjustable hyper parameters.
  • data_load.py loads data and put them in queues so multiple mini-bach data are generated in parallel.
  • modules.py contains some wrapper functions.
  • train.py is for training.
  • test.py is for test.

Training

Test

  • Run python test.py.

Evaluation Metric

Accuracy is defined as

Number of blanks where the prediction matched the solution / Number of blanks.

Results

After a couple of hours of training, the training curve seems to reach the optimum.

I use a simple trick in inference. Instead of cracking the whole blanks all at once, I fill in a single blank where the prediction is the most probable among the all predictions. As can be seen below, my model scored 0.86 in accuracy. Details are available in the `results` folder.
LevelAccuracy (#correct/#blanks=acc.)
Easy47/47 = 1.00
Easy45/45 = 1.00
Easy47/47 = 1.00
Easy45/45 = 1.00
Easy47/47 = 1.00
Easy46/46 = 1.00
Medium33/53 = 0.62
Medium55/55 = 1.00
Medium55/55 = 1.00
Medium53/53 = 1.00
Medium33/52 = 0.63
Medium51/56 = 0.91
Hard29/56 = 0.52
Hard55/55 = 1.00
Hard27/55 = 0.49
Hard57/57 = 1.00
Hard35/55 = 0.64
Hard15/56 = 0.27
Expert56/56 = 1.00
Expert55/55 = 1.00
Expert54/54 = 1.00
Expert55/55 = 1.00
Expert17/55 = 0.31
Expert54/54 = 1.00
Evil50/50 = 1.00
Evil50/50 = 1.00
Evil49/49 = 1.00
Evil28/53 = 0.53
Evil51/51 = 1.00
Evil51/51 = 1.00
Total Accuracy1345/1568 = 0.86

References

If you use this code for research, please cite:

@misc{sudoku2018,
  author = {Park, Kyubyong},
  title = {Can Convolutional Neural Networks Crack Sudoku Puzzles?},
  year = {2018},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/Kyubyong/sudoku}}
}

Papers that referenced this repository