public static (BaseModel model, double accuracy) TrainAndTest(BaseModel model) { // Global parameters string datasetPath = @"Sudoku.NeuralNetwork\Dataset\sudoku.csv.gz"; int numSudokus = 1000; // ML parameters double testPercent = 0.2; float learningRate = .001F; int batchSize = 32; int epochs = 2; Console.WriteLine("Initialize dataset"); var(sPuzzles, sSols) = DataSetHelper.ParseCSV(datasetPath, numSudokus); var(_sPuzzzlesTrain, _sPuzzlesTest) = DataSetHelper.SplitDataSet(sPuzzles, testPercent); var(_sSolsTrain, _sSolsTest) = DataSetHelper.SplitDataSet(sSols, testPercent); Console.WriteLine("Preprocess data"); var sPuzzzlesTrain = DataSetHelper.PreprocessSudokus(_sPuzzzlesTrain); var sSolsTrain = DataSetHelper.PreprocessSudokus(_sSolsTrain); var sPuzzlesTest = DataSetHelper.PreprocessSudokus(_sPuzzlesTest); var sSolsTest = DataSetHelper.PreprocessSudokus(_sSolsTest); // Add optimizer var adam = new Keras.Optimizers.Adam(learningRate); model.Compile(loss: "sparse_categorical_crossentropy", optimizer: adam); Console.WriteLine("Train model"); model.Fit(sPuzzzlesTrain, sSolsTrain, batch_size: batchSize, epochs: epochs); // Test model int correct = 0; for (int i = 0; i < 1; i++) { Console.WriteLine("Testing " + i); // Predict result var prediction = Solve(sPuzzlesTest[i], model); // Convert to sudoku var sPredict = new Core.Sudoku() { Cells = prediction.flatten().astype(np.int32).GetData <int>().ToList() }; var sSol = new Core.Sudoku() { Cells = ((sSolsTest[i] + 0.5) * 9).flatten().astype(np.int32).GetData <int>().ToList() }; // Compare sudoku var same = true; for (int j = 0; j < 9; j++) { for (int k = 0; k < 9; k++) { if (sPredict.GetCell(j, k) != sSol.GetCell(j, k)) { same = false; } } } Console.WriteLine("Prediction : \n" + sPredict); Console.WriteLine("Solution : \n" + sSol); if (same) { correct += 1; } } // Calculate accuracy var accuracy = (correct / sPuzzlesTest.size) * 100; // Return return(model, accuracy); }