def test_mnist_svm(): import os.path import numpy as np import pykitml as pk from pykitml.datasets import mnist # Download dataset if(not os.path.exists('mnist.pkl')): mnist.get() # Load mnist data set inputs_train, outputs_train, inputs_test, outputs_test = mnist.load() # Train on only first 10000 inputs_train = inputs_train[:10000] outputs_train = outputs_train[:10000] # Transform inputs using gaussian kernal sigma = 3.15 gaussian_inputs_train = pk.gaussian_kernel(inputs_train, inputs_train, sigma) gaussian_inputs_test = pk.gaussian_kernel(inputs_test, inputs_train, sigma) # Format the outputs for svm training, zeros to -1 svm_outputs_train = np.where(outputs_train==0, -1, 1) svm_outputs_test = np.where(outputs_test==0, -1, 1) # Create model svm_mnist_classifier = pk.SVM(gaussian_inputs_train.shape[1], 10) # Train the model svm_mnist_classifier.train( training_data=gaussian_inputs_train, targets=svm_outputs_train, batch_size=20, epochs=1000, optimizer=pk.Adam(learning_rate=3.5, decay_rate=0.95), testing_data=gaussian_inputs_test, testing_targets=svm_outputs_test, testing_freq=30, decay_freq=10 ) # Save it pk.save(svm_mnist_classifier, 'svm_mnist_classifier.pkl') # Print accuracy accuracy = svm_mnist_classifier.accuracy(gaussian_inputs_train, outputs_train) print('Train accuracy:', accuracy) accuracy = svm_mnist_classifier.accuracy(gaussian_inputs_test, outputs_test) print('Test accuracy:', accuracy) # Plot performance svm_mnist_classifier.plot_performance() # Plot confusion matrix svm_mnist_classifier.confusion_matrix(gaussian_inputs_test, outputs_test) # Assert if it has enough accuracy assert svm_mnist_classifier.accuracy(gaussian_inputs_train, outputs_train) >= 90
def test_adam_fashion(): import os import numpy as np import pykitml as pk from pykitml.datasets import mnist # If the dataset is not available then download it if (not os.path.exists('mnist.pkl')): mnist.get(type='fashion') # Load dataset training_data, training_targets, testing_data, testing_targets = mnist.load( ) # Create a new neural network fashion_classifier = pk.NeuralNetwork([784, 100, 10]) # Train it fashion_classifier.train(training_data=training_data, targets=training_targets, batch_size=50, epochs=1200, optimizer=pk.Adam(learning_rate=0.012, decay_rate=0.95), testing_data=testing_data, testing_targets=testing_targets, testing_freq=30, decay_freq=10) # Save it pk.save(fashion_classifier, 'fashion_classifier_network.pkl') # Show performance accuracy = fashion_classifier.accuracy(training_data, training_targets) print('Train Accuracy:', accuracy) accuracy = fashion_classifier.accuracy(testing_data, testing_targets) print('Test Accuracy:', accuracy) # Plot performance fashion_classifier.plot_performance() # Show confusion matrix fashion_classifier.confusion_matrix(training_data, training_targets, gnames=[ 'T-shirt/Top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle Boot' ]) # Assert if it has enough accuracy assert fashion_classifier.accuracy(training_data, training_targets) > 84
def test_search(): import os import numpy as np import pykitml as pk from pykitml.datasets import mnist # If the dataset is not available then download it if (not os.path.exists('mnist.pkl')): mnist.get(type='fashion') # Load dataset training_data, training_targets, testing_data, testing_targets = mnist.load( ) # Search for hyperparameters # Learning rate alpha = 10^-4 to 10^-2 # Decay rate = 0.8 to 1 # Decay frequency = 10 to 30 # Batch size = 10 to 100 search = pk.RandomSearch() for alpha, decay, decay_freq, bsize in search.search( 10, 3, 5, [-4, -2, 'log'], [0.8, 1, 'float'], [10, 30, 'int'], [10, 100, 'int']): # Create a new neural network fashion_classifier = pk.NeuralNetwork([784, 100, 10]) # Train it fashion_classifier.train(training_data=training_data, targets=training_targets, batch_size=bsize, epochs=1200, optimizer=pk.Adam(learning_rate=alpha, decay_rate=decay), testing_freq=100, decay_freq=decay_freq) cost = fashion_classifier.cost(testing_data, testing_targets) search.set_cost(cost) # Save the best model if (search.best): pk.save(fashion_classifier, 'best.pkl') # Load the best model fashion_classifier = pk.load('best.pkl') # Show performance accuracy = fashion_classifier.accuracy(testing_data, testing_targets) print('Test Accuracy:', accuracy) # Assert accuracy assert accuracy > 84
def test_pca_compression(): import os.path import random import numpy as np import matplotlib.pyplot as plt import pykitml as pk from pykitml.datasets import mnist # Download dataset if(not os.path.exists('mnist.pkl')): mnist.get() # Load dataset training_data, training_targets, testing_data, testing_targets = mnist.load() # Train PCA, reduce 784 dimensions to 250 dimensions pca = pk.PCA(training_data, 250) print('Variance retention:', pca.retention) # Pick random datapoints indices = random.sample(range(1, 1000), 16) examples = training_data[indices] # Show the original images plt.figure('Original', figsize=(10, 7)) for i in range(1, 17): plt.subplot(4, 4, i) plt.imshow(examples[i-1].reshape((28, 28)), cmap='gray') # Transform the example and compress transformed_examples = pca.transform(examples) # Inverse transform and recover the examples recovered_examples = pca.inverse_transform(transformed_examples) # Show the inverse transformed examples plt.figure('Recovered', figsize=(10, 7)) for i in range(1, 17): plt.subplot(4, 4, i) plt.imshow(recovered_examples[i-1].reshape((28, 28)), cmap='gray') # Show results plt.show()
def test_download(): from pykitml.datasets import mnist # Download the mnist data set mnist.get(type='fashion') # Test ran successfully assert True