def run_softmax_on_MNIST(temp_parameter=1): """ Trains softmax, classifies test data, computes test error, and plots cost function Runs softmax_regression on the MNIST training set and computes the test error using the test set. It uses the following values for parameters: alpha = 0.3 lambda = 1e-4 num_iterations = 150 Saves the final theta to ./theta.pkl.gz Returns: Final test error """ train_x, train_y, test_x, test_y = get_MNIST_data() theta, cost_function_history = softmax_regression(train_x, train_y, temp_parameter, alpha=0.3, lambda_factor=1.0e-4, k=10, num_iterations=150) plot_cost_function_over_time(cost_function_history) test_error = compute_test_error(test_x, test_y, theta, temp_parameter) # Save the model parameters theta obtained from calling softmax_regression to disk. write_pickle_data(theta, "./theta.pkl.gz") # TODO: add your code here for the "Using the Current Model" question in tab 4. # and print the test_error_mod3 (train_y_mod3, test_y_mod3) = update_y(train_y, test_y) test_error_mod3 = compute_test_error_mod3(test_x, test_y_mod3, theta, temp_parameter) return (test_error, test_error_mod3)
def check_test_error_mod3(): train_x, train_y, test_x, test_y = utils.get_MNIST_data() train_y_3, test_y_3 = softmax.update_y(train_y, test_y) error_rate = softmax.compute_test_error_mod3(train_x, train_y_3, theta, temp_parameter=1) print("Error rate:", error_rate)
def run_multiclass_svm_on_MNIST(): """ Trains svm, classifies test data, computes test error on test set Returns: Test error for the binary svm """ train_x, train_y, test_x, test_y = get_MNIST_data() pred_test_y = multi_class_svm(train_x, train_y, test_x) test_error = compute_test_error_svm(test_y, pred_test_y) return test_error
def run_svm_one_vs_rest_on_MNIST(C): """ Trains svm, classifies test data, computes test error on test set Returns: Test error for the binary svm """ train_x, train_y, test_x, test_y = get_MNIST_data() train_y[train_y != 0] = 1 test_y[test_y != 0] = 1 pred_test_y = one_vs_rest_svm(train_x, train_y, test_x, C) test_error = compute_test_error_svm(test_y, pred_test_y) return test_error
def run_linear_regression_on_MNIST(lambda_factor=1): """ Trains linear regression, classifies test data, computes test error on test set Returns: Final test error """ train_x, train_y, test_x, test_y = get_MNIST_data() train_x_bias = np.hstack([np.ones([train_x.shape[0], 1]), train_x]) test_x_bias = np.hstack([np.ones([test_x.shape[0], 1]), test_x]) theta = closed_form(train_x_bias, train_y, lambda_factor) test_error = compute_test_error_linear(test_x_bias, test_y, theta) return test_error
def prep_data(): # Load the dataset X_train, y_train, X_test, y_test = get_MNIST_data() # Split into train and dev dev_split_index = int(9 * len(X_train) / 10) X_dev = X_train[dev_split_index:] y_dev = y_train[dev_split_index:] X_train = X_train[:dev_split_index] y_train = y_train[:dev_split_index] permutation = np.array([i for i in range(len(X_train))]) np.random.shuffle(permutation) X_train = [X_train[i] for i in permutation] y_train = [y_train[i] for i in permutation] return X_train, y_train, X_dev, y_dev, X_test, y_test
def main(): # Load the dataset #num_classes = 10 X_train, y_train, X_test, y_test = get_MNIST_data() # We need to rehape the data back into a 1x28x28 image X_train = np.reshape(X_train, (X_train.shape[0], 1, 28, 28)) X_test = np.reshape(X_test, (X_test.shape[0], 1, 28, 28)) # Split into train and dev dev_split_index = int(9 * len(X_train) / 10) X_dev = X_train[dev_split_index:] y_dev = y_train[dev_split_index:] X_train = X_train[:dev_split_index] y_train = y_train[:dev_split_index] permutation = np.array([i for i in range(len(X_train))]) np.random.shuffle(permutation) X_train = [X_train[i] for i in permutation] y_train = [y_train[i] for i in permutation] # Split dataset into batches batch_size = 32 train_batches = batchify_data(X_train, y_train, batch_size) dev_batches = batchify_data(X_dev, y_dev, batch_size) test_batches = batchify_data(X_test, y_test, batch_size) ################################# ## Model specification TODO model = nn.Sequential(nn.Conv2d(1, 32, (3, 3)), nn.LeakyReLU(0.001), nn.MaxPool2d((2, 2)), nn.Conv2d(32, 64, (3, 3)), nn.LeakyReLU(0.001), nn.MaxPool2d((2, 2)), Flatten(), nn.Linear(1600, 128), nn.Dropout(0.5), nn.Linear(128, 10)) ################################## train_model(train_batches, dev_batches, model, nesterov=True) ## Evaluate the model on test data loss, accuracy = run_epoch(test_batches, model.eval(), None) print("Loss on test set:" + str(loss) + " Accuracy on test set: " + str(accuracy))
def run_softmax_on_MNIST_mod3(temp_parameter=1): """ Trains Softmax regression on digit (mod 3) classifications. See run_softmax_on_MNIST for more info. """ #YOUR CODE HERE train_x, train_y, test_x, test_y = get_MNIST_data() # morph y values to mod3 (train_y_mod3, test_y_mod3) = update_y(train_y, test_y) theta, cost_function_history = softmax_regression(train_x, train_y_mod3, temp_parameter, alpha=0.3, lambda_factor=1.0e-4, k=10, num_iterations=150) plot_cost_function_over_time(cost_function_history) test_error = compute_test_error(test_x, test_y_mod3, theta, temp_parameter) return test_error
def check_get_mnist(): ex_name = "Get MNIST data" train_x, train_y, test_x, test_y = utils.get_MNIST_data() log(green("PASS"), ex_name, "")
def check_get_mnist(): ex_name = "Get MNIST data" _, _, _, _ = utils.get_MNIST_data() log(green("PASS"), ex_name, "")
import numpy as np import matplotlib.pyplot as plt sys.path.append("..") from utils import get_MNIST_data, plot_images, write_pickle_data from linear_regression import closed_form, compute_test_error_linear from svm import one_vs_rest_svm, compute_test_error_svm, multi_class_svm from softmax import softmax_regression, plot_cost_function_over_time, compute_test_error, update_y, compute_test_error_mod3 from features import project_onto_PC, principal_components, reconstruct_PC, plot_PC, cubic_features # from kernel import * ####################################################################### # 1. Introduction ####################################################################### # Load MNIST data: train_x, train_y, test_x, test_y = get_MNIST_data() # Plot the first 20 images of the training set. plot_images(train_x[0:20, :]) ####################################################################### # 2. Linear Regression with Closed Form Solution ####################################################################### # TODO: first fill out functions in linear_regression.py, or the below functions will not work def run_linear_regression_on_MNIST(lambda_factor=1): """ Trains linear regression, classifies test data, computes test error on test set Returns:
def print_imagination(imagination): """ imagination: (10, 28, 28) """ imagination = imagination.astype(np.int) print('Imagination of numbers in Bayesian classifier:\n') for i, v in enumerate(imagination): print('%s:' % i) for j in range(v.shape[0]): print(v[j]) print() print('%s : Naive bayes classifier for %s\n' % (args.mode, 'discrete mode' if args.mode == 0 else 'continuous mode')) x_train, y_train, x_test, y_test = get_MNIST_data('./data') if args.mode == 0: print('\nIn discrete mode, start binnify 256 to 32') bin_pixel(x_train) bin_pixel(x_test) print('Binnify completely.') # calculate prior # -------------------------------------------------------------- # _, count_of_classes = np.unique(y_train, return_counts=True) prior = count_of_classes / x_train.shape[0] # -------------------------------------------------------------- # # calculate likelihood # -------------------------------------------------------------- #