コード例 #1
0
ファイル: part3.py プロジェクト: staufferl16/CompSci315
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
コード例 #2
0
ファイル: part2.py プロジェクト: staufferl16/CompSci315
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")
コード例 #3
0
ファイル: part1.py プロジェクト: staufferl16/CompSci315
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")