|
1 | 1 | # Can Convolutional Neural Networks Crack Sudoku Puzzles?
|
2 | 2 |
|
3 |
| -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 that, including computational ones. In this pilot project, we show that convolutional neural networks have the potential to crack Sukoku puzzles without any other rule-based post-processing. |
| 3 | +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 Sukoku without any rule-based postprocessing. |
4 | 4 |
|
5 | 5 | ## Requirements
|
6 |
| - * numpy >= 1.11.1 |
7 |
| - * sugartensor >= 0.0.1.8 (pip install sugartensor) |
| 6 | + * NumPy >= 1.11.1 |
| 7 | + * TensorFlow == 1.1 |
8 | 8 |
|
9 |
| -## Research Question |
10 |
| -Can Convolutional Neural Networks Crack Sudoku Puzzles? |
11 |
| - |
12 | 9 | ## Background
|
13 | 10 | * To see what Sudoku is, check the [wikipedia](https://en.wikipedia.org/wiki/Sudoku)
|
14 | 11 | * To investigate this task comprehensively, read through [McGuire et al. 2013](https://arxiv.org/pdf/1201.0749.pdf).
|
15 | 12 |
|
| 13 | +## Dataset |
| 14 | +* 1M games were generated using `generate_sudoku.py` for training. I've uploaded them on the Kaggle dataset storage. They are available [here](https://www.kaggle.com/bryanpark/sudoku/downloads/sudoku.zip). |
| 15 | +* 30 authentic games were collected from http://1sudoku.com. |
| 16 | + |
| 17 | +## Model description |
| 18 | +* 10 blocks of convolution layers of kernel size 3. |
| 19 | + |
| 20 | +## File description |
| 21 | + * `generate_sudoku.py` create sudoku games. You don't have to run this. Instead, download [pre-generated games](https://www.kaggle.com/bryanpark/sudoku/downloads/sudoku.zip). |
| 22 | + * `hyperparams.py` includes all ajustable hyper parameters. |
| 23 | + * `data_load.py` loads data and put them in queues so multiple mini-bach data are generated in parallel. |
| 24 | + * `modules.py` contains some wrapper functions. |
| 25 | + * `train.py` is for training. |
| 26 | + * `test.py` is for test. |
| 27 | + |
| 28 | + |
16 | 29 | ## Training
|
17 |
| -* STEP 1. Generate 1 million Sudoku games. (See `generate_sudoku.py`). The pre-generated games are available [here](https://www.kaggle.com/bryanpark/sudoku/downloads/sudoku.zip). |
18 |
| -* STEP 2. Construct convolutional networks as follows. (See `Graph` in `train.py`)<br/> |
19 |
| - |
20 |
| -* STEP 3. Train the model, feeding X (quizzes) and Y (solutions). Note that only the predictions for the position of the blanks count when computing loss. (See `train.py`)<br/> |
| 30 | +* STEP 1. Download and extract [training data](https://www.kaggle.com/bryanpark/sudoku/downloads/sudoku.zip). |
| 31 | +* STEP 2. Run `python train.py`. Or download the [pretrained file](https://u42868014.dl.dropboxusercontent.com/u/42868014/sudoku/logdir.tar.gz). |
| 32 | + |
| 33 | +## Test |
| 34 | +* Run `python test.py`. |
21 | 35 |
|
22 |
| -## Evaluation |
23 |
| -We test the performance of the final model against 30 real Sudoku puzzles and their solutions, which vary from the easy to evil level. Metrics are the following two. |
24 |
| -* Accuracy: the number of blanks where our prediction matched to the solution. |
25 |
| -* Success rate: the number of games where 100% accurately matched to our prediction. |
| 36 | +## Evaluation Metric |
| 37 | + |
| 38 | +Accuray is defined as |
| 39 | + |
| 40 | +Number of blanks where the prediction matched the solution / Number of blanks. |
26 | 41 |
|
27 | 42 | ## Results
|
28 |
| -After 4 epochs, we got [the best model file](https://drive.google.com/open?id=0B0ZXk88koS2KV1VIT2RYUGhuOEU). We designed two test methods. |
29 | 43 |
|
30 |
| -* Test method 1: Predict the numbers in blanks all at once. |
31 |
| -* Test method 2: Predict the numbers sequentially the most confident one at a time. |
| 44 | +Ater a couple of hours of training, the training curve seems to reach the optimum. |
| 45 | + |
| 46 | +<img src="fig/training_curve.png"> |
| 47 | +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 avaiable in the `results` folder. |
| 48 | + |
32 | 49 |
|
33 | 50 |
|
34 |
| -| Level | Test1 <br/>(#correct/#blanks=acc.)| Test2 <br/>(#correct/#blanks=acc.) | |
35 |
| -| --- |--- |--- | |
36 |
| -|Easy|43/47=0.91|**47/47=1.00**| |
37 |
| -|Easy|37/45=0.82|**45/45=1.00**| |
38 |
| -|Easy|40/47=0.85|**47/47=1.00**| |
39 |
| -|Easy|33/45=0.73|**45/45=1.00**| |
40 |
| -|Easy|37/47=0.79|**47/47=1.00**| |
41 |
| -|Easy|39/46=0.85|**46/46=1.00**| |
42 |
| -|Medium|27/53=0.51|32/53=0.60| |
43 |
| -|Medium|27/55=0.49|27/55=0.49| |
44 |
| -|Medium|32/55=0.58|36/55=0.65| |
45 |
| -|Medium|28/53=0.53|**53/53=1.00**| |
46 |
| -|Medium|27/52=0.52|33/52=0.63| |
47 |
| -|Medium|29/56=0.52|39/56=0.70| |
48 |
| -|Hard|30/56=0.54|41/56=0.73| |
49 |
| -|Hard|31/55=0.56|28/55=0.51| |
50 |
| -|Hard|33/55=0.60|**55/55=1.00**| |
51 |
| -|Hard|33/57=0.58|**57/57=1.00**| |
52 |
| -|Hard|27/55=0.49|50/55=0.91| |
53 |
| -|Hard|28/56=0.50|27/56=0.48| |
54 |
| -|Expert|32/56=0.57|22/56=0.39| |
55 |
| -|Expert|32/55=0.58|**55/55=1.00**| |
56 |
| -|Expert|37/54=0.69|**54/54=1.00**| |
57 |
| -|Expert|33/55=0.60|**55/55=1.00**| |
58 |
| -|Expert|30/55=0.55|23/55=0.42| |
59 |
| -|Expert|25/54=0.46|**54/54=1.00**| |
60 |
| -|Evil|32/50=0.64|**50/50=1.00**| |
61 |
| -|Evil|33/50=0.66|**50/50=1.00**| |
62 |
| -|Evil|34/49=0.69|**49/49=1.00**| |
63 |
| -|Evil|33/53=0.62|**53/53=1.00**| |
64 |
| -|Evil|35/51=0.69|**51/51=1.00**| |
65 |
| -|Evil|34/51=0.67|**51/51=1.00**| |
66 |
| -|Total Accuracy| 971/1568=0.62| **1322/1568=0.84**| |
67 |
| -|Success Rate| 0/30=0| **19/30=0.63**| |
68 |
| - |
69 |
| -## Conclusions |
70 |
| -* I also tested fully connected layers, to no avail. |
71 |
| -* Up to some point, it seems that CNNs can learn to solve Sudoku. |
72 |
| -* For most problems, the second method was outperform the fist one. |
73 |
| -* Humans cannot predict all numbers simultaneously. Probably so do CNNs. |
74 |
| - |
75 |
| -## Furthery Study |
76 |
| -* Reinforcement learning would be more appropriate for Sudoku solving. |
77 |
| - |
78 |
| -## Notes for reproducibility |
79 |
| -* Download pre-generated Sudoku games [here](https://www.kaggle.com/bryanpark/sudoku/downloads/sudoku.zip) and extract it to `data/` folder. |
80 |
| -* Download the pre-trained model file [here](https://drive.google.com/open?id=0B0ZXk88koS2KV1VIT2RYUGhuOEU) and extract it to `asset/train/ckpt` folder. |
81 |
| - |
| 51 | +| Level | Accuracy (#correct/#blanks=acc.) | |
| 52 | +| --- |--- | |
| 53 | +|Easy|**47/47 = 1.00**| |
| 54 | +|Easy|**45/45 = 1.00**| |
| 55 | +|Easy|**47/47 = 1.00**| |
| 56 | +|Easy|**45/45 = 1.00**| |
| 57 | +|Easy|**47/47 = 1.00**| |
| 58 | +|Easy|**46/46 = 1.00**| |
| 59 | +|Medium|33/53 = 0.62| |
| 60 | +|Medium|**55/55 = 1.00**| |
| 61 | +|Medium|**55/55 = 1.00**| |
| 62 | +|Medium|**53/53 = 1.00**| |
| 63 | +|Medium|33/52 = 0.63| |
| 64 | +|Medium|51/56 = 0.91| |
| 65 | +|Hard|29/56 = 0.52| |
| 66 | +|Hard|**55/55 = 1.00**| |
| 67 | +|Hard|27/55 = 0.49| |
| 68 | +|Hard|**57/57 = 1.00**| |
| 69 | +|Hard|35/55 = 0.64| |
| 70 | +|Hard|15/56 = 0.27| |
| 71 | +|Expert|**56/56 = 1.00**| |
| 72 | +|Expert|**55/55 = 1.00**| |
| 73 | +|Expert|**54/54 = 1.00**| |
| 74 | +|Expert|**55/55 = 1.00**| |
| 75 | +|Expert|17/55 = 0.31| |
| 76 | +|Expert|**54/54 = 1.00**| |
| 77 | +|Evil|**50/50 = 1.00**| |
| 78 | +|Evil|**50/50 = 1.00**| |
| 79 | +|Evil|**49/49 = 1.00**| |
| 80 | +|Evil|28/53 = 0.53| |
| 81 | +|Evil|**51/51 = 1.00**| |
| 82 | +|Evil|**51/51 = 1.00**| |
| 83 | +|Total Accuracy| 1345/1568 = _0.86_| |
82 | 84 |
|
83 | 85 |
|
84 | 86 |
|
|
0 commit comments