def main(): """This is the driver program for showing results from part 1. Uncomment the code below if you need to pickle a new trained perceptron.""" digitsMatrices, digitsTargets = loadPatterns("digits_train.txt") digitsTests, digitsTestTargets = loadPatterns("digits_test.txt") target = np.array(()) for p in range(2500): arrayOfTen = np.array(()) beginning = np.append(np.zeros(digitsTargets[p]),np.ones(1)) arrayOfTen = np.append(beginning,np.zeros(9-digitsTargets[p])) target = np.append(target,arrayOfTen) target = np.reshape(target,(2500,10)) """trainEta = 1 #Change only these when tweaking for better results numberOfPatterns = 10000 #Change only these when tweaking for better results print("Initialized Perceptron: \n") digitsPerceptron = Perceptron(196,10,3,.1) digitsPerceptron.train(digitsMatrices,target,numberOfPatterns,trainEta) digitsPerceptron.save("part3.dat")""" newPerceptron = Perceptron(196,10,3,.1) unPickled = newPerceptron.load("part3.dat") confusion = np.zeros((10,10),"int") for k in range(2500): row = k // 250 col = np.argmax(unPickled.test(digitsTests[k])) confusion[row,col] += 1 print(confusion) """threshold = .23 #Change only these when tweaking for better results
def main(): """This is the driver program for showing results from part 1. Uncomment the code below if you need to pickle a new trained perceptron.""" twoMatrices, twoTargets = loadPatterns("digits_train.txt") twoTests, twoTestsTargets = loadPatterns("digits_test.txt") target = np.zeros(2500) target[500:750] = 1 falsePositives = 0 misses = 0 """ trainEta = 1 #Change only these when tweaking for better results numberOfPatterns = 2000 #Chang only these when tweaking for better results print("Initialized Perceptron: \n") twoPerceptron = Perceptron(196,1,3,.1) twoPerceptron.train(twoMatrices,target,numberOfPatterns,trainEta) twoPerceptron.save()""" newPerceptron = Perceptron(196,1,3,.1) unPickled = newPerceptron.load('weights.dat') threshold = .23 #Change only these when tweaking for better results #print("Expect greater than " + str(threshold) +": " + str(unPickled.test(twoTests[521]))) for i in range(len(twoTests)): test = unPickled.test(twoTests[i]) if test >= threshold and target[i] == 0: falsePositives += 1 if test < threshold and target[i] == 1: misses += 1 print("Misses: " + str(misses) + "/250") print("False Positives: " + str(falsePositives) + "/2250")
def main(): """This is the driver program for showing results from part 1. Uncomment the code below if you need to pickle a new trained perceptron.""" digitsMatrices, digitsTargets = loadPatterns("digits_train.txt") digitsTests, digitsTestTargets = loadPatterns("digits_test.txt") target = np.array(()) for p in range(2500): arrayOfTen = np.array(()) beginning = np.append(np.zeros(digitsTargets[p]), np.ones(1)) arrayOfTen = np.append(beginning, np.zeros(9 - digitsTargets[p])) target = np.append(target, arrayOfTen) target = np.reshape(target, (2500, 10)) """trainEta = 1 #Change only these when tweaking for better results numberOfPatterns = 10000 #Change only these when tweaking for better results print("Initialized Perceptron: \n") digitsPerceptron = Perceptron(196,10,3,.1) digitsPerceptron.train(digitsMatrices,target,numberOfPatterns,trainEta) digitsPerceptron.save("part3.dat")""" newPerceptron = Perceptron(196, 10, 3, .1) unPickled = newPerceptron.load("part3.dat") confusion = np.zeros((10, 10), "int") for k in range(2500): row = k // 250 col = np.argmax(unPickled.test(digitsTests[k])) confusion[row, col] += 1 print(confusion) """threshold = .23 #Change only these when tweaking for better results
def main(): """This is the driver program for showing results from part 1. Uncomment the code below if you need to pickle a new trained perceptron.""" twoMatrices, twoTargets = loadPatterns("digits_train.txt") twoTests, twoTestsTargets = loadPatterns("digits_test.txt") target = np.zeros(2500) target[500:750] = 1 falsePositives = 0 misses = 0 """ trainEta = 1 #Change only these when tweaking for better results numberOfPatterns = 2000 #Chang only these when tweaking for better results print("Initialized Perceptron: \n") twoPerceptron = Perceptron(196,1,3,.1) twoPerceptron.train(twoMatrices,target,numberOfPatterns,trainEta) twoPerceptron.save()""" newPerceptron = Perceptron(196, 1, 3, .1) unPickled = newPerceptron.load('weights.dat') threshold = .23 #Change only these when tweaking for better results #print("Expect greater than " + str(threshold) +": " + str(unPickled.test(twoTests[521]))) for i in range(len(twoTests)): test = unPickled.test(twoTests[i]) if test >= threshold and target[i] == 0: falsePositives += 1 if test < threshold and target[i] == 1: misses += 1 print("Misses: " + str(misses) + "/250") print("False Positives: " + str(falsePositives) + "/2250")
from digits import * if __name__ == '__main__': #p = Perceptron(196, 10, 20, .5) inputArray, visualArray, outputArray = readPattern("digits_train.txt") inputArray2, visualArray2, outputArray2 = readPattern("digits_test.txt") confusionMatrix = np.zeros((10,10)) print("TESTING PERCEPTRON: CLASSIFYING ALL 10 DIGITS") print("NITER = 5000; ETA = .75; HIDDEN UNITS = 20") #p.train(inputArray, outputArray, 5000, .75) #p.save("part3perceptron.txt") p = Perceptron(1,1,1,1) p.load("part3perceptron.txt") testOutput = p.test(inputArray2) for j in range(len(outputArray)): a = np.argmax(outputArray[j]) b = np.argmax(testOutput[j]) confusionMatrix[a][b] += 1 print(confusionMatrix)