Part 2: Neural Networks


In [ ]:
import numpy as np
import scipy.io
import matplotlib.pyplot as plt
%matplotlib inline

In [ ]:
ex3data1 = scipy.io.loadmat("./ex3data1.mat")
X = ex3data1['X']
y = ex3data1['y'][:,0]
y[y==10] = 0
m, n = X.shape
m, n

In [ ]:
## Setup the parameters you will use for this exercise
input_layer_size  = n    # 20x20 Input Images of Digits
hidden_layer_size = 25   # 25 hidden units
num_labels = 10          # 10 labels, from 0 to 9

We have supplied pre-calculated neural network parameters. We load them:


In [ ]:
ex3weights = scipy.io.loadmat('./ex3weights.mat')
Theta1 = ex3weights['Theta1']
Theta2 = ex3weights['Theta2']
Theta1.shape, Theta2.shape

In [ ]:
def sigmoid(z):
    return 1 / (1+np.exp(-z))

In [ ]:
def predict(Theta1, Theta2, X):
    #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)
    
    
    # You need to return the following variables correctly 
    p = np.zeros(X.shape[0])

    
    # ====================== YOUR CODE HERE ======================
    # Instructions: Complete the following code to make predictions using
    #               your learned neural network. You should set p to a 
    #               vector containing labels between 1 to num_labels.
    #
    # Hint: The np.argmax might come in useful.




    # =========================================================================
    
    return p

In [ ]:
predictions = predict(Theta1, Theta2, X)
# The supplied parameters assume label ordering of 1,2,3,4,5,6,7,8,9,0
norm_predictions = (predictions + 1) % 10
plt.scatter(range(m),norm_predictions, s=1)

In [ ]:
(y == norm_predictions).mean()