def findBestRegularization(s, x_sub, y_sub): regs = np.linspace(0, 10, 20) reg_acc_cv = [] reg_acc_train = [] max_acc = 0 best_reg = 0 for r in regs: th1, th2 = Train.trainSciPy2(s, x_sub, y_sub, r) acc_cv = accuracy_score(y_cv, [SimpleNN2.predictClass(s, th1, th2, w) for w in x_cv]) acc_train = accuracy_score(y_sub, [SimpleNN2.predictClass(s, th1, th2, w) for w in x_sub]) reg_acc_cv.append(acc_cv) reg_acc_train.append(acc_train) if max_acc < acc_cv: max_acc = acc_cv best_reg = r print("Validating regularization parameter [{0}]; Train accuracy: [{1}] CV accuracy: [{2}]" .format(r, acc_train, acc_cv)) print("Best reg param: {0} with accuracy on CV dataset: {1}".format(best_reg, max_acc)) plt.plot(regs, reg_acc_cv);plt.plot(regs, reg_acc_train) plt.show() return best_reg
def makeTestPerdictions(): x, _ = DataModel.loadData("..\\test.csv") s, th1, th2 = SimpleNN2.loadNetwork("..\\NeuralNetwork.bin") y = [SimpleNN2.predictClass(s, th1, th2, w) for w in x] with open("results.csv", "w") as f: imageId = 1 f.write("ImageId,Label\n") for i in y: f.write("{0},{1}\n".format(imageId, i)) imageId = imageId + 1
def test3(): (x, y) = DataModel.loadData("..\\train.csv") (x_train, x_cv, y_train, y_cv) = DataModel.splitData(x, y) x_sub = x_train[:20000,:] y_sub = y_train[:20000] s = SimpleNN2.NeuralNetConfig(784, 70, 10) regLambda = 6.84 #s = Train.trainGradientDescent(s, x_sub, y_sub, 5) th1, th2 = Train.trainSciPy2(s, x_sub, y_sub, regLambda) #th1, th2 = Train.trainGradientDescent2(s, x_sub, y_sub, 5) acc_cv = accuracy_score(y_cv, [SimpleNN2.predictClass(s, th1, th2, w) for w in x_cv]) print("Accuracy on CV set: {0}".format(acc_cv))
def trainFullAndSave(): (x, y) = DataModel.loadData("..\\train.csv") (x_train, x_cv, y_train, y_cv) = DataModel.splitData(x, y) s = SimpleNN2.NeuralNetConfig(784, 70, 10) regLambda = 6.84 print("Training neural network on full dataset") #s = Train.trainGradientDescent(s, x_sub, y_sub, 5) th1, th2 = Train.trainSciPy2(s, x_train, y_train, regLambda) #th1, th2 = Train.trainGradientDescent2(s, x_sub, y_sub, 5) print("Training complete, checking accuracy on CV data") acc_cv = accuracy_score(y_cv, [SimpleNN2.predictClass(s, th1, th2, w) for w in x_cv]) print("Accuracy on CV set: {0}".format(acc_cv)) SimpleNN2.saveNetwork(s, th1, th2, "..\\NeuralNetwork.bin")