Beispiel #1
0
Datei: Run.py Projekt: yaselh/NN
def main():
    data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000)

    myPerceptronClassifier = Perceptron(data.trainingSet,
                                        data.validationSet,
                                        data.testSet,
                                        learningRate=0.01,
                                        epochs=10)

    # Train the classifiers
    print("=========================")
    print("Training..")

    print("\nTraining the Perceptron..")
    myPerceptronClassifier.train()
    print("Done..")


    # Do the recognizer
    perceptronPred = myPerceptronClassifier.evaluate()

    # Report the result
    print("=========================")
    evaluator = Evaluator()

    print("\nResult of the Perceptron recognizer:")
    #evaluator.printComparison(data.testSet, perceptronPred)
    evaluator.printAccuracy(data.testSet, perceptronPred)
Beispiel #2
0
def main():
    data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000)
    myStupidClassifier = StupidRecognizer(data.trainingSet,
                                          data.validationSet,
                                          data.testSet)
    myPerceptronClassifier = Perceptron(data.trainingSet,
                                        data.validationSet,
                                        data.testSet,
                                        epochs=10)

    # Train the classifiers
    print("=========================")
    print("Training..")
    myStupidClassifier.train()
    myPerceptronClassifier.train()

    # Do the recognizer
    stupidPred = myStupidClassifier.evaluate()
    perceptronPred = myPerceptronClassifier.evaluate()

    # Report the result
    print("=========================")
    evaluator = Evaluator()

    print("Result of the stupid recognizer:")
    #evaluator.printComparison(data.testSet, stupidPred)
    evaluator.printAccuracy(data.testSet, stupidPred)

    print("\nResult of the perceptron recognizer:")
    #evaluator.printComparison(data.testSet, perceptronPred)
    evaluator.printAccuracy(data.testSet, perceptronPred)
Beispiel #3
0
def main():
    data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000)

    myPerceptronClassifier = Perceptron(data.trainingSet,
                                        data.validationSet,
                                        data.testSet,
                                        learningRate=0.01,
                                        epochs=10)

    # Train the classifiers
    print("=========================")
    print("Training..")

    print("\nTraining the Perceptron..")
    myPerceptronClassifier.train()
    print("Done..")

    # Do the recognizer
    perceptronPred = myPerceptronClassifier.evaluate()

    # Report the result
    print("=========================")
    evaluator = Evaluator()

    print("\nResult of the Perceptron recognizer:")
    #evaluator.printComparison(data.testSet, perceptronPred)
    evaluator.printAccuracy(data.testSet, perceptronPred)
Beispiel #4
0
def main():
    data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000)
    myStupidClassifier = StupidRecognizer(data.trainingSet,
                                          data.validationSet,
                                          data.testSet)
    myPerceptronClassifier = Perceptron(data.trainingSet,
                                        data.validationSet,
                                        data.testSet,
                                        learningRate=0.005,
                                        epochs=30)
    myLRClassifier = LogisticRegression(data.trainingSet,
                                        data.validationSet,
                                        data.testSet,
                                        learningRate=0.005,
                                        epochs=30)

    # Train the classifiers
    print("=========================")
    print("Training..")

    print("\nStupid Classifier has been training..")
    myStupidClassifier.train()
    print("Done..")

    print("\nPerceptron has been training..")
    myPerceptronClassifier.train()
    print("Done..")

    print("\nLogistic Regression has been training..")
    myLRClassifier.train()
    print("Done..")

    # Do the recognizer
    # Explicitly specify the test set to be evaluated
    stupidPred = myStupidClassifier.evaluate()
    perceptronPred = myPerceptronClassifier.evaluate()
    lrPred = myLRClassifier.evaluate()

    # Report the result
    print("=========================")
    evaluator = Evaluator()

    print("Result of the stupid recognizer:")
    # evaluator.printComparison(data.testSet, stupidPred)
    evaluator.printAccuracy(data.testSet, stupidPred)

    print("\nResult of the Perceptron recognizer:")
    # evaluator.printComparison(data.testSet, perceptronPred)
    evaluator.printAccuracy(data.testSet, perceptronPred)
    
    print("\nResult of the Logistic Regression recognizer:")
    # evaluator.printComparison(data.testSet, perceptronPred)    
    evaluator.printAccuracy(data.testSet, lrPred)
Beispiel #5
0
def main():
    data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000, oneHot=True)
    myStupidClassifier = StupidRecognizer(data.trainingSet, data.validationSet,
                                          data.testSet)
    myPerceptronClassifier = Perceptron(data.trainingSet,
                                        data.validationSet,
                                        data.testSet,
                                        learningRate=0.005,
                                        epochs=30)

    myLRClassifier = LogisticRegression(data.trainingSet,
                                        data.validationSet,
                                        data.testSet,
                                        learningRate=0.005,
                                        epochs=30)

    # Train the classifiers
    print("=========================")
    print("Training..")

    print("\nStupid Classifier has been training..")
    myStupidClassifier.train()
    print("Done..")

    print("\nPerceptron has been training..")
    myPerceptronClassifier.train()
    print("Done..")

    print("\nLogistic Regression has been training..")
    myLRClassifier.train()
    print("Done..")

    # Do the recognizer
    # Explicitly specify the test set to be evaluated
    stupidPred = myStupidClassifier.evaluate()
    perceptronPred = myPerceptronClassifier.evaluate()
    lrPred = myLRClassifier.evaluate()

    # Report the result
    print("=========================")
    evaluator = Evaluator()

    print("Result of the stupid recognizer:")
    # evaluator.printComparison(data.testSet, stupidPred)
    evaluator.printAccuracy(data.testSet, stupidPred)

    print("\nResult of the Perceptron recognizer:")
    # evaluator.printComparison(data.testSet, perceptronPred)
    evaluator.printAccuracy(data.testSet, perceptronPred)

    print("\nResult of the Logistic Regression recognizer:")
    # evaluator.printComparison(data.testSet, perceptronPred)
    evaluator.printAccuracy(data.testSet, lrPred)
Beispiel #6
0
def main():
    data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000)
    myStupidClassifier = StupidRecognizer(data.trainingSet, data.validationSet,
                                          data.testSet)
    # Uncomment this to make your Perceptron evaluated
    myPerceptronClassifier = Perceptron(
        data.trainingSet,
        data.validationSet,
        data.testSet,
        learningRate=1.0,  #0.005,
        epochs=1  #30
    )

    # Train the classifiers
    print("=========================")
    print("Training..")

    print("\nStupid Classifier has been training..")
    myStupidClassifier.train()
    print("Done..")

    print("\nPerceptron has been training..")
    myPerceptronClassifier.train()
    print("Done..")

    # Do the recognizer
    # Explicitly specify the test set to be evaluated
    stupidPred = myStupidClassifier.evaluate()
    # Uncomment this to make your Perceptron evaluated
    perceptronPred = myPerceptronClassifier.evaluate()

    # Report the result
    print("=========================")
    evaluator = Evaluator()

    print("Result of the stupid recognizer:")
    #evaluator.printComparison(data.testSet, stupidPred)
    evaluator.printAccuracy(data.testSet, stupidPred)

    print("\nResult of the Perceptron recognizer:")
    #evaluator.printComparison(data.testSet, perceptronPred)
    # Uncomment this to make your Perceptron evaluated
    evaluator.printAccuracy(data.testSet, perceptronPred)

    evaluator.printConfusionMatrix(data.testSet, perceptronPred)
    evaluator.printClassificationResult(data.testSet, perceptronPred,
                                        ['class 0', 'class 1'])  #target_names)
