show_plots = True save_plots = True # Load in MNIST data, process it for multiclass classification print("Loading MNIST data...") X_train, y_train = mu.load_mnist(dataset='training') y_train_true = np.asarray(y_train[:, None] == np.arange(max(y_train) + 1), dtype=int).squeeze() X_test, y_test = mu.load_mnist(dataset='testing') y_test_true = np.asarray(y_test[:, None] == np.arange(max(y_test) + 1), dtype=int).squeeze() # Solve for all principal components but do calculations using only 50 # Can reset l later if need be as all principal components are retained print("Performing PCA with k = %d components..." % k) PCA = pca.PCA(l=k, center=False) PCA.fit(X_train) X_train = PCA.transform(X_train) X_test = PCA.transform(X_test) print("Training neural network...") activators = [deep.relu, deep.linear] activators_prime = [deep.relu_prime, deep.linear_prime] y_hat, w_1, w_2, b_1, b_2, iter_arr, train_sq_loss, train_01_loss, test_sq_loss, \ test_01_loss = \ deep.neural_net(X_train, y_train_true, nodes=nodes, activators=activators, activators_prime=activators_prime, scale=scale, eps=eps, eta=eta, lam=lam, adaptive=True, batchsize=batchsize, nout=nout, nclass=nclass,
# Load in MNIST data print("Loading MNIST Training data...") X_train, y_train = mu.load_mnist(dataset='training') # Training using only one digit? if use_one_digit: mask = y_train.squeeze() == 5 X_train = X_train[mask] print(X_train.shape) y_train = y_train[mask] # Init PCA object # Solve for all principal components but do calculations using only 50 # Can reset l later if need be as all principal components are retained PCA = pca.PCA(l=50, center=True) # Fit model print("Fitting PCA model...") PCA.fit(X_train) ##################################################### # # 1.2.1: Eigenvalues and you # ##################################################### eval_list = np.asarray([1, 2, 10, 30, 50]) - 1 # my 1st eigenvalue is evals[0] for ev in eval_list: print("%d eigenvalue: %lf" % ((ev+1),PCA.evals[ev])) print("Sum of eigenvalues: %lf" % np.sum(PCA.evals))
import matplotlib as mpl import matplotlib.pyplot as plt # Flags to control functionality show_plots = True save_plots = True use_one_digit = False # Load in MNIST data print("Loading MNIST Training data...") X_train, y_train = mu.load_mnist(dataset='training') # Init PCA object # Solve for all principal components but do calculations using only 50 # Can reset l later if need be as all principal components are retained PCA = pca.PCA(l=12, center=True) # Fit model print("Fitting PCA model...") PCA.fit(X_train) ##################################################### # # 1.3.1: Eigenvectors and you: Plot first 10 # ##################################################### if show_plots: fig, axes = plt.subplots(nrows=4, ncols=4)