In [1]:
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
from keras.models import load_model
import sklearn
import numpy as np
In [100]:
#from sklearn.model_selection import train_test_split
#I did not write this code. It was provided in the kaggle page
#https://www.kaggle.com/bryanpark/sudoku?sortBy=null&group=datasets
quizzes = np.zeros((1000000, 81), np.int32)
solutions = np.zeros((1000000, 81), np.int32)
for i, line in enumerate(open('sudoku.csv', 'r').read().splitlines()[1:]):
quiz, solution = line.split(",")
for j, q_s in enumerate(zip(quiz, solution)):
q, s = q_s
quizzes[i, j] = q
solutions[i, j] = s
#quizzes = quizzes.reshape((-1, 9, 9))
#solutions = solutions.reshape((-1, 9, 9))
# make a train test split
#quizzes are inputs
#conver the outputs to be categorical outputs
#y_test = keras.utils.to_categorical(y_test, num_classes)
In [42]:
#quizzes = np.expand_dims(quizzes, -1)
#quizzes.shape
In [43]:
solutions[0]
Out[43]:
In [ ]:
solutions[1]
In [2]:
from sklearn.model_selection import train_test_split
quiz_train, quiz_test, output_train, output_test = sklearn.model_selection.train_test_split(quizzes, solutions, test_size = 0.2, random_state = 42)
In [3]:
from keras.layers.normalization import BatchNormalization
In [52]:
#need 729 outputs 81 cells. 81 possible probabilities
mlp = Sequential()
mlp.add(Dense(256, activation = 'relu', input_shape = (81,)))
mlp.add(BatchNormalization())
mlp.add(Dense(256, activation = 'relu'))
mlp.add(BatchNormalization())
mlp.add(Dense(256, activation = 'relu'))
mlp.add(BatchNormalization())
mlp.add(Dense(256, activation = 'relu'))
mlp.add(BatchNormalization())
mlp.add(Dense(256, activation = 'relu'))
mlp.add(BatchNormalization())
mlp.add(Dense(81, activation = 'relu'))
mlp.summary()
mlp.compile(loss='mean_squared_error', optimizer=RMSprop(),
metrics=['accuracy'])
history = mlp.fit(quizzes[0:500000], solutions[0:500000], batch_size = 100, epochs = 3,
verbose = 1, validation_data = (quizzes[500000:600000,solutions[500000:600000]))
score = mlp.evaluate(quizzes, solutions, verbose = 1)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
In [4]:
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D, Conv1D, BatchNormalization
In [5]:
X_train = quizzes.reshape(quizzes.shape[0], 81, 1)
#X_test = X_test.reshape(X_test.shape[0], img_cols, img_rows, 1)
In [ ]:
cnn = Sequential()cnn.add(Conv1D(64, kernel_size=3, activation='relu', input_shape=(81,1)))
cnn.add(BatchNormalization())
cnn.add(Conv1D(64, kernel_size=3, activation='relu'))
cnn.add(BatchNormalization())
cnn.add(Conv1D(64, kernel_size=3, activation='relu'))
cnn.add(BatchNormalization())
cnn.add(Conv1D(32, kernel_size=3, activation='relu'))
cnn.add(BatchNormalization())
cnn.add(Conv1D(32, kernel_size=3, activation='relu'))
cnn.add(Flatten())
cnn.add(BatchNormalization())
cnn.add(Dense(81, activation = 'linear'))
#cnn.add(MaxPooling2D(pool_size=(2, 2)))
#cnn.add(Flatten())
#cnn.add(Dense(num_classes, activation='softmax'))
cnn.compile(loss='mean_squared_error',
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
history2 = cnn.fit(X_train,solutions,
batch_size=100,
epochs=3,
verbose=1,
validation_data=(X_train, solutions))
score = cnn.evaluate(X_train, solutions, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
In [53]:
mlp.save('sudokuMLP2.h5')
In [26]:
mlp.save('sudokuMLP.h5')
In [40]:
cnn.save('sudokuCNN.h5')
In [39]:
cnn2 = Sequential()
cnn2.add(Conv1D(64, kernel_size=3, activation='relu', input_shape=(81,1)))
cnn2.add(BatchNormalization())
cnn2.add(Conv1D(64, kernel_size=3, activation='relu'))
cnn2.add(BatchNormalization())
cnn2.add(Conv1D(64, kernel_size=3, activation='relu'))
cnn2.add(BatchNormalization())
cnn2.add(Conv1D(64, kernel_size=3, activation='relu'))
cnn2.add(BatchNormalization())
cnn2.add(Conv1D(64, kernel_size=3, activation='relu'))
cnn2.add(BatchNormalization())
cnn2.add(Conv1D(64, kernel_size=3, activation='relu'))
cnn2.add(BatchNormalization())
cnn2.add(Conv1D(64, kernel_size=3, activation='relu'))
cnn2.add(BatchNormalization())
cnn2.add(Conv1D(64, kernel_size=3, activation='relu'))
cnn2.add(BatchNormalization())
cnn2.add(Conv1D(64, kernel_size=3, activation='relu'))
cnn2.add(Flatten())
cnn2.add(BatchNormalization())
cnn2.add(Dense(81, activation = 'linear'))
#cnn.add(MaxPooling2D(pool_size=(2, 2)))
#cnn.add(Flatten())
#cnn.add(Dense(num_classes, activation='softmax'))
cnn2.compile(loss='mean_squared_error',
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
history2 = cnn2.fit(X_train[0:500000],solutions[0:500000],
batch_size=100,
epochs=1,
verbose=1,
validation_data=(X_train[500000:600000], solutions[500000:600000]))
score2 = cnn2.evaluate(X_train[500000:600000], solutions[500000:600000], verbose=1)
print('Test loss:', score2[0])
print('Test accuracy:', score2[1])
In [41]:
cnn2.save("deepSudokuCNN.h5")
In [60]:
testPuzzle = quizzes[-1]
#testPuzzle = np.array([1])
zeros = np.where(testPuzzle == 0)[0]
#testPuzzle = testPuzzle.reshape((1,) + testPuzzle.shape)
#edit the tensor dimensions so that it can be used for the cnn
#testPuzzle = testPuzzle.reshape(1, 81, 1)
print(testPuzzle.shape)
In [20]:
testSolution = solutions[-1]
testSolution
Out[20]:
In [ ]:
nn = load_model('deepSudokuCNN.h5')
#np.transpose(testPuzzle)
In [58]:
mlp = load_model('sudokuMLP.h5')
In [61]:
print(quizzes[-1].shape)
print(testPuzzle.shape)
In [75]:
#testPuzzle = testPuzzle.reshape((1,) + testPuzzle.shape)
print(testPuzzle.shape)
print(mlp.predict(testPuzzle))
prediction = mlp.predict(testPuzzle)
#change the type to int so that you we can evaluate the prediction
rounded = np.around(prediction)
cast = prediction.astype(int)
cast
Out[75]:
In [85]:
def solve(nn, testBoard, solution, netType):
#into our cnn
# 1:mlp, 2:1d cnn, 3:2d cnn
tensor = None
#depending on the type of net you want to predict with set the tensor dimensions
if netType == 2:
tensor = testBoard.reshape(1, 81, 1)
elif netType == 1:
#print("Reshaping the tensor for mlp")
tensor = testBoard.reshape(1,81)
#print(tensor.shape)
prediction = nn.predict(tensor)
rounded = np.around(prediction)
cast = prediction.astype(int)
correct = 0
if netType == 2 or netType == 1:
for current in range(81):
#compare the values of the cast and the solution
if cast[0][current] == solution[current]:
correct += 1
accuracy = correct / 81
print(cast)
print("The accuracy of the board was: " + str(accuracy))
In [101]:
print(quizzes[-1])
solve(nn, quizzes[-1], solutions[-1], 2)
solve(mlp, quizzes[-1], solutions[-1], 1)
print(quizzes[-1])
In [115]:
#keep going until the there are no more zeros in the input
#use the nn to predict the solution
#repredict the using the update input
def iterative(nn, testBoard, solution, netType):
zeros = np.where(testBoard == 0)[0]
while len(zeros) != 0:
if netType == 2:
tensor = testBoard.reshape(1, 81, 1)
elif netType == 1:
#print("Reshaping the tensor for mlp")
tensor = testBoard.reshape(1,81)
#print(tensor.shape)
prediction = nn.predict(tensor)
rounded = np.around(prediction)
cast = prediction.astype(int)
#update the testboard
#print(test)
#print(zeros[0])
#print(cast[0][zeros[0]])
index = zeros[0]
testBoard[index] = cast[0][index]
#remove the first element from zeros
zeros = np.delete(zeros, [0])
correct = 0
if netType == 2 or netType == 1:
for current in range(81):
#compare the values of the cast and the solution
if cast[0][current] == solution[current]:
correct += 1
accuracy = correct / 81
#print(cast)
print("The accuracy of the board was: " + str(accuracy))
In [ ]:
In [116]:
iterative(mlp, np.copy(quizzes[-1]), solutions[-1], netType = 1)
In [99]:
quizzes[-1]
Out[99]:
In [82]:
Out[82]:
In [19]:
#need 729 outputs 81 cells. 81 possible probabilities
mlp2 = Sequential()
mlp2.add(Dense(128, activation = 'relu', input_shape = (81,)))
mlp2.add(BatchNormalization())
mlp2.add(Dense(128, activation = 'relu'))
mlp2.add(BatchNormalization())
mlp2.add(Dense(128, activation = 'relu'))
mlp2.add(BatchNormalization())
mlp2.add(Dense(128, activation = 'relu'))
mlp2.add(BatchNormalization())
mlp2.add(Dense(128, activation = 'relu'))
mlp2.add(BatchNormalization())
mlp2.add(Dense(output_dim = 810, activation = 'softmax'))
mlp2.summary()
mlp2.compile(loss='categorical_crossentropy', optimizer=RMSprop(),
metrics=['accuracy'])
history = mlp2.fit(quizzes[0:500000], y_train, batch_size = 100, epochs = 3,
verbose = 1, validation_data = (quizzes[500000:600000], y_test))
score = mlp2.evaluate(quizzes[500000:600000], y_test, verbose = 1)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
In [ ]: