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.""" 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.""" 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.""" inputArray = np.array([[0,0],[0,1],[1,0],[1,1]]) targetXOR = np.array([[0],[1],[1],[0]]) print("Initializing 1X3 Perceptron....") testP = Perceptron(2,1,3) print(str(testP) + "\n") print("\n"+"Testing learning XOR: \n") testP.train(inputArray, targetXOR) print("Training results:\n") print("Expect Close to 0: " + str(testP.test(np.array([0,0])))) print("Expect Close to 1: " + str(testP.test(np.array([0,1])))) print("Expect Close to 1: " + str(testP.test(np.array([1,0])))) print("Expect Close to 0: " + str(testP.test(np.array([1,1]))) + "\n")
def main(): """This is the driver program for showing results from part 1.""" inputArray = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) targetXOR = np.array([[0], [1], [1], [0]]) print("Initializing 1X3 Perceptron....") testP = Perceptron(2, 1, 3) print(str(testP) + "\n") print("\n" + "Testing learning XOR: \n") testP.train(inputArray, targetXOR) print("Training results:\n") print("Expect Close to 0: " + str(testP.test(np.array([0, 0])))) print("Expect Close to 1: " + str(testP.test(np.array([0, 1])))) print("Expect Close to 1: " + str(testP.test(np.array([1, 0])))) print("Expect Close to 0: " + str(testP.test(np.array([1, 1]))) + "\n")
""" from backprop import Perceptron import numpy as np if __name__ == '__main__': print("Assignment 3 : Part 1") boolInputs = np.vstack([np.array([0,0]), np.array([0,1]), np.array([1,0]), np.array([1,1])]) xorP = Perceptron(2,1,3,1) xorOutputs = np.vstack([np.array([0]), np.array([1]), np.array([1]), np.array([0])]) xorP.train(boolInputs, xorOutputs, 10000) print("\nTESTING NEURAL NET ON XOR") print("NITER = 10000; ETA = 1; HIDDEN UNITS = 3") xorTest = xorP.test(boolInputs) for i in range(len(xorTest)): print("Input = " + str(boolInputs[i]) + " Output = " + str(xorTest[i])) #andP = Perceptron(2,1,3) #andOutputs = np.vstack([np.array([0]), np.array([0]), np.array([0]),
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)
""" Stephen Rowley roc.py Plots a Receiver Operator Characteristic for a backprop perceptron network Professor Levy """ from backprop import Perceptron import numpy as np from digits import * import matplotlib.pyplot as plt #load in perceptron p = Perceptron(1,1,1,1) p.load("part2perceptron.txt") #initialize falsePosList = np.array([]) missList = np.array([]) truePosList = np.array([]) inputArray2, visualArray2, outputArray2 = readPattern("digits_test.txt") xAxis = np.arange(0,1,.05) if (outputArray2[1][2] == 0): newOutputArray = np.array([0]) if (outputArray2[1][2] == 1): newOutputArray = np.array([1]) for i in range(len(outputArray2)-1): if (outputArray2[i+1][2] == 1): newOutputArray = np.vstack([newOutputArray, 1])