def main(): data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000, oneHot=False) myMLP = MultilayerPerceptron(data.trainingSet, data.validationSet, data.testSet, learningRate=0.005, epochs=30) # myMLP.fortest() myMLP.train() # Do the recognizer # Explicitly specify the test set to be evaluated MLPPred = myMLP.evaluate() # Report the result print("=========================") evaluator = Evaluator() print("\nResult of the MLP:") evaluator.printAccuracy(data.testSet, MLPPred) # Draw plot = PerformancePlot("MLP validation") plot.draw_performance_epoch(myMLP.performances, myMLP.epochs)
def main(): # ------------ NOTE -------------- # oneHot = False, as the framwork provided implements binary one-hot encoding (is it a 7 = True/False) # Our targets are one-of-k encoded (e.g. 1= (0,1,0,0,0,0,0,0,0)). Network predicts the exact number on the picture not just 7 = True/False data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000, oneHot=False) myMLPClassifier = MultilayerPerceptron( data.trainingSet, data.validationSet, data.testSet, hiddenLayerSizes=[ 65, 30 ], # size of hidden layers, input and output layers sizes are constant learningRate=0.028, # learning rate epochs=50) # epochs # Train the classifiers print("=========================") print("Training..") print("\nMLP 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:") print("\nResult of the MLP:") #evaluator.printComparison(data.testSet, lrPred) evaluator.printAccuracy(data.testSet, mlpPred) # Draw plot = PerformancePlot("MLP validation") plot.draw_performance_epoch(myMLPClassifier.performances, myMLPClassifier.epochs)
def main(): data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000, oneHot=False) myMLPClassifier = MultilayerPerceptron(data.trainingSet, data.validationSet, data.testSet, learningRate=0.05, epochs=20) #no more changes after 20 epochs #Removed old stuff # Report the result # print("=========================") evaluator = Evaluator() # Train the classifiers print("=========================") print("Training..") print("\nMLP 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("\nResult of the MLP recognizer:") #evaluator.printComparison(data.testSet, lrPred) evaluator.printAccuracy(data.testSet, mlpPred) # Draw plot = PerformancePlot("MLP validation") plot.draw_performance_epoch(myMLPClassifier.performances, myMLPClassifier.epochs)
def main(): # ------------ NOTE -------------- # oneHot does not work: It makes not sense to have binary labeled data (targetDigit or notTargetDigit), # but having a MLP with 10 output nodes and softmax function. It needs to be trained with digit labels, not binary ones! data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000, oneHot=False) myMLPClassifier = MultilayerPerceptron(data.trainingSet, data.validationSet, data.testSet, hiddenLayerSizes=[65,30], # size of hidden layers, input and output layers sizes are constant learningRate=0.028, # learning rate epochs=50) # epochs # Train the classifiers print("=========================") print("Training..") print("\nMLP 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:") print("\nResult of the MLP:") #evaluator.printComparison(data.testSet, lrPred) evaluator.printAccuracy(data.testSet, mlpPred) # Draw plot = PerformancePlot("MLP validation") plot.draw_performance_epoch(myMLPClassifier.performances, myMLPClassifier.epochs)
def main(): data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000, oneHot=False) myMLPClassifier = MultilayerPerceptron(data.trainingSet, data.validationSet, data.testSet, loss='ce', layers=[128, 150], learningRate=0.005, epochs=10) # Report the result # # Train the classifiers print("=========================") print("Training..") 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("\nResult of the Multi Layer Perceptron recognizer:") evaluator.printComparison(data.testSet, mlpPred) evaluator.printAccuracy(data.testSet, mlpPred) # Draw plot = PerformancePlot("Multi Layer Perceptron validation") plot.draw_performance_epoch(myMLPClassifier.performances, myMLPClassifier.epochs)
def main(): data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000, oneHot=False) 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) mlp = MultilayerPerceptron(data.trainingSet, data.validationSet, data.testSet, layers=None, inputWeights=None, outputTask='classification', outputActivation='softmax', loss='cee', learningRate=0.01, epochs=50) # Report the result # print("=========================") evaluator = Evaluator() # 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..") print("\nmlp has been training..") mlp.train() print("Done..") # Do the recognizer # Explicitly specify the test set to be evaluated stupidPred = myStupidClassifier.evaluate() #perceptronPred = myPerceptronClassifier.evaluate() #lrPred = myLRClassifier.evaluate() mlppred = MultilayerPerceptron.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) print("Result of the mlp:") evaluator.printAccuracy(data.testSet, mlppred) # Draw #plot = PerformancePlot("Logistic Regression validation") #plot.draw_performance_epoch(myLRClassifier.performances, #myLRClassifier.epochs) ####可能有问题 plot = PerformancePlot("mlp validation") plot.draw_performance_epoch(mlp.performances, mlp.epochs)
def main(): # 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 the autoencoder..") number_hidden_layer = 100 print(data.training_set.input.shape) myDAE = DenoisingAutoEncoder(data.training_set, data.validation_set, data.test_set, learning_rate=0.05, epochs=30, number_hidden_layer=number_hidden_layer) print("\nAutoencoder has been training..") myDAE.train() print("Done..") # Multi-layer Perceptron # NOTES: # Now take the trained weights (layer) from the Autoencoder # Feed it to be a hidden layer of the MLP, continue training (fine-tuning) # And do the classification # Correct the code here layers = [] myDAE.__del__() print(data.training_set.input.shape) layers.append(LogisticLayer(data.training_set.input.shape[1], number_hidden_layer, weights=myDAE._get_weights(), activation="sigmoid", is_classifier_layer=False)) # Output layer layers.append(LogisticLayer(number_hidden_layer, 10, None, activation="softmax", is_classifier_layer=True)) myMLPClassifier = MultilayerPerceptron(data.training_set, data.validation_set, data.test_set, layers=layers, learning_rate=0.05, epochs=30) print("\nMulti-layer 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 DAE + MLP recognizer (on test set):") # evaluator.printComparison(data.testSet, perceptronPred) evaluator.printAccuracy(data.test_set, mlpPred) # Draw plot = PerformancePlot("DAE + MLP on MNIST task") plot.draw_performance_epoch(myMLPClassifier.performances, myMLPClassifier.epochs)
def main(): # 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.005, # 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() # Multi-layer Perceptron myMLPClassifier = MultilayerPerceptron(data.training_set, data.validation_set, data.test_set, learning_rate=0.05, epochs=30) print("\nMulti-layer 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("Multi-layer Perceptron on MNIST task") plot.draw_performance_epoch(myMLPClassifier.performances, myMLPClassifier.epochs)
def main(): data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000, oneHot=False) # myLRClassifier = LogisticRegression(data.trainingSet, # data.validationSet, # data.testSet, # learningRate=0.005, # epochs=30) hidden_layers = [ LogisticLayer(128, 32, isClassifierLayer=True) for layer in range(1) ] mlp = MultilayerPerceptron(data.trainingSet, data.validationSet, data.testSet, hidden_layers, learningRate=0.005, epochs=30) # Train the classifiers #print("=========================") print("Training...") # print("\nLogistic Regression has been training..") # myLRClassifier.train() # print("Done..") print("Training MLP...") mlp.train() print("Done.") # Do the recognizer # Explicitly specify the test set to be evaluated # stupidPred = myStupidClassifier.evaluate() # perceptronPred = myPerceptronClassifier.evaluate() # lrPred = myLRClassifier.evaluate() mlpPred = mlp.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) print("Result of the MLP recognizer:") evaluator.printComparison(data.testSet, mlpPred) evaluator.printAccuracy(data.testSet, mlpPred) # Draw plot = PerformancePlot("Logistic Regression validation") # plot.draw_performance_epoch(myLRClassifier.performances, # myLRClassifier.epochs) plot.draw_performance_epoch(mlp.performances, mlp.epochs)
def main(): # 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 the autoencoder..") myDAE = DenoisingAutoEncoder(data.training_set, data.validation_set, data.test_set, learning_rate=0.05, epochs=30) print("\nAutoencoder has been training..") myDAE.train() print("Done..") # Multi-layer Perceptron # NOTES: # Now take the trained weights (layer) from the Autoencoder # Feed it to be a hidden layer of the MLP, continue training (fine-tuning) # And do the classification # Correct the code here myMLPClassifier = MultilayerPerceptron(data.training_set, data.validation_set, data.test_set, learning_rate=0.05, epochs=30) print("\nMulti-layer 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 DAE + MLP recognizer (on test set):") # evaluator.printComparison(data.testSet, perceptronPred) evaluator.printAccuracy(data.test_set, mlpPred) # Draw plot = PerformancePlot("DAE + MLP on MNIST task") plot.draw_performance_epoch(myMLPClassifier.performances, myMLPClassifier.epochs)
def main(): data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000, oneHot=False) # 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) myMLPlassifier = MultilayerPerceptron(data.trainingSet, data.validationSet, data.testSet, learningRate=0.005, epochs=30) # Report the result # print("=========================") evaluator = Evaluator() # 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..") print("\nMLP has been training..") myMLPlassifier.train() print("Done..") # Do the recognizer # Explicitly specify the test set to be evaluated # stupidPred = myStupidClassifier.evaluate() # perceptronPred = myPerceptronClassifier.evaluate() # lrPred = myLRClassifier.evaluate() mplPred = myMLPlassifier.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) print("Result of the MLP recognizer:") #evaluator.printComparison(data.testSet, stupidPred) evaluator.printAccuracy(data.testSet, mplPred) # Draw plot = PerformancePlot("MLP validation") plot.draw_performance_epoch(myMLPlassifier.performances, myMLPlassifier.epochs)
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)
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) # Report the result # print("=========================") evaluator = Evaluator() # 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..") myMLP = MultilayerPerceptron(data.trainingSet, data.validationSet, data.testSet, learningRate=0.01, epochs=30, loss="ce", outputActivation="softmax", weight_decay=0.1) print("\nMLP has been training..") myMLP.train() print("Done..") # Do the recognizer # Explicitly specify the test set to be evaluated # stupidPred = myStupidClassifier.evaluate() # perceptronPred = myPerceptronClassifier.evaluate() # lrPred = myLRClassifier.evaluate() mlpPred = myMLP.evaluate(data.validationSet) # # 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) print("\nResult of the Multilayer Perceptron recognizer:") #evaluator.printComparison(data.testSet, lrPred) # evaluator.printAccuracy(data.testSet, mlpPred) plot = PerformancePlot("MLP validation") plot.draw_performance_epoch(myMLP.performances, myMLP.epochs)
def main(): # 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 the autoencoder..") myDAE = DenoisingAutoEncoder(data.training_set, data.validation_set, data.test_set, learning_rate=dae_lr, noiseRatio=dae_nr, hiddenLayerNeurons=hiddenLayerNeurons, epochs=dae_epochs) print("\nAutoencoder has been training..") myDAE.train() print("Done..") # Multi-layer Perceptron # NOTES: # Now take the trained weights (layer) from the Autoencoder # Feed it to be a hidden layer of the MLP, continue training (fine-tuning) # And do the classification myMLPLayers = [] # First hidden layer number_of_1st_hidden_layer = hiddenLayerNeurons myMLPLayers.append(LogisticLayer(data.training_set.input.shape[1]-1, # bias "1" already added so remove one number_of_1st_hidden_layer, weights=myDAE._get_weights(), activation="sigmoid", is_classifier_layer=False)) # Output layer number_of_output_layer = 10 myMLPLayers.append(LogisticLayer(number_of_1st_hidden_layer, number_of_output_layer, None, activation="softmax", is_classifier_layer=True)) # Correct the code here myMLPClassifier = MultilayerPerceptron(data.training_set, data.validation_set, data.test_set, layers=myMLPLayers, learning_rate=mlp_lr, epochs=mlp_epochs) # remove double added bias "1" myMLPClassifier.__del__() print("\nMulti-layer 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 DAE + MLP recognizer (on test set):") # evaluator.printComparison(data.testSet, perceptronPred) evaluator.printAccuracy(data.test_set, mlpPred) os.chdir("..") # Draw plot = PerformancePlot("DAE + MLP on MNIST task on validation set") plot.draw_performance_epoch(myMLPClassifier.performances, myMLPClassifier.epochs, "plots", filename) print("drawing weights of auto encoder mlp input…") weight_plotter = WeightVisualizationPlot(myDAE.autoencMLP) weight_plotter.plot()
def main(): data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000, one_hot=False) # Train # Denoising Auto Encoder n_encoder_neurons = 100 myDAE = DenoisingAutoEncoder(data.training_set, data.validation_set, data.test_set, n_hidden_neurons=n_encoder_neurons, noise_ratio=0.2, learning_rate=0.05, epochs=5) print("Train autoencoder..") myDAE.train(verbose=True) print("Done..") # Multilayer Perceptron layers = [] # Add auto envoder hidden layer. layers.append(LogisticLayer(data.training_set.input.shape[1], n_encoder_neurons, weights=myDAE.get_weights(), cost="mse", activation="sigmoid", learning_rate=0.05)) # Add another hidden layer just like in the previous exercise. n_second_hidden_neurons = 100 layers.append(LogisticLayer(n_encoder_neurons, n_second_hidden_neurons, cost="mse", activation="sigmoid", learning_rate=0.05)) # Add output classifier layer with one neuron per digit. n_out_neurons = 10 layers.append(LogisticLayer(n_second_hidden_neurons, n_out_neurons, cost="crossentropy", activation="softmax", learning_rate=0.05)) myMLPClassifier = MultilayerPerceptron(data.training_set, data.validation_set, data.test_set, layers=layers, epochs=15) print("Train MLP..") myMLPClassifier.train(verbose=True) print("Done..") print("") # Evaluate print("Evaluate..") mlpPred = myMLPClassifier.evaluate() print("Done..") print("") print("Results:") evaluator = Evaluator() print("") # 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) # evaluator.printComparison(data.testSet, perceptronPred) evaluator.printAccuracy(data.test_set, mlpPred) # Draw plot = PerformancePlot("DAE + MLP on MNIST task") plot.draw_performance_epoch(myMLPClassifier.performances, myMLPClassifier.epochs)
def main(): # 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 the autoencoder..") myDAE = DenoisingAutoEncoder(data.training_set, learning_rate=0.05, epochs=30, decay=0.99) print("\nAutoencoder has been training..") myDAE.train() print("Done..") # Multi-layer Perceptron # NOTES: # Now take the trained weights (layer) from the Autoencoder # Feed it to be a hidden layer of the MLP, continue training (fine-tuning) # And do the classification # reload data, simpler since it was modified by the autoencoder (adding the biais) data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000, one_hot=False) # Correct the code here myMLPClassifier = MultilayerPerceptron(data.training_set, data.validation_set, data.test_set, learning_rate=0.05, decay=0.99, epochs=30, input_weights=myDAE.layers[0].weights) print("\nMulti-layer 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 DAE + MLP recognizer (on test set):") # evaluator.printComparison(data.testSet, perceptronPred) evaluator.printAccuracy(data.test_set, mlpPred) # Draw plot = PerformancePlot("DAE + MLP on MNIST task") plot.draw_performance_epoch(myMLPClassifier.performances, myMLPClassifier.epochs) plotweights = WeightVisualizationPlot(myDAE.layers[0].weights) plotweights.show()