Beispiel #7
0
def main():
    data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000)
    myStupidClassifier = StupidRecognizer(data.trainingSet, data.validationSet,
                                          data.testSet)

    # parameters
    learnRate = 0.005
    maxEpochs = 20
    #epochNumber = 30

    xEpochs = []
    yAccuracyPerceptron = []
    yAccuracyLogistic = []
    # loop for gathering data for graph plotting
    for epochNumber in xrange(1, maxEpochs + 1):

        myPerceptronClassifier = Perceptron(
            data.trainingSet,
            data.validationSet,
            data.testSet,
            learningRate=learnRate,  #0.005,
            epochs=epochNumber)
        # Uncomment this to run Logistic Neuron Layer
        myLRClassifier = LogisticRegression(
            data.trainingSet,
            data.validationSet,
            data.testSet,
            learningRate=learnRate,  #0.005,
            epochs=epochNumber  #30
        )

        # Train the classifiers
        print("=========================")
        print("Training..")

        print("\nStupid Classifier has been training..")
        myStupidClassifier.train()
        print("Done..")

        print("\nPerceptron has been training..")
        myPerceptronClassifier.train()
        print("Done..")

        print("\nLogistic Regression has been training..")
        myLRClassifier.train()
        print("Done..")

        # Do the recognizer
        # Explicitly specify the test set to be evaluated
        stupidPred = myStupidClassifier.evaluate()
        perceptronPred = myPerceptronClassifier.evaluate()
        lrPred = myLRClassifier.evaluate()

        # Report the result
        print("=========================")
        evaluator = Evaluator()

        print("Result of the stupid recognizer:")
        #evaluator.printComparison(data.testSet, stupidPred)
        evaluator.printAccuracy(data.testSet, stupidPred)

        print("\nResult of the Perceptron recognizer:")
        #evaluator.printComparison(data.testSet, perceptronPred)
        evaluator.printAccuracy(data.testSet, perceptronPred)

        print("\nResult of the Logistic Regression recognizer:")
        #evaluator.printComparison(data.testSet, lrPred)
        evaluator.printAccuracy(data.testSet, lrPred)

        # accumulate plotting data
        xEpochs.append(epochNumber)
        yAccuracyPerceptron.append(
            accuracy_score(data.testSet.label, perceptronPred) * 100)
        yAccuracyLogistic.append(
            accuracy_score(data.testSet.label, lrPred) * 100)

        # === end of for loop ===

    # plot the graph
    plt.plot(xEpochs, yAccuracyPerceptron, marker='o', label='Perceptron')
    plt.plot(xEpochs,
             yAccuracyLogistic,
             marker='o',
             color='r',
             label='Logistic Neuron')
    plt.xlabel('Number of epochs')
    plt.ylabel('Accuracy [%]')
    plt.title(
        'Performance on different epochs\n(using: testSet | learningRate: ' +
        str(learnRate) + ')')
    #plt.legend()
    plt.legend(loc=4)
    #plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=2, mode="expand", borderaxespad=0.)
    #plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
    plt.show()
Beispiel #8
0
def classify_one():
    data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000,
                      one_hot=True, target_digit='7')

    # NOTE:
    # Comment out the MNISTSeven instantiation above and
    # uncomment the following to work with full MNIST task
    # data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000,
    #                   one_hot=False)

    # NOTE:
    # Other 1-digit classifiers do not make sense now for comparison purpose
    # So you should comment them out, let alone the MLP training and evaluation

    # Train the classifiers #
    print("=========================")
    print("Training..")

    # Stupid Classifier
    myStupidClassifier = StupidRecognizer(data.training_set,
                                          data.validation_set,
                                          data.test_set)

    print("\nStupid Classifier has been training..")
    myStupidClassifier.train()
    print("Done..")
    # Do the recognizer
    # Explicitly specify the test set to be evaluated
    stupidPred = myStupidClassifier.evaluate()

    # Perceptron
    myPerceptronClassifier = Perceptron(data.training_set,
                                        data.validation_set,
                                        data.test_set,
                                        learning_rate=0.005,
                                        epochs=10)

    print("\nPerceptron has been training..")
    myPerceptronClassifier.train()
    print("Done..")
    # Do the recognizer
    # Explicitly specify the test set to be evaluated
    perceptronPred = myPerceptronClassifier.evaluate()

    # Logistic Regression
    myLRClassifier = LogisticRegression(data.training_set,
                                        data.validation_set,
                                        data.test_set,
                                        learning_rate=0.20,
                                        epochs=30)

    print("\nLogistic Regression has been training..")
    myLRClassifier.train()
    print("Done..")
    # Do the recognizer
    # Explicitly specify the test set to be evaluated
    lrPred = myLRClassifier.evaluate()

    # Logistic Regression
    myMLPClassifier = MultilayerPerceptron(data.training_set,
                                           data.validation_set,
                                           data.test_set,
                                           learning_rate=0.30,
                                           epochs=50)

    print("\nMultilayer Perceptron has been training..")
    myMLPClassifier.train()
    print("Done..")
    # Do the recognizer
    # Explicitly specify the test set to be evaluated
    mlpPred = myMLPClassifier.evaluate()

    # Report the result #
    print("=========================")
    evaluator = Evaluator()

    print("Result of the stupid recognizer:")
    # evaluator.printComparison(data.testSet, stupidPred)
    evaluator.printAccuracy(data.test_set, stupidPred)

    print("\nResult of the Perceptron recognizer (on test set):")
    # evaluator.printComparison(data.testSet, perceptronPred)
    evaluator.printAccuracy(data.test_set, perceptronPred)

    print("\nResult of the Logistic Regression recognizer (on test set):")
    # evaluator.printComparison(data.testSet, perceptronPred)
    evaluator.printAccuracy(data.test_set, lrPred)

    print("\nResult of the Multi-layer Perceptron recognizer (on test set):")
    # evaluator.printComparison(data.testSet, perceptronPred)
    evaluator.printAccuracy(data.test_set, mlpPred)


    # Draw
    plot = PerformancePlot("Logistic Regression")
    plot.draw_performance_epoch(myLRClassifier.performances,
                                myLRClassifier.epochs)
Beispiel #9
0
def classify_one():
    data = MNISTSeven("../data/mnist_seven.csv",
                      3000,
                      1000,
                      1000,
                      one_hot=True,
                      target_digit='7')

    # NOTE:
    # Comment out the MNISTSeven instantiation above and
    # uncomment the following to work with full MNIST task
    # data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000,
    #                   one_hot=False)

    # NOTE:
    # Other 1-digit classifiers do not make sense now for comparison purpose
    # So you should comment them out, let alone the MLP training and evaluation

    # Train the classifiers #
    print("=========================")
    print("Training..")

    # Stupid Classifier
    myStupidClassifier = StupidRecognizer(data.training_set,
                                          data.validation_set, data.test_set)

    print("\nStupid Classifier has been training..")
    myStupidClassifier.train()
    print("Done..")
    # Do the recognizer
    # Explicitly specify the test set to be evaluated
    stupidPred = myStupidClassifier.evaluate()

    # Perceptron
    myPerceptronClassifier = Perceptron(data.training_set,
                                        data.validation_set,
                                        data.test_set,
                                        learning_rate=0.005,
                                        epochs=10)

    print("\nPerceptron has been training..")
    myPerceptronClassifier.train()
    print("Done..")
    # Do the recognizer
    # Explicitly specify the test set to be evaluated
    perceptronPred = myPerceptronClassifier.evaluate()

    # Logistic Regression
    myLRClassifier = LogisticRegression(data.training_set,
                                        data.validation_set,
                                        data.test_set,
                                        learning_rate=0.20,
                                        epochs=30)

    print("\nLogistic Regression has been training..")
    myLRClassifier.train()
    print("Done..")
    # Do the recognizer
    # Explicitly specify the test set to be evaluated
    lrPred = myLRClassifier.evaluate()

    # Logistic Regression
    myMLPClassifier = MultilayerPerceptron(data.training_set,
                                           data.validation_set,
                                           data.test_set,
                                           learning_rate=0.30,
                                           epochs=50)

    print("\nMultilayer Perceptron has been training..")
    myMLPClassifier.train()
    print("Done..")
    # Do the recognizer
    # Explicitly specify the test set to be evaluated
    mlpPred = myMLPClassifier.evaluate()

    # Report the result #
    print("=========================")
    evaluator = Evaluator()

    print("Result of the stupid recognizer:")
    # evaluator.printComparison(data.testSet, stupidPred)
    evaluator.printAccuracy(data.test_set, stupidPred)

    print("\nResult of the Perceptron recognizer (on test set):")
    # evaluator.printComparison(data.testSet, perceptronPred)
    evaluator.printAccuracy(data.test_set, perceptronPred)

    print("\nResult of the Logistic Regression recognizer (on test set):")
    # evaluator.printComparison(data.testSet, perceptronPred)
    evaluator.printAccuracy(data.test_set, lrPred)

    print("\nResult of the Multi-layer Perceptron recognizer (on test set):")
    # evaluator.printComparison(data.testSet, perceptronPred)
    evaluator.printAccuracy(data.test_set, mlpPred)

    # Draw
    plot = PerformancePlot("Logistic Regression")
    plot.draw_performance_epoch(myLRClassifier.performances,
                                myLRClassifier.epochs)