def finalTest(size_training, size_test, hidden_layers, lambd, num_iterations): print "\nBeginning of the finalTest... \n" images_training, labels_training, images_test, labels_test = read_dataset(size_training, size_test) # Setup the parameters you will use for this exercise input_layer_size = 784 # 28x28 Input Images of Digits num_labels = 10 # 10 labels, from 0 to 9 (one label for each digit) layers = [input_layer_size] + hidden_layers + [num_labels] num_of_hidden_layers = len(hidden_layers) # Fill the randInitializeWeights.py in order to initialize the neural network weights. Theta = randInitializeWeights(layers) # Unroll parameters nn_weights = unroll_params(Theta) res = fmin_l_bfgs_b(costFunction, nn_weights, fprime=backwards, args=(layers, images_training, labels_training, num_labels, lambd), maxfun = num_iterations, factr = 1., disp = True) Theta = roll_params(res[0], layers) print "\nTesting Neural Network... \n" pred_training = predict(Theta, images_training) print '\nAccuracy on training set: ' + str(mean(labels_training == pred_training) * 100) pred = predict(Theta, images_test) print '\nAccuracy on test set: ' + str(mean(labels_test == pred) * 100) # Display the images where the algorithm got wrong temp = (labels_test == pred) indexes_false = [] for i in range(size_test): if temp[i] == 0: indexes_false.append(i) displayData(images_training[indexes_false, :])
def main(): # Get data data = loadmat('ex4data1.mat') X = data['X'] y = data['y'] y_cat = to_categorical(y) # Categoriza los datos y_cat = y_cat[:, 1:] # Se busca que el 1 esté en la primera posición y el 0 en la última # Show data sample = np.random.choice(X.shape[0], 100) fig, ax = displayData(X[sample, :]) fig.savefig('numeros.png') X = np.hstack([np.ones((len(X), 1)), X]) # Le añade una columna de unos a las x # Get thetas weights = loadmat('ex4weights.mat') theta1, theta2 = weights['Theta1'], weights['Theta2'] # Theta1 es de dimensión 25x401 # Theta2 es de dimensión 10x26 params_rn = np.concatenate((theta1.ravel(), theta2.ravel())) l = 1 #cost, grad = backprop(params_rn, len(X[0]) - 1, len(theta1), len(theta2), X, y_cat, l) #print(cost) #checkNNGradients(backprop, 0) theta1, theta2 = min_coste( len(X[0]) - 1, len(theta1), len(theta2), X, y_cat, l) evaluar(getH(X, theta1, theta2), y)
def display100(file): """Randomly pick 100 images from a file and displays them in a nice grid.""" # Load Training Data print('Loading and Visualizing Data ...') data = scipy.io.loadmat(file) # training data stored in arrays X, y X = data['X'] y = data['y'] np.savetxt("y.csv", y) m, _ = X.shape print(y.shape) np.savetxt("newy.csv", y) # Randomly select 100 data points to display rand_indices = np.random.permutation(range(m)) sel = X[rand_indices[0:100], :] displayData(sel)
def main(): data = loadmat("ex4data1.mat") y = data["y"].ravel() X = data["X"] num_entradas = X.shape[1] num_ocultas = 25 num_etiquetas = 10 # Transforma Y en una matriz de vectores, donde cada vector está formado por todo # 0s excepto el valor marcado en Y, que se pone a 1 # 3 ---> [0, 0, 0, 1, 0, 0, 0, 0, 0, 0] lenY = len(y) y = (y - 1) y_onehot = np.zeros((lenY, num_etiquetas)) for i in range(lenY): y_onehot[i][y[i]] = 1 # Crea una X nueva con 100 valores aleatorios de X X_show = np.zeros((100, X.shape[1])) for i in range(100): random = np.random.randint(low=0, high=X.shape[0]) X_show[i] = X[random] # Muestra por pantalla algunos ejemplos formados por la nueva X displayData(X_show) plt.show() # Lectura de los pesos del archivo weights = loadmat("ex4weights.mat") theta1 = weights["Theta1"] # (25, 401) theta2 = weights["Theta2"] # (10, 26) # Concatenación de las matrices de pesos en un solo vector thetaVec = np.concatenate((np.ravel(theta1), np.ravel(theta2))) # Cálculo del coste backprop(thetaVec, X.shape[1], num_ocultas, num_etiquetas, X, y_onehot, 1)
# This is the same datset that we used in the **ex3** exericse. There are 5000 training examples in **ex3data1.mat**. Each of these training is a single row in our data matrix **X**. This give us 5000 by 400 matrix X where every row is a training example for a handwritten digit image. # # $$X = # \begin{bmatrix} # \big( x^{(1)} \big)^{T} \\ # \big( x^{(2)} \big)^{T} \\ # \vdots \\ # \big( x^{(m)} \big)^{T} # \end{bmatrix} # $$ # In[4]: # Randomly select 100 data points to display sel = np.random.permutation(m) displayData(X[sel[:100], :]) # ### 1.2 Model representation # Our network is shown in figure below: # # ![](fig/NN_2layer.png) # We have been provided with a set of network parameters $\big( \Theta^{(1)}, \Theta^{(2)} \big)$ already trained by us. These are stored in **ex4weights.mat** file and will be loaded by **loadmat** function of Scipy library. The parameters have dimensions that are sized for a neural network with 25 units in the second layer and 10 output units (corresponding to the 10 digit classes). # In[5]: ## ====================== Part 2: Loading Parameters ====================== # In this part of the exercises, we load some pre-initialized # neural network parameters. print('\nLoading saved Neural Network Parameters ...\n')
# ================================ Step 1: Loading and Visualizing Data ================================ print("\nLoading and visualizing Data ...\n") #Reading of the dataset # You are free to reduce the number of samples retained for training, in order to reduce the computational cost size_training = 60000 # number of samples retained for training size_test = 10000 # number of samples retained for testing images_training, labels_training, images_test, labels_test = read_dataset(size_training, size_test) # Randomly select 100 data points to display random_instances = list(range(size_training)) random.shuffle(random_instances) displayData(images_training[random_instances[0:100],:]) input('Program paused. Press enter to continue!!!') # ================================ Step 2: Setting up Neural Network Structure & Initialize NN Parameters ================================ print("\nSetting up Neural Network Structure ...\n") # Setup the parameters you will use for this exercise input_layer_size = 784 # 28x28 Input Images of Digits num_labels = 10 # 10 labels, from 0 to 9 (one label for each digit) num_of_hidden_layers = int(input('Please select the number of hidden layers: ')) print("\n") layers = [input_layer_size] for i in range(num_of_hidden_layers):
def ex3_nn(): ## Machine Learning Online Class - Exercise 3 | Part 2: Neural Networks # Instructions # ------------ # # This file contains code that helps you get started on the # linear exercise. You will need to complete the following functions # in this exericse: # # lrCostFunction.m (logistic regression cost function) # oneVsAll.m # predictOneVsAll.m # predict.m # # For this exercise, you will not need to change any code in this file, # or any other files other than those mentioned above. # ## Initialization #clear ; close all; clc ## Setup the parameters you will use for this exercise input_layer_size = 400 # 20x20 Input Images of Digits hidden_layer_size = 25 # 25 hidden units num_labels = 10 # 10 labels, from 1 to 10 # (note that we have mapped "0" to label 10) ## =========== Part 1: Loading and Visualizing Data ============= # We start the exercise by first loading and visualizing the dataset. # You will be working with a dataset that contains handwritten digits. # # Load Training Data print('Loading and Visualizing Data ...') mat = scipy.io.loadmat('ex3data1.mat') X = mat['X'] y = mat['y'].ravel() m = X.shape[0] # Randomly select 100 data points to display sel = np.random.choice(m, 100, replace=False) displayData(X[sel, :]) plt.savefig('figure2.png') print('Program paused. Press enter to continue.') #pause; ## ================ Part 2: Loading Pameters ================ # In this part of the exercise, we load some pre-initialized # neural network parameters. print('\nLoading Saved Neural Network Parameters ...') # Load the weights into variables Theta1 and Theta2 mat = scipy.io.loadmat('ex3weights.mat') Theta1 = mat['Theta1'] Theta2 = mat['Theta2'] ## ================= Part 3: Implement Predict ================= # After training the neural network, we would like to use it to predict # the labels. You will now implement the "predict" function to use the # neural network to predict the labels of the training set. This lets # you compute the training set accuracy. pred = predict(Theta1, Theta2, X) print('\nTraining Set Accuracy: %f' % (np.mean((pred == y).astype(int)) * 100)) print('Program paused. Press enter to continue.\n') #pause; # To give you an idea of the network's output, you can also run # through the examples one at the a time to see what it is predicting. # Randomly permute examples rp = np.random.choice(m, 100, replace=False) for i in range(m): # Display print('\nDisplaying Example Image') displayData(X[rp[i], :][None]) plt.savefig('figure3_%d.png' % i) pred = predict(Theta1, Theta2, X[rp[i],:]) print('\nNeural Network Prediction: %d (digit %d)' % (pred, pred % 10)) # Pause print('Program paused. Press enter to continue.') input()
# ==================== 1. Multi-class Classification ==================== # Load saved matrices from file input_layer_size = 400 # 20x20 Input Images of Digits num_labels = 10 # 10 labels, from 1 to 10 data = sio.loadmat('ex3data1.mat') X = data['X'] y = data['y'].ravel() m, n = X.shape # =================== 1.2 Visualizing the data =========================== # Randomly select 100 data points to display rand_indices = np.random.permutation(m) sel = X[rand_indices[0:100], :] plt.figure() displayData(sel, padding=1) plt.show() # ===================== 1.3 Vectorizing logistic regression ============== theta_t = np.array([-2, -1, 1, 2]) X_t = np.hstack((np.ones( (5, 1)), np.arange(1, 16).reshape(5, 3, order='F') / 10.0)) y_t = np.array([1, 0, 1, 0, 1]) lambda_t = 3 cost, grad = lrCostFunction(theta_t, X_t, y_t, lambda_t) print('Cost:', cost) print('Expected cost: 2.534819') print('Gradients: \n', grad) print('Expected gradients: \n [ 0.146561 -0.548558 0.724722 1.398003 ]')
print 'Program paused. Press enter to continue.' raw_input() ## =============== Part 4: Loading and Visualizing Face Data ============= # We start the exercise by first loading and visualizing the dataset. # The following code will load the dataset into your environment # print '\nLoading face dataset.\n' # Load Face dataset X = loadmat('ex7faces.mat')['X'] # Display the first 100 faces in the dataset fig = figure() displayData(X[:100, :]) fig.show() print 'Program paused. Press enter to continue.' raw_input() ## =========== Part 5: PCA on Face Data: Eigenfaces =================== # Run PCA and visualize the eigenvectors which are in this case eigenfaces # We display the first 36 eigenfaces. # print '\nRunning PCA on face dataset.' print '(this might take a minute or two ...)\n' # Before running PCA, it is important to first normalize X by subtracting # the mean value from each feature X_norm, mu, sigma = featureNormalize(X);
test_set = sio.loadmat('test_set.mat') th_set = sio.loadmat('weights.mat') X_test = test_set['X'] y_test = np.int64(test_set['y']) th1 = th_set['Theta1'] th2 = th_set['Theta2'] m = X_test.shape[0] def random(m): return np.random.permutation(m) displayData(np.random.choice(X_test, 100, replace=False)) pre = predict(X_test, th1, th2) y_test.ravel() accuracy = np.mean(np.double(pre == y_test.ravel())) print('Accuracy: ' + str(accuracy * 100) + '%%') rp = random(m) plt.figure() for i in range(5): X2 = X_test[rp[i], :] X2 = np.matrix(X_test[rp[i]]) pred = predict(X2.getA(), th1, th2) pred = np.squeeze(pred) pred_str = 'Neural Network Prediction: %d (digit %d)' % (pred,
def main(): ''' Main function ''' ## %% =========== Part 1: Loading and Visualizing Data ============= #% We start the exercise by first loading and visualizing the dataset. #% You will be working with a dataset that contains handwritten digits. #% # Read the Matlab data m, n, X, y = getMatlabTrainingData() # number of features input_layer_size = n # Select some random images from X print('Selecting random examples of the data to display.\n') sel = np.random.permutation(m) sel = sel[0:100] # Re-work the data orientation of each training example image_size = 20 XMatlab = np.copy(X) # Need a deep copy, not just the reference for i in range(m): XMatlab[i, :] = XMatlab[i, :].reshape(image_size, image_size).transpose().reshape(1, image_size*image_size) # display the sample images displayData(XMatlab[sel, :]) # Print Out the labels for what is being seen. print('These are the labels for the data ...\n') print(y[sel, :].reshape(10, 10)) # Pause program print("Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") #%% ================ Part 2: Loading Parameters ================ #% In this part of the exercise, we load some pre-initialized # % neural network parameters. print('\nLoading Saved Neural Network Parameters ...\n') # Load the weights into variables Theta1 and Theta2 import scipy .io as sio fnWeights = '/home/jennym/Kaggle/DigitRecognizer/ex4/ex4weights.mat' weights = sio.loadmat(fnWeights) Theta1 = weights['Theta1'] Theta2 = weights['Theta2'] #% Unroll parameters nn_params = np.hstack((Theta1.ravel(order='F'), Theta2.ravel(order='F'))) #%% ================ Part 3: Compute Cost (Feedforward) ================ #% To the neural network, you should first start by implementing the #% feedforward part of the neural network that returns the cost only. You #% should complete the code in nnCostFunction.m to return cost. After #% implementing the feedforward to compute the cost, you can verify that #% your implementation is correct by verifying that you get the same cost #% as us for the fixed debugging parameters. #% #% We suggest implementing the feedforward cost *without* regularization #% first so that it will be easier for you to debug. Later, in part 4, you #% will get to implement the regularized cost. #% print('\nFeedforward Using Neural Network ...\n') #% Weight regularization parameter (we set this to 0 here). MLlambda = 0.0 # Cluge, put y back to matlab version, then adjust to use python # indexing later into y_matrix y[(y == 0)] = 10 y = y - 1 J, _ = nnCostFunction(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, MLlambda) print('Cost at parameters (loaded from ex4weights): ' + str(J) + '\n (this value should be about 0.287629)\n') # Pause print("Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") #%% =============== Part 4: Implement Regularization =============== #% Once your cost function implementation is correct, you should now #% continue to implement the regularization with the cost. #% print('\nChecking Cost Function (with Regularization) ... \n') # % Weight regularization parameter (we set this to 1 here). MLlambda = 1.0 J, _ = nnCostFunction(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, MLlambda) print('Cost at parameters (loaded from ex4weights): ' + str(J) + '\n(this value should be about 0.383770)\n'); # Pause print("Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") #%% ================ Part 5: Sigmoid Gradient ================ #% Before you start implementing the neural network, you will first #% implement the gradient for the sigmoid function. You should complete the #% code in the sigmoidGradient.m file. #% print('\nEvaluating sigmoid gradient...\n') g = sigmoidGradient(np.array([1, -0.5, 0, 0.5, 1])) print('Sigmoid gradient evaluated at [1 -0.5 0 0.5 1]:\n ') print(g) print('\n\n') # Pause print("Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") #%% ================ Part 6: Initializing Parameters ================ #% In this part of the exercise, you will be starting to implement a two #% layer neural network that classifies digits. You will start by #% implementing a function to initialize the weights of the neural network #% (randInitializeWeights.m) print('\nInitializing Neural Network Parameters ...\n') initial_Theta1 = randInitializeWeights(input_layer_size, hidden_layer_size) initial_Theta2 = randInitializeWeights(hidden_layer_size, num_labels) #% Unroll parameters initial_nn_params = np.hstack(( initial_Theta1.ravel(order = 'F'), initial_Theta2.ravel(order = 'F'))) # Pause print("Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") #%% =============== Part 7: Implement Backpropagation =============== #% Once your cost matches up with ours, you should proceed to implement the #% backpropagation algorithm for the neural network. You should add to the #% code you've written in nnCostFunction.m to return the partial #% derivatives of the parameters. #% print('\nChecking Backpropagation... \n') #% Check gradients by running checkNNGradients checkNNGradients() # Pause print("Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") #%% =============== Part 8: Implement Regularization =============== #% Once your backpropagation implementation is correct, you should now #% continue to implement the regularization with the cost and gradient. #% print('\nChecking Backpropagation (w/ Regularization) ... \n') #% Check gradients by running checkNNGradients MLlambda = 3 checkNNGradients(MLlambda) # Pause print("Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") #% Also output the costFunction debugging values debug_J, _ = nnCostFunction(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, MLlambda) print('\n\n Cost at (fixed) debugging parameters (w/ lambda = ' + '{0}): {1}'.format(MLlambda, debug_J)) print('\n (this value should be about 0.576051)\n\n') # Pause print("Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") #%% =================== Part 8b: Training NN =================== #% You have now implemented all the code necessary to train a neural #% network. To train your neural network, we will now use "fmincg", which #% is a function which works similarly to "fminunc". Recall that these #% advanced optimizers are able to train our cost functions efficiently as #% long as we provide them with the gradient computations. #% print ('\nTraining Neural Network... \n') #% After you have completed the assignment, change the MaxIter to a larger #% value to see how more training helps. #% jkm change maxIter from 50-> 400 options = {'maxiter': MAXITER} #% You should also try different values of lambda MLlambda = 1 #% Create "short hand" for the cost function to be minimized costFunc = lambda p: nnCostFunction(p, input_layer_size, hidden_layer_size, num_labels, X, y, MLlambda) #% Now, costFunction is a function that takes in only one argument (the #% neural network parameters) ''' NOTES: Call scipy optimize minimize function method : str or callable, optional Type of solver. CG -> Minimization of scalar function of one or more variables using the conjugate gradient algorithm. jac : bool or callable, optional Jacobian (gradient) of objective function. Only for CG, BFGS, Newton-CG, L-BFGS-B, TNC, SLSQP, dogleg, trust-ncg. If jac is a Boolean and is True, fun is assumed to return the gradient along with the objective function. If False, the gradient will be estimated numerically. jac can also be a callable returning the gradient of the objective. In this case, it must accept the same arguments as fun. callback : callable, optional. Called after each iteration, as callback(xk), where xk is the current parameter vector. ''' # Setup a callback for displaying the cost at the end of each iteration class Callback(object): def __init__(self): self.it = 0 def __call__(self, p): self.it += 1 print "Iteration %5d | Cost: %e" % (self.it, costFunc(p)[0]) result = sci.minimize(costFunc, initial_nn_params, method='CG', jac=True, options=options, callback=Callback()) nn_params = result.x cost = result.fun # matlab: [nn_params, cost] = fmincg(costFunction, initial_nn_params, options); #% Obtain Theta1 and Theta2 back from nn_params Theta1 = np.reshape(nn_params[:hidden_layer_size * (input_layer_size + 1)], (hidden_layer_size, (input_layer_size + 1)), order = 'F') Theta2 = np.reshape(nn_params[hidden_layer_size * (input_layer_size + 1):], (num_labels, (hidden_layer_size + 1)), order = 'F') # Pause print("Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") #%% ================= Part 9: Visualize Weights ================= #% You can now "visualize" what the neural network is learning by #% displaying the hidden units to see what features they are capturing in #% the data.# print('\nVisualizing Neural Network... \n') displayData(Theta1[:, 1:]) # Pause print("Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") #%% ================= Part 10: Implement Predict ================= #% After training the neural network, we would like to use it to predict #% the labels. You will now implement the "predict" function to use the #% neural network to predict the labels of the training set. This lets #% you compute the training set accuracy. pred = predict(Theta1, Theta2, X) # JKM - my array was column stacked - don't understand why this works pp = np.row_stack(pred) accuracy = np.mean(np.double(pp == y)) * 100 print('\nTraining Set Accuracy: {0} \n'.format(accuracy)) # Pause print("Program paused. Press Ctrl-D to continue.\n") code.interact(local=dict(globals(), **locals())) print(" ... continuing\n ") # ======================================== # All Done! return
# We start the exercise by first loading and visualizing the dataset. # You will be working with a dataset that contains handwritten digits. # Load Training Data print('Loading and Visualizing Data ...') mat = scipy.io.loadmat('ex4data1.mat') X, y = mat['X'], mat['y'] m = X.shape[0] # Randomly select 100 data points to display # Randomly select 100 data points to display rand_indices = np.random.permutation(m) sel = X[rand_indices[0:100], :] dd.displayData(sel) input('Program paused. Press enter to continue.') # ## ================ Part 2: Loading Parameters ================ # In this part of the exercise, we load some pre-initialized # neural network parameters. print('\nLoading Saved Neural Network Parameters ...') # Load the weights into variables Theta1 and Theta2 mat = scipy.io.loadmat('ex4weights.mat') Theta1 = mat['Theta1'] Theta2 = mat['Theta2'] # Unroll parameters nn_params = [Theta1, Theta2]
print "\nLoading and visualizing Data ...\n" #Reading of the dataset # You are free to reduce the number of samples retained for training, in order to reduce the computational cost # TODO: change this #size_training = 60000 # number of samples retained for training size_training = 5000 # number of samples retained for training #size_test = 10000 # number of samples retained for testing size_test = 5000 # number of samples retained for testing images_training, labels_training, images_test, labels_test = read_dataset(size_training, size_test) # Randomly select 100 data points to display random_instances = range(size_training) random.shuffle(random_instances) displayData(images_training[random_instances[0:100],:]) raw_input('Program paused. Press enter to continue!!!') # ================================ Step 2: Setting up Neural Network Structure & Initialize NN Parameters ================================ print "\nSetting up Neural Network Structure ...\n" # Setup the parameters you will use for this exercise input_layer_size = 784 # 28x28 Input Images of Digits num_labels = 10 # 10 labels, from 0 to 9 (one label for each digit) num_of_hidden_layers = int(raw_input('Please select the number of hidden layers: ')) print "\n" layers = [input_layer_size] for i in range(num_of_hidden_layers):
You will be working with a dataset that contains handwritten digits. """ # Load Training Data print('Loading and Visualizing Data ...\n') X = np.loadtxt('ex4_features.csv', delimiter =",") m,_ = X.shape y = np.loadtxt('ex4_labels.csv', delimiter =",").reshape(m, 1) #Randomly select 100 data points to display rand_indices = np.random.choice(m, 100) displayData(X[rand_indices]) plt.draw() plt.show(block=False) print('Program paused. Press enter to continue.\n') pause() """## Part 2: Loading Pameters ================ In this part of the exercise, we load some pre-initialized neural network parameters.""" print('\nLoading Saved Neural Network Parameters ...\n') # Load the weights into variables Theta1 and Theta2
X = mat["X"] y = mat["y"] m = X.shape[0] # crucial step in getting good performance! # changes the dimension from (m,1) to (m,) # otherwise the minimization isn't very effective... y=y.flatten() # Randomly select 100 data points to display rand_indices = np.random.permutation(m) sel = X[rand_indices[:100],:] dd.displayData(sel) raw_input('Program paused. Press enter to continue.\n') ## ============ Part 2: Vectorize Logistic Regression ============ # In this part of the exercise, you will reuse your logistic regression # code from the last exercise. You task here is to make sure that your # regularized logistic regression implementation is vectorized. After # that, you will implement one-vs-all classification for the handwritten # digit dataset. # print('Training One-vs-All Logistic Regression...') lambda_reg = 0.1 all_theta = ova.oneVsAll(X, y, num_labels, lambda_reg)
# You will be working with a dataset that contains handwritten digits. # # Load Training Data print 'Loading and Visualizing Data ...' ex3data1 = loadmat('ex3data1.mat') # training data stored in arrays X, y X = ex3data1['X'] y = ex3data1['y'].ravel() m = size(X, 0) # Randomly select 100 data points to display sel = random.permutation(m) fig = figure() displayData(X[sel[:100], :]) fig.show() print 'Program paused. Press enter to continue.' raw_input() ## ============ Part 2: Vectorize Logistic Regression ============ # In this part of the exercise, you will reuse your logistic regression # code from the last exercise. You task here is to make sure that your # regularized logistic regression implementation is vectorized. After # that, you will implement one-vs-all classification for the handwritten # digit dataset. # print '\nTraining One-vs-All Logistic Regression...'
# =============== Part 4: Loading and Visualizing Face Data ============= # We start the exercise by first loading and visualizing the dataset. # The following code will load the dataset into your environment # print('\nLoading face dataset.\n\n') # Load Face dataset #load ('ex7faces.mat') data = sio.loadmat(ml_dir+'ex7faces.mat') # % training data stored in arrays X, y X = data['X'] # Display the first 100 faces in the dataset displayData(X[0:100, :]) # =========== Part 5: PCA on Face Data: Eigenfaces =================== # Run PCA and visualize the eigenvectors which are in this case eigenfaces # We display the first 36 eigenfaces. # print('\nRunning PCA on face dataset.This mght take a minute or two ...)\n\n') # Before running PCA, it is important to first normalize X by subtracting # the mean value from each feature [X_norm, mu, sigma] = featureNormalize(X) # Run PCA
pause() """ ## Part 4: Loading and Visualizing Face Data We start the exercise by first loading and visualizing the dataset. The following code will load the dataset into your environment """ print('\nLoading face dataset.\n\n') # Load Face dataset mat_contents = sio.loadmat('ex7faces.mat') X = mat_contents['X'] # Display the first 10x10 faces in the dataset displayData(X) plt.draw() plt.show(block=False) print('Program paused. Press enter to continue.\n') pause() """"## Part 5: PCA on Face Data: Eigenfaces Run PCA and visualize the eigenvectors which are in this case eigenfaces We display the first 36 eigenfaces. """ print('\nRunning PCA on face dataset.(this might take a minute or two ...)\n\n') # Before running PCA, it is important to first normalize X by subtracting # the mean value from each feature [X_norm, mu, sigma] = featureNormalize(X)
# You will be working with a dataset that contains handwritten digits. # # Load Training Data print('Loading and Visualizing Data ...') data = scipy.io.loadmat('ex3data1.mat') X = data['X'] y = data['y'] m, _ = X.shape # Randomly select 100 data points to display rand_indices = np.random.permutation(range(m)) sel = X[rand_indices[0:100], :] displayData(sel) input('Program paused. Press <Enter> to continue...') ## ================ Part 2: Loading Pameters ================ # In this part of the exercise, we load some pre-initialized # neural network parameters. print('Loading Saved Neural Network Parameters ...') # Load the weights into variables Theta1 and Theta2 data = scipy.io.loadmat('ex3weights.mat') Theta1 = data['Theta1'] Theta2 = data['Theta2'] ## ================= Part 3: Implement Predict =================
# You will be working with a dataset that contains handwritten digits. # # Load Training Data print 'Loading and Visualizing Data ...' ex3data1 = loadmat('ex3data1.mat') X = ex3data1['X'] y = ex3data1['y'].ravel() m = size(X, 0) # Randomly select 100 data points to display sel = random.permutation(m) fig = figure() displayData(X[sel[:100], :]) fig.show() print 'Program paused. Press enter to continue.' raw_input() ## ================ Part 2: Loading Pameters ================ # In this part of the exercise, we load some pre-initialized # neural network parameters. print '\nLoading Saved Neural Network Parameters ...' # Load the weights into variables Theta1 and Theta2 ex3weights = loadmat('ex3weights.mat') Theta1 = ex3weights['Theta1'] Theta2 = ex3weights['Theta2']
# Load Training Data print('Loading and Visualizing Data ...') data = scipy.io.loadmat('ex3data1.mat') # training data stored in arrays X, y X = data['X'] y = data['y'] m, _ = X.shape print("X shape", X.shape) print("y shape", y.shape) # Randomly select 100 data points to display rand_indices = np.random.permutation(range(m)) sel = X[rand_indices[0:100], :] displayData(sel) input("Program paused. Press Enter to continue...") ## ============ Part 2: Vectorize Logistic Regression ============ # In this part of the exercise, you will reuse your logistic regression # code from the last exercise. You task here is to make sure that your # regularized logistic regression implementation is vectorized. After # that, you will implement one-vs-all classification for the handwritten # digit dataset. # print('Training One-vs-All Logistic Regression...') Lambda = 0. all_theta = oneVsAll(X, y, num_labels, Lambda)
def ex7_pca(): ## Machine Learning Online Class # Exercise 7 | Principle Component Analysis and K-Means Clustering # # Instructions # ------------ # # This file contains code that helps you get started on the # exercise. You will need to complete the following functions: # # pca.m # projectData.m # recoverData.m # computeCentroids.m # findClosestCentroids.m # kMeansInitCentroids.m # # For this exercise, you will not need to change any code in this file, # or any other files other than those mentioned above. # ## Initialization #clear ; close all; clc ## ================== Part 1: Load Example Dataset =================== # We start this exercise by using a small dataset that is easily to # visualize # print('Visualizing example dataset for PCA.\n') # The following command loads the dataset. You should now have the # variable X in your environment mat = scipy.io.loadmat('ex7data1.mat') X = mat['X'] # Visualize the example dataset plt.plot(X[:, 0], X[:, 1], 'wo', ms=10, mec='b', mew=1) plt.axis([0.5, 6.5, 2, 8]) plt.savefig('figure1.png') print('Program paused. Press enter to continue.') #pause ## =============== Part 2: Principal Component Analysis =============== # You should now implement PCA, a dimension reduction technique. You # should complete the code in pca.m # print('\nRunning PCA on example dataset.\n') # Before running PCA, it is important to first normalize X X_norm, mu, sigma = featureNormalize(X) # Run PCA U, S = pca(X_norm) # Compute mu, the mean of the each feature # Draw the eigenvectors centered at mean of data. These lines show the # directions of maximum variations in the dataset. #hold on print(S) print(U) drawLine(mu, mu + 1.5 * np.dot(S[0], U[:,0].T)) drawLine(mu, mu + 1.5 * np.dot(S[1], U[:,1].T)) #hold off plt.savefig('figure2.png') print('Top eigenvector: ') print(' U(:,1) = %f %f ' % (U[0,0], U[1,0])) print('\n(you should expect to see -0.707107 -0.707107)') print('Program paused. Press enter to continue.') #pause ## =================== Part 3: Dimension Reduction =================== # You should now implement the projection step to map the data onto the # first k eigenvectors. The code will then plot the data in this reduced # dimensional space. This will show you what the data looks like when # using only the corresponding eigenvectors to reconstruct it. # # You should complete the code in projectData.m # print('\nDimension reduction on example dataset.\n\n') # Plot the normalized dataset (returned from pca) fig = plt.figure() plt.plot(X_norm[:, 0], X_norm[:, 1], 'bo') # Project the data onto K = 1 dimension K = 1 Z = projectData(X_norm, U, K) print('Projection of the first example: %f' % Z[0]) print('\n(this value should be about 1.481274)\n') X_rec = recoverData(Z, U, K) print('Approximation of the first example: %f %f' % (X_rec[0, 0], X_rec[0, 1])) print('\n(this value should be about -1.047419 -1.047419)\n') # Draw lines connecting the projected points to the original points plt.plot(X_rec[:, 0], X_rec[:, 1], 'ro') for i in range(X_norm.shape[0]): drawLine(X_norm[i,:], X_rec[i,:]) #end plt.savefig('figure3.png') print('Program paused. Press enter to continue.\n') #pause ## =============== Part 4: Loading and Visualizing Face Data ============= # We start the exercise by first loading and visualizing the dataset. # The following code will load the dataset into your environment # print('\nLoading face dataset.\n\n') # Load Face dataset mat = scipy.io.loadmat('ex7faces.mat') X = mat['X'] # Display the first 100 faces in the dataset displayData(X[:100, :]) plt.savefig('figure4.png') print('Program paused. Press enter to continue.\n') #pause ## =========== Part 5: PCA on Face Data: Eigenfaces =================== # Run PCA and visualize the eigenvectors which are in this case eigenfaces # We display the first 36 eigenfaces. # print('\nRunning PCA on face dataset.\n(this mght take a minute or two ...)\n') # Before running PCA, it is important to first normalize X by subtracting # the mean value from each feature X_norm, mu, sigma = featureNormalize(X) # Run PCA U, S = pca(X_norm) # Visualize the top 36 eigenvectors found displayData(U[:, :36].T) plt.savefig('figure5.png') print('Program paused. Press enter to continue.') #pause ## ============= Part 6: Dimension Reduction for Faces ================= # Project images to the eigen space using the top k eigenvectors # If you are applying a machine learning algorithm print('\nDimension reduction for face dataset.\n') K = 100 Z = projectData(X_norm, U, K) print('The projected data Z has a size of: ') print(formatter('%d ', Z.shape)) print('\n\nProgram paused. Press enter to continue.') #pause ## ==== Part 7: Visualization of Faces after PCA Dimension Reduction ==== # Project images to the eigen space using the top K eigen vectors and # visualize only using those K dimensions # Compare to the original input, which is also displayed print('\nVisualizing the projected (reduced dimension) faces.\n') K = 100 X_rec = recoverData(Z, U, K) # Display normalized data #subplot(1, 2, 1) displayData(X_norm[:100,:]) plt.gcf().suptitle('Original faces') #axis square plt.savefig('figure6.a.png') # Display reconstructed data from only k eigenfaces #subplot(1, 2, 2) displayData(X_rec[:100,:]) plt.gcf().suptitle('Recovered faces') #axis square plt.savefig('figure6.b.png') print('Program paused. Press enter to continue.') #pause ## === Part 8(a): Optional (ungraded) Exercise: PCA for Visualization === # One useful application of PCA is to use it to visualize high-dimensional # data. In the last K-Means exercise you ran K-Means on 3-dimensional # pixel colors of an image. We first visualize this output in 3D, and then # apply PCA to obtain a visualization in 2D. #close all; close all; clc # Re-load the image from the previous exercise and run K-Means on it # For this to work, you need to complete the K-Means assignment first A = matplotlib.image.imread('bird_small.png') # If imread does not work for you, you can try instead # load ('bird_small.mat') A = A / 255 X = A.reshape(-1, 3) K = 16 max_iters = 10 initial_centroids = kMeansInitCentroids(X, K) centroids, idx = runkMeans('7', X, initial_centroids, max_iters) # Sample 1000 random indexes (since working with all the data is # too expensive. If you have a fast computer, you may increase this. sel = np.random.choice(X.shape[0], size=1000) # Setup Color Palette #palette = hsv(K) #colors = palette(idx(sel), :) # Visualize the data and centroid memberships in 3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(X[sel, 0], X[sel, 1], X[sel, 2], cmap='rainbow', c=idx[sel], s=8**2) ax.set_title('Pixel dataset plotted in 3D. Color shows centroid memberships') plt.savefig('figure8.png') print('Program paused. Press enter to continue.') #pause ## === Part 8(b): Optional (ungraded) Exercise: PCA for Visualization === # Use PCA to project this cloud to 2D for visualization # Subtract the mean to use PCA X_norm, mu, sigma = featureNormalize(X) # PCA and project the data to 2D U, S = pca(X_norm) Z = projectData(X_norm, U, 2) # Plot in 2D fig = plt.figure() plotDataPoints(Z[sel, :], [idx[sel]], K, 0) plt.title('Pixel dataset plotted in 2D, using PCA for dimensionality reduction') plt.savefig('figure9.png') print('Program paused. Press enter to continue.\n')
# You will be working with a dataset that contains handwritten digits. # # Load Training Data print 'Loading and Visualizing Data ...' data = scipy.io.loadmat('ex3/ex3data1.mat') X = data['X'] y = data['y'] m, _ = X.shape # Randomly select 100 data points to display sel = np.random.permutation(range(m)) sel = sel[0:100] displayData(X[sel,:]) #raw_input("Program paused. Press Enter to continue...") ## ================ Part 2: Loading Pameters ================ # In this part of the exercise, we load some pre-initialized # neural network parameters. print 'Loading Saved Neural Network Parameters ...' # Load the weights into variables Theta1 and Theta2 data = scipy.io.loadmat('ex3/ex3weights.mat') Theta1 = data['Theta1'] Theta2 = data['Theta2'] ## ================= Part 3: Implement Predict =================
# ==================== 1. Neural Networks ==================== # Load saved matrices from file mat_data = sio.loadmat('ex4data1.mat') X = mat_data['X'] y = mat_data['y'].ravel() m, n = X.shape input_layer_size = 400 # 20x20 Input Images of Digits hidden_layer_size = 25 # 25 hidden units num_labels = 10 # 10 labels, from 1 to 10 (note that we have mapped "0" to label 10) # Randomly select 100 data points to display rand_indices = np.random.permutation(m) plt.figure() displayData(X[rand_indices[0:100], :], padding=1) plt.show() # =================== 1.2 Model representation =================== # Load the weights into variables Theta1 and Theta2 mat_param = sio.loadmat('ex4weights.mat') theta_1 = mat_param['Theta1'] theta_2 = mat_param['Theta2'] params_trained = np.hstack((theta_1.flatten(), theta_2.flatten())) # =================== 1.3 Feedforward and cost function ============= l = 0.0 j, _ = nnCostFunction(params_trained, input_layer_size, hidden_layer_size, num_labels, X, y, l) print('Cost at parameters (this value should be about 0.287629):', j)
# You will be working with a dataset that contains handwritten digits. # # Load Training Data print('\n -------------------------- \n') print('Loading and Visualizing Data ...') datafile = 'ex3data1.mat' mat = scipy.io.loadmat(datafile) X, y = mat['X'], mat['y'] m, n = X.shape # Randomly select 100 data points to display sel = np.random.permutation(range(m)) sel = sel[0:100] displayData(X[sel, :]) # %% ================ Part 2: Loading Pameters ================ # In this part of the exercise, we load some pre-initialized # neural network parameters. print('\n -------------------------- \n') print('Loading Saved Neural Network Parameters ...') # Load the weights into variables Theta1 and Theta2 data = scipy.io.loadmat('ex3weights.mat') Theta1 = data['Theta1'] Theta2 = data['Theta2'] # %% ================= Part 3: Implement Predict ================= # After training the neural network, we would like to use it to predict # the labels. You will now implement the "predict" function to use the
# Load Training Data print('Loading and Visualizing Data ...\n') X = np.loadtxt('MNIST_DATA.csv', delimiter =",") m,_ = X.shape y = np.loadtxt('MNIST_DATA_LABEL.csv', delimiter =",").reshape(m, 1) #since python indexes start with 0 and matlab's ones start with 10, we replace all 10 by 0 y = np.where(y == 10, 0, y) #Randomly select 100 data points to display rand_indices = np.random.choice(m, 100) displayData(X[rand_indices]) plt.show() print('Program paused. Press enter to continue.\n') pause() """## Part 2: Loading Pameters ================ In this part of the exercise, we load some pre-initialized neural network parameters.""" print('\nLoading Saved Neural Network Parameters ...\n') # Load the weights into variables Theta1 and Theta2 Theta1 = np.loadtxt('Theta1.csv', delimiter =",")
# You will be working with a dataset that contains handwritten digits. # # Load Training Data print "Loading and Visualizing Data ..." data = scipy.io.loadmat("ex3data1.mat") # training data stored in arrays X, y X = data["X"] y = data["y"] m, _ = X.shape # Randomly select 100 data points to display rand_indices = np.random.permutation(range(m)) sel = X[rand_indices[0:100], :] displayData(sel) raw_input("Program paused. Press Enter to continue...") ## ============ Part 2: Vectorize Logistic Regression ============ # In this part of the exercise, you will reuse your logistic regression # code from the last exercise. You task here is to make sure that your # regularized logistic regression implementation is vectorized. After # that, you will implement one-vs-all classification for the handwritten # digit dataset. # print "Training One-vs-All Logistic Regression..." Lambda = 0.1 all_theta = oneVsAll(X, y, num_labels, Lambda)
# # Load Training Data print('Loading and Visualizing Data ...\n') data = loadmat('ex3data1.mat') X = data['X'] y = data['y'].ravel() m = data['X'] m = X.shape[0] # Randomly select 100 data points to display sell = np.random.permutation(range(m)) sell = sell[:100] displayData(X[sell, :]) print('Program d. Press enter to continue.\n') ## ================ Part 2: Loading Pameters ================ # In this part of the exercise, we load some pre-initialized # neural network parameters. print('\nLoading Saved Neural Network Parameters ...\n') # # Load the weights into variables Theta1 and Theta2 weight = loadmat('ex3weights.mat') Theta1 = weight['Theta1'] Theta2 = weight['Theta2'] # ## ================= Part 3: Implement Predict =================
# change the parameters below. visibleSize = 8 * 8 # number of input units hiddenSize = 25 # number of hidden units sparsityParam = 0.01 # desired average activation of the hidden units. _lambda = 0.0001 # weight decay parameter beta = 3 # weight of sparsity penalty term ##====================================================================== ## STEP 1: Implement sampleIMAGES # # After implementing sampleIMAGES, the display_network command should # display a random sample of 200 patches from the dataset patches = sampleIMAGES() displayData(patches[:, np.random.randint(10000, size=100)].T) # Obtain random parameters theta theta = initializeParameters(hiddenSize, visibleSize) input('\nProgram paused. Press enter to continue.\n') ##====================================================================== ## STEP 2: Implement sparseAutoencoderCost # # You can implement all of the components (squared error cost, weight decay term, # sparsity penalty) in the cost function at once, but it may be easier to do # it step-by-step and run gradient checking (see STEP 3) after each step. We # suggest implementing the sparseAutoencoderCost function using the following steps: # # (a) Implement forward propagation in your neural network, and implement the
drawLine(X_norm[i, :], X_rec[i, :], dash=True) axes = plt.gca() axes.set_xlim([-4, 3]) axes.set_ylim([-4, 3]) axes.set_aspect('equal', adjustable='box') plt.show() # =============== 2.4 Face image dataset ============= print('Loading face dataset.') # Load Face dataset mat_data = sio.loadmat('ex7faces.mat') X = mat_data['X'] plt.figure() displayData(X[0:100, :]) plt.show() # =========== 2.4.1 PCA on faces =================== print('Running PCA on face dataset.') # Before running PCA, it is important to first normalize X by subtracting the mean value from each feature X_norm, mu, sigma = featureNormalize(X) # Run PCA U, S, V = pca(X_norm) # Visualize the top 36 eigenvectors found plt.figure() displayData(U[:, 0:36].T) plt.show()
#print(y[0:10, :]) print(X.shape) print(y.shape) m = X.shape[0] # Randomly select 100 data points to display rand_indices = np.random.permutation(m) #sel = X[rand_indices[1:101], :] #sel_Y = y[rand_indices[1:101], :] sel = X[1:101, :] sel_Y = y[1:101, :] print(y) displayData(sel, sel_Y) ''' %% ============ Part 2: Vectorize Logistic Regression ============ % In this part of the exercise, you will reuse your logistic regression % code from the last exercise. You task here is to make sure that your % regularized logistic regression implementation is vectorized. After % that, you will implement one-vs-all classification for the handwritten % digit dataset. % ''' #fprintf('\nTraining One-vs-All Logistic Regression...\n') lambda_ = 0.1 all_theta = oneVsAll(X, y, num_labels, lambda_)
from kMeansInitCentroids import kMeansInitCentroids from show import show print 'Finding closest centroids.' # Load an example dataset that we will be using data = scipy.io.loadmat('ex7data2.mat') X = data['X'] # Select an initial set of centroids K = 3 # 3 Centroids initial_centroids = np.array([[3, 3], [6, 2], [8, 5]]) displayData(X,[initial_centroids]) plt.show(block=False) # Find the closest centroids for the examples using the # initial_centroids idx = findClosestCentroids(X, initial_centroids) print 'Closest centroids for the first 3 examples:' print idx[0:3].tolist() print '(the closest centroids should be 0, 2, 1 respectively)' raw_input("Program paused. Press Enter to continue...") ## ===================== Part 2: Compute Means ========================= # After implementing the closest centroids function, you should now
input_layer_size = 400 hidden_layer_size = 25 num_labels = 10 # =========== Part 1: Loading and Visualizing Data ============= # We start the exercise by first loading and visualizing the dataset. # You will be working with a dataset that contains handwritten digits. print('Loading and Visualizing Data ...\n') data = loadmat('ex4data1.mat') X, y = data['X'], data['y'] m = X.shape[0] # Randomly select 100 data points to display rand_indices = np.random.permutation(range(m)) sel = X[rand_indices[0:100], :] displayData(sel) # ================ Part 2: Loading Parameters ================ # In this part of the exercise, we load some pre-initialized # neural network parameters. print('Loading Saved Neural Network Parameters ...\n') parameters = loadmat('ex4weights.mat') Theta1, Theta2 = parameters['Theta1'], parameters['Theta2'] # Unroll parameters nn_params = np.hstack((Theta1.T.ravel(), Theta2.T.ravel())) # ================ Part 3: Compute Cost (Feedforward) ================ # To the neural network, you should first start by implementing the
# =============== Part 4: Loading and Visualizing Face Data ============= # We start the exercise by first loading and visualizing the dataset. # The following code will load the dataset into your environment # print('\nLoading face dataset.\n\n') # Load Face dataset #load ('ex7faces.mat') data = sio.loadmat(ml_dir + 'ex7faces.mat') # % training data stored in arrays X, y X = data['X'] # Display the first 100 faces in the dataset displayData(X[0:100, :]) # =========== Part 5: PCA on Face Data: Eigenfaces =================== # Run PCA and visualize the eigenvectors which are in this case eigenfaces # We display the first 36 eigenfaces. # print('\nRunning PCA on face dataset.This mght take a minute or two ...)\n\n') # Before running PCA, it is important to first normalize X by subtracting # the mean value from each feature [X_norm, mu, sigma] = featureNormalize(X) # Run PCA U, S, V = pca(X_norm)
import scipy.io mat = scipy.io.loadmat('ex3data1.mat') X = mat["X"] y = mat["y"] y = y.flatten() m = X.shape[0] # Randomly select 100 data points to display rand_indices = np.random.permutation(m) sel = X[rand_indices[:100],:] dd.displayData(sel) raw_input('Program paused. Press enter to continue.\n') ## ================ Part 2: Loading Pameters ================ # In this part of the exercise, we load some pre-initialized # neural network parameters. print('Loading Saved Neural Network Parameters ...') # Load the weights into variables Theta1 and Theta2 mat = scipy.io.loadmat('ex3weights.mat') Theta1 = mat["Theta1"] Theta2 = mat["Theta2"] ## ================= Part 3: Implement Predict =================
hidden_layer_size = 25 # 25 hidden units num_labels = 10 # Load Training Data print('Loading and Visualizing Data ...') data = sco.loadmat('ex4data1.mat') X, y = data['X'], data['y'] m, n = np.shape(X) # Randomly select 100 data points to display randomVal = np.random.choice(m, m)[:100] sel = X[randomVal] example_width = round(np.sqrt(sel.shape[1])) displayData.displayData(sel, example_width) ## ================ Part 2: Loading Parameters ================ # In this part of the exercise, we load some pre-initialized neural network parameters. print('Loading Saved Neural Network Parameters ...') # Load the weights into variables Theta1 and Theta2 weight = sco.loadmat('ex4weights.mat') Theta1, Theta2 = weight['Theta1'], weight['Theta2'] m1, n1 = np.shape(Theta1) m2, n2 = np.shape(Theta2) # Unroll parameters nn_params = np.r_[(Theta1.ravel().reshape(m1 * n1, 1), Theta2.ravel().reshape(m2 * n2, 1))]
# (note that we have mapped "0" to label 10) # =========== Part 1: Loading and Visualizing Data ============= # Load Training Data print('Loading and Visualizing Data ...') data = io.loadmat('ex3data1.mat') # training data stored in arrays X, y y, X = np.matrix(data['y']), np.matrix(data['X']) m, n = X.shape # Randomly select 100 data points to display rand_indices = np.random.permutation(m) sel = X[rand_indices[:100], :] displayData(sel) input('Program paused. Press enter to continue.') # ================ Part 2: Loading Pameters ================ # In this part of the exercise, we load some pre-initialized # neural network parameters. print('Loading Saved Neural Network Parameters ...') # Load the weights into variables Theta1 and Theta2 weights = io.loadmat('ex3weights.mat') Theta1, Theta2 = np.matrix(weights['Theta1']), np.matrix(weights['Theta2']) # ================= Part 3: Implement Predict ================= # After training the neural network, we would like to use it to predict
print 'Program paused. Press enter to continue.' raw_input() ## =============== Part 4: Loading and Visualizing Face Data ============= # We start the exercise by first loading and visualizing the dataset. # The following code will load the dataset into your environment # print '\nLoading face dataset.\n' # Load Face dataset X = loadmat('ex7faces.mat')['X'] # Display the first 100 faces in the dataset fig = figure() displayData(X[:100, :]) fig.show() print 'Program paused. Press enter to continue.' raw_input() ## =========== Part 5: PCA on Face Data: Eigenfaces =================== # Run PCA and visualize the eigenvectors which are in this case eigenfaces # We display the first 36 eigenfaces. # print '\nRunning PCA on face dataset.' print '(this might take a minute or two ...)\n' # Before running PCA, it is important to first normalize X by subtracting # the mean value from each feature X_norm, mu, sigma = featureNormalize(X)
# # Load Training Data print('Loading and Visualizing Data ...') ex4data1 = loadmat('ex4data1.mat') X = ex4data1['X'] y = ex4data1['y'].ravel() m = size(X, 0) # Randomly select 100 data points to display sel = list(range(m)) random.shuffle(sel) fig = figure() displayData(X[sel[:100], :]) fig.show() print('Program paused. Press enter to continue.') input() ## ================ Part 2: Loading Parameters ================ # In this part of the exercise, we load some pre-initialized # neural network parameters. print('Loading Saved Neural Network Parameters ...') # Load the weights into variables Theta1 and Theta2 ex4weights = loadmat('ex4weights.mat') Theta1 = ex4weights['Theta1'] Theta2 = ex4weights['Theta2']
# # Load Training Data print('Loading and Visualizing Data ...\n') data = sio.loadmat('number_data.mat') # training data stored in arrays X, y X = data['X'] y = data['y'] % 10 m = X.shape[0] # Randomly select 100 data points to display rand_indices = np.random.permutation(m) sel = X[rand_indices[0:100]] displayData(sel) input('Program paused. Press enter to continue.\n') # # ================ Part 2: Loading Pameters ================ # In this part of the exercise, we load some pre-initialized # neural network parameters. print('\nLoading Saved Neural Network Parameters ...\n') # Load the weights into variables Theta1 and Theta2 data = sio.loadmat('weights.mat') Theta1 = data['Theta1'] Theta2 = data['Theta2'] # # ================= Part 3: Implement Predict =================
data = loadmat ("ex4data1.mat") y = data ["y"] X = data ["X"] _lambda = 0.1 muestras = len(X) x_unos = np.hstack([np.ones((len(X),1)),X]) Y = np.ravel(y) weights = loadmat('ex4weights.mat') thetha1,thetha2 = weights['Theta1'], weights['Theta2'] sample = np.random.choice(X.shape[0], 100) im = X[sample,:] dp.displayData(im) #esto muestra la tabla con 100 numeros random dp.displayImage(X[4999]) #esto muestra solo una imagen plt.show() def sigmoid(z): return 1.0/(1.0 + np.exp(-z)) def sigmoidDeriv(z): return sigmoid(z) * (1.0 - sigmoid(z)) def forwardProp(): def fun_coste(X,Y, _lambda,K): theta1 = np.array(thetha1)
print('Часть 1. Визуализация данных') # Загрузка данных и формирование матрицы объекты-признаки X и вектора меток y data = spi.loadmat('data.mat') X = data['X'] y = data['y'] m, n = X.shape # Визуализация данных rand_indices = np.random.permutation(range(m)) sel = X[rand_indices[0:100], :] displayData(sel, 10) input('Программа остановлена. Нажмите Enter для продолжения ... \n') # = Часть 2. Загрузка параметров модели обученной нейронной сети = print('Часть 2. Загрузка параметров модели обученной нейронной сети') # Загрузка параметров weights = spi.loadmat('weights.mat') # Архитектура рассматриваемой нейронной сети является следующей # 1. Число слоев: 3 # 2. Число нейронов во входном слое: 400 (равно числу признаков) без учета компоненты смещения # 3. Число нейронов в скрытом слое: 25 без учета компоненты смещения # 4. Число нейронов в выходном слое: 10 (равно числу классов)
print(X.shape) print(y.shape) m = X.shape[0] # Randomly select 100 data points to display rand_indices = np.random.permutation(m); #sel = X[rand_indices[1:101], :] #sel_Y = y[rand_indices[1:101], :] sel = X[1:101, :] sel_Y = y[1:101, :] print(y) displayData(sel,sel_Y) ''' %% ============ Part 2: Vectorize Logistic Regression ============ % In this part of the exercise, you will reuse your logistic regression % code from the last exercise. You task here is to make sure that your % regularized logistic regression implementation is vectorized. After % that, you will implement one-vs-all classification for the handwritten % digit dataset. % ''' #fprintf('\nTraining One-vs-All Logistic Regression...\n') lambda_ = 0.1