Programming homeworks of Stanford Machine learning open course. The original version is in Matlab, rewritten with numpy.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

#### 97 lines 2.9 KiB Raw Blame History

 `## Machine Learning Online Class - Exercise 3 | Part 2: Neural Networks` `import numpy as np` `import matplotlib.pyplot as plt` `import scipy, scipy.io, scipy.optimize` `from ex3.ex3_ import displaydata, sigmoid` ``` ``` ``` ``` `# PREDICT Predict the label of an input given a trained neural network` `# p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the` `# trained weights of a neural network (Theta1, Theta2)` `def predict(Theta1, Theta2, X):` ` # Useful values` ` m = X.shape` ` num_labels = Theta2.shape` ``` ``` ` # Add ones to the X data matrix` ` X = np.hstack((np.ones((m, 1)), X))` ``` ``` ` # First hidden layer` ` temp = sigmoid(X.dot(Theta1.T))` ` m2 = temp.shape` ``` ``` ` # Add ones to hidden layer units` ` temp = np.hstack((np.ones((m2, 1)), temp))` ``` ``` ` # Output layer` ` temp = sigmoid(temp.dot(Theta2.T))` ``` ``` ` p = temp.argmax(axis=1) + 1` ` return p` ``` ``` ``` ``` ``` ``` `## ===================== Start Main ======================` `if __name__ == "__main__":` ` ## 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` ``` ``` ` ## =========== Part 1: Loading and Visualizing Data =============` ` # Load Training Data` ` print('Loading and Visualizing Data ...\n')` ``` ``` ` data = scipy.io.loadmat('mat/ex3data1.mat', matlab_compatible=True) # training data stored in arrays X, y` ` X = data['X']` ` y = data['y']` ` m = X.shape` ``` ``` ` # Randomly select 100 data points to display` ` sel = np.random.permutation(m)` ` sel = X[sel[:100], :]` ``` ``` ` displaydata(sel)` ` plt.show()` ``` ``` ` print('Program paused. Press enter to continue.\n')` ` input()` ``` ``` ` ## ================ Part 2: Loading Pameters ================` ` print('\nLoading Saved Neural Network Parameters ...\n')` ``` ``` ` # Load the weights into variables Theta1 and Theta2` ` data = scipy.io.loadmat('mat/ex3weights.mat', matlab_compatible=True)` ` Theta1 = data['Theta1']` ` Theta2 = data['Theta2']` ``` ``` ``` ``` ` ## ================= Part 3: Implement Predict =================` ` pred = predict(Theta1, Theta2, X)` ``` ``` ` print('\nTraining Set Accuracy: {0:f}\n'.format(np.mean(pred == y.flatten()) * 100))` ``` ``` ` print('Program paused. Press enter to continue.\n')` ` input()` ``` ``` ` # 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.permutation(m)` ``` ``` ` for i in range(0, m):` ` ind = rp[i] # Chosen index in X` ``` ``` ` # Display` ` print('\nDisplaying Example Image\n')` ` displaydata(X[ind:ind+1, :])` ` plt.pause(0.0001)` ` plt.show(block=False)` ``` ``` ` pred = predict(Theta1, Theta2, X[ind:ind+1,:])` ` print('\nNeural Network Prediction: {0:d} (digit {1:d})\n'.format(pred, np.mod(pred, 10)))` ``` ``` ` # Pause` ` print('Program paused. Press enter to continue.\n')` ``` input() ``` ``` ```