Implementing a Neural Network

In this exercise we will develop a neural network with fully-connected layers to perform classification, and test it out on the CIFAR-10 dataset.


In [1]:
# A bit of setup

import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

# for auto-reloading external modules
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2

def rel_error(x, y):
  """ returns relative error """
  return np.max(np.abs(x - y) / (np.maximum(1e-8, np.abs(x) + np.abs(y))))

The neural network parameters will be stored in a dictionary (model below), where the keys are the parameter names and the values are numpy arrays. Below, we initialize toy data and a toy model that we will use to verify your implementations.


In [2]:
# Create some toy data to check your implementations
input_size = 4
hidden_size = 10
num_classes = 3
num_inputs = 5

def init_toy_model():
  model = {}
  model['W1'] = np.linspace(-0.2, 0.6, num=input_size*hidden_size).reshape(input_size, hidden_size)
  model['b1'] = np.linspace(-0.3, 0.7, num=hidden_size)
  model['W2'] = np.linspace(-0.4, 0.1, num=hidden_size*num_classes).reshape(hidden_size, num_classes)
  model['b2'] = np.linspace(-0.5, 0.9, num=num_classes)
  return model

def init_toy_data():
  X = np.linspace(-0.2, 0.5, num=num_inputs*input_size).reshape(num_inputs, input_size)
  y = np.array([0, 1, 2, 2, 1])
  return X, y

model = init_toy_model()
X, y = init_toy_data()

Forward pass: compute scores

Open the file cs231n/classifiers/neural_net.py and look at the function two_layer_net. This function is very similar to the loss functions you have written for the SVM and Softmax exercises: It takes the data and weights and computes the class scores, the loss, and the gradients on the parameters.

Implement the first part of the forward pass which uses the weights and biases to compute the scores for all inputs.


In [3]:
from cs231n.classifiers.neural_net import two_layer_net

scores = two_layer_net(X, model)
print scores
correct_scores = [[-0.5328368, 0.20031504, 0.93346689],
 [-0.59412164, 0.15498488, 0.9040914 ],
 [-0.67658362, 0.08978957, 0.85616275],
 [-0.77092643, 0.01339997, 0.79772637],
 [-0.89110401, -0.08754544, 0.71601312]]

# the difference should be very small. We get 3e-8
print 'Difference between your scores and correct scores:'
print np.sum(np.abs(scores - correct_scores))


[[-0.5328368   0.20031504  0.93346689]
 [-0.59412164  0.15498488  0.9040914 ]
 [-0.67658362  0.08978957  0.85616275]
 [-0.77092643  0.01339997  0.79772637]
 [-0.89110401 -0.08754544  0.71601312]]
Difference between your scores and correct scores:
3.84868230029e-08

Forward pass: compute loss

In the same function, implement the second part that computes the data and regularizaion loss.


In [4]:
reg = 0.1
loss, _ = two_layer_net(X, model, y, reg)
correct_loss = 1.38191946092

# should be very small, we get 5e-12
print 'Difference between your loss and correct loss:'
print np.sum(np.abs(loss - correct_loss))


Difference between your loss and correct loss:
4.67692551354e-12

Backward pass

Implement the rest of the function. This will compute the gradient of the loss with respect to the variables W1, b1, W2, and b2. Now that you (hopefully!) have a correctly implemented forward pass, you can debug your backward pass using a numeric gradient check:


In [5]:
from cs231n.gradient_check import eval_numerical_gradient

# Use numeric gradient checking to check your implementation of the backward pass.
# If your implementation is correct, the difference between the numeric and
# analytic gradients should be less than 1e-8 for each of W1, W2, b1, and b2.

loss, grads = two_layer_net(X, model, y, reg)

# these should all be less than 1e-8 or so
for param_name in grads:
  param_grad_num = eval_numerical_gradient(lambda W: two_layer_net(X, model, y, reg)[0], model[param_name], verbose=False)
  print '%s max relative error: %e' % (param_name, rel_error(param_grad_num, grads[param_name]))


b2 max relative error: 8.190173e-11
W2 max relative error: 1.786138e-09
W1 max relative error: 4.426512e-09
b1 max relative error: 5.435436e-08

Train the network

To train the network we will use SGD with Momentum. Last assignment you implemented vanilla SGD. You will now implement the momentum update and the RMSProp update. Open the file classifier_trainer.py and familiarze yourself with the ClassifierTrainer class. It performs optimization given an arbitrary cost function data, and model. By default it uses vanilla SGD, which we have already implemented for you. First, run the optimization below using Vanilla SGD:


In [6]:
from cs231n.classifier_trainer import ClassifierTrainer

model = init_toy_model()
trainer = ClassifierTrainer()
# call the trainer to optimize the loss
# Notice that we're using sample_batches=False, so we're performing Gradient Descent (no sampled batches of data)
best_model, loss_history, _, _ = trainer.train(X, y, X, y,
                                             model, two_layer_net,
                                             reg=0.001,
                                             learning_rate=1e-1, momentum=0.0, learning_rate_decay=1,
                                             update='sgd', sample_batches=False,
                                             num_epochs=100,
                                             verbose=False)
print 'Final loss with vanilla SGD: %f' % (loss_history[-1], )


Finished epoch 0 / 100: cost 1.125220, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 1 / 100: cost 1.107355, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 2 / 100: cost 1.094051, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 3 / 100: cost 1.084137, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 4 / 100: cost 1.076713, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 5 / 100: cost 1.071104, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 6 / 100: cost 1.066806, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 7 / 100: cost 1.063451, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 8 / 100: cost 1.060770, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 9 / 100: cost 1.058569, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 10 / 100: cost 1.056708, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 11 / 100: cost 1.055089, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 12 / 100: cost 1.053640, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 13 / 100: cost 1.052310, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 14 / 100: cost 1.051064, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 15 / 100: cost 1.049876, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 16 / 100: cost 1.048728, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 17 / 100: cost 1.047607, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 18 / 100: cost 1.046504, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 19 / 100: cost 1.045412, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 20 / 100: cost 1.044328, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 21 / 100: cost 1.043246, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 22 / 100: cost 1.042166, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 23 / 100: cost 1.041085, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 24 / 100: cost 1.040002, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 25 / 100: cost 1.038916, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 26 / 100: cost 1.037826, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 27 / 100: cost 1.036732, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 28 / 100: cost 1.035632, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 29 / 100: cost 1.034527, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 30 / 100: cost 1.033417, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 31 / 100: cost 1.032300, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 32 / 100: cost 1.031176, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 33 / 100: cost 1.030046, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 34 / 100: cost 1.028918, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 35 / 100: cost 1.027800, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 36 / 100: cost 1.026662, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 37 / 100: cost 1.025530, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 38 / 100: cost 1.024383, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 39 / 100: cost 1.023236, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 40 / 100: cost 1.022078, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 41 / 100: cost 1.020915, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 42 / 100: cost 1.019746, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 43 / 100: cost 1.018566, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 44 / 100: cost 1.017386, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 45 / 100: cost 1.016187, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 46 / 100: cost 1.014996, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 47 / 100: cost 1.013777, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 48 / 100: cost 1.012575, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 49 / 100: cost 1.011335, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 50 / 100: cost 1.010121, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 51 / 100: cost 1.008859, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 52 / 100: cost 1.007633, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 53 / 100: cost 1.006348, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 54 / 100: cost 1.005109, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 55 / 100: cost 1.003800, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 56 / 100: cost 1.002549, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 57 / 100: cost 1.001216, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 58 / 100: cost 0.999949, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 59 / 100: cost 0.998598, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 60 / 100: cost 0.997306, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 61 / 100: cost 0.995975, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 62 / 100: cost 0.994723, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 63 / 100: cost 0.993374, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 64 / 100: cost 0.992111, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 65 / 100: cost 0.990743, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 66 / 100: cost 0.989469, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 67 / 100: cost 0.988082, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 68 / 100: cost 0.986797, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 69 / 100: cost 0.985390, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 70 / 100: cost 0.984093, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 71 / 100: cost 0.982668, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 72 / 100: cost 0.981354, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 73 / 100: cost 0.979923, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 74 / 100: cost 0.978575, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 75 / 100: cost 0.977143, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 76 / 100: cost 0.975744, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 77 / 100: cost 0.974305, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 78 / 100: cost 0.972868, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 79 / 100: cost 0.971432, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 80 / 100: cost 0.969956, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 81 / 100: cost 0.968524, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 82 / 100: cost 0.967007, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 83 / 100: cost 0.965581, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 84 / 100: cost 0.964022, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 85 / 100: cost 0.962603, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 86 / 100: cost 0.961004, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 87 / 100: cost 0.959586, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 88 / 100: cost 0.957956, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 89 / 100: cost 0.956526, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 90 / 100: cost 0.954873, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 91 / 100: cost 0.953431, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 92 / 100: cost 0.951753, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 93 / 100: cost 0.950299, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 94 / 100: cost 0.948597, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 95 / 100: cost 0.947132, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 96 / 100: cost 0.945406, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 97 / 100: cost 0.943929, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 98 / 100: cost 0.942184, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 99 / 100: cost 0.940686, train: 0.400000, val 0.400000, lr 1.000000e-01
Final loss with vanilla SGD: 0.940686

Now fill in the momentum update in the first missing code block inside the train function, and run the same optimization as above but with the momentum update. You should see a much better result in the final obtained loss:


In [7]:
model = init_toy_model()
trainer = ClassifierTrainer()
# call the trainer to optimize the loss
# Notice that we're using sample_batches=False, so we're performing Gradient Descent (no sampled batches of data)
best_model, loss_history, _, _ = trainer.train(X, y, X, y,
                                             model, two_layer_net,
                                             reg=0.001,
                                             learning_rate=1e-1, momentum=0.9, learning_rate_decay=1,
                                             update='momentum', sample_batches=False,
                                             num_epochs=100,
                                             verbose=False)
correct_loss = 0.494394
print 'Final loss with momentum SGD: %f. We get: %f' % (loss_history[-1], correct_loss)


Finished epoch 0 / 100: cost 1.125220, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 1 / 100: cost 1.107355, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 2 / 100: cost 1.082366, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 3 / 100: cost 1.062956, train: 0.200000, val 0.200000, lr 1.000000e-01
Finished epoch 4 / 100: cost 1.056309, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 5 / 100: cost 1.060496, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 6 / 100: cost 1.067216, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 7 / 100: cost 1.068161, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 8 / 100: cost 1.059913, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 9 / 100: cost 1.044799, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 10 / 100: cost 1.028297, train: 0.200000, val 0.200000, lr 1.000000e-01
Finished epoch 11 / 100: cost 1.015242, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 12 / 100: cost 1.007327, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 13 / 100: cost 1.002891, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 14 / 100: cost 0.998442, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 15 / 100: cost 0.991486, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 16 / 100: cost 0.980994, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 17 / 100: cost 0.967566, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 18 / 100: cost 0.953211, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 19 / 100: cost 0.939365, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 20 / 100: cost 0.927013, train: 0.200000, val 0.200000, lr 1.000000e-01
Finished epoch 21 / 100: cost 0.916019, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 22 / 100: cost 0.906615, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 23 / 100: cost 0.895739, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 24 / 100: cost 0.883325, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 25 / 100: cost 0.868924, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 26 / 100: cost 0.853357, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 27 / 100: cost 0.839343, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 28 / 100: cost 0.826565, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 29 / 100: cost 0.814707, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 30 / 100: cost 0.804486, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 31 / 100: cost 0.793798, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 32 / 100: cost 0.782511, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 33 / 100: cost 0.771401, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 34 / 100: cost 0.759565, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 35 / 100: cost 0.749276, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 36 / 100: cost 0.740494, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 37 / 100: cost 0.731286, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 38 / 100: cost 0.723294, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 39 / 100: cost 0.715775, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 40 / 100: cost 0.707801, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 41 / 100: cost 0.700965, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 42 / 100: cost 0.694539, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 43 / 100: cost 0.688068, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 44 / 100: cost 0.682277, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 45 / 100: cost 0.676757, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 46 / 100: cost 0.671309, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 47 / 100: cost 0.665160, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 48 / 100: cost 0.659684, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 49 / 100: cost 0.654924, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 50 / 100: cost 0.651388, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 51 / 100: cost 0.647858, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 52 / 100: cost 0.644354, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 53 / 100: cost 0.641141, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 54 / 100: cost 0.637402, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 55 / 100: cost 0.633878, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 56 / 100: cost 0.630529, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 57 / 100: cost 0.626991, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 58 / 100: cost 0.623562, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 59 / 100: cost 0.620400, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 60 / 100: cost 0.617649, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 61 / 100: cost 0.614649, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 62 / 100: cost 0.612319, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 63 / 100: cost 0.609449, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 64 / 100: cost 0.606737, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 65 / 100: cost 0.604003, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 66 / 100: cost 0.601308, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 67 / 100: cost 0.598893, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 68 / 100: cost 0.596186, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 69 / 100: cost 0.593349, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 70 / 100: cost 0.590431, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 71 / 100: cost 0.587889, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 72 / 100: cost 0.585493, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 73 / 100: cost 0.582927, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 74 / 100: cost 0.580394, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 75 / 100: cost 0.577568, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 76 / 100: cost 0.574544, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 77 / 100: cost 0.571470, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 78 / 100: cost 0.568821, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 79 / 100: cost 0.566309, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 80 / 100: cost 0.563435, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 81 / 100: cost 0.560370, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 82 / 100: cost 0.557238, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 83 / 100: cost 0.554067, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 84 / 100: cost 0.551146, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 85 / 100: cost 0.547899, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 86 / 100: cost 0.544725, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 87 / 100: cost 0.541299, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 88 / 100: cost 0.537863, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 89 / 100: cost 0.534274, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 90 / 100: cost 0.530788, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 91 / 100: cost 0.527103, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 92 / 100: cost 0.523383, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 93 / 100: cost 0.519479, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 94 / 100: cost 0.515567, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 95 / 100: cost 0.511614, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 96 / 100: cost 0.507558, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 97 / 100: cost 0.503282, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 98 / 100: cost 0.498852, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 99 / 100: cost 0.494394, train: 0.800000, val 0.800000, lr 1.000000e-01
Final loss with momentum SGD: 0.494394. We get: 0.494394

Now also implement the RMSProp update rule inside the train function and rerun the optimization:


In [8]:
model = init_toy_model()
trainer = ClassifierTrainer()
# call the trainer to optimize the loss
# Notice that we're using sample_batches=False, so we're performing Gradient Descent (no sampled batches of data)
best_model, loss_history, _, _ = trainer.train(X, y, X, y,
                                             model, two_layer_net,
                                             reg=0.001,
                                             learning_rate=1e-1, momentum=0.9, learning_rate_decay=1,
                                             update='rmsprop', sample_batches=False,
                                             num_epochs=100,
                                             verbose=False)
correct_loss = 0.439368
print 'Final loss with RMSProp: %f. We get: %f' % (loss_history[-1], correct_loss)


Finished epoch 0 / 100: cost 1.125220, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 1 / 100: cost 2.871420, train: 0.200000, val 0.200000, lr 1.000000e-01
Finished epoch 2 / 100: cost 1.318958, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 3 / 100: cost 1.149133, train: 0.400000, val 0.400000, lr 1.000000e-01
Finished epoch 4 / 100: cost 1.112934, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 5 / 100: cost 0.992248, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 6 / 100: cost 0.895418, train: 0.600000, val 0.600000, lr 1.000000e-01
Finished epoch 7 / 100: cost 0.811222, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 8 / 100: cost 0.751323, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 9 / 100: cost 0.710385, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 10 / 100: cost 0.680959, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 11 / 100: cost 0.658590, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 12 / 100: cost 0.640606, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 13 / 100: cost 0.625372, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 14 / 100: cost 0.611866, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 15 / 100: cost 0.599463, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 16 / 100: cost 0.587802, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 17 / 100: cost 0.576712, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 18 / 100: cost 0.566154, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 19 / 100: cost 0.556165, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 20 / 100: cost 0.546822, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 21 / 100: cost 0.538195, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 22 / 100: cost 0.530332, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 23 / 100: cost 0.523241, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 24 / 100: cost 0.516893, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 25 / 100: cost 0.511232, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 26 / 100: cost 0.506190, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 27 / 100: cost 0.501689, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 28 / 100: cost 0.497657, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 29 / 100: cost 0.494029, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 30 / 100: cost 0.490747, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 31 / 100: cost 0.487763, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 32 / 100: cost 0.485038, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 33 / 100: cost 0.482537, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 34 / 100: cost 0.480233, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 35 / 100: cost 0.478103, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 36 / 100: cost 0.476127, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 37 / 100: cost 0.474289, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 38 / 100: cost 0.472575, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 39 / 100: cost 0.470973, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 40 / 100: cost 0.469472, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 41 / 100: cost 0.468063, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 42 / 100: cost 0.466738, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 43 / 100: cost 0.465490, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 44 / 100: cost 0.464313, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 45 / 100: cost 0.463200, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 46 / 100: cost 0.462147, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 47 / 100: cost 0.461149, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 48 / 100: cost 0.460202, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 49 / 100: cost 0.459301, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 50 / 100: cost 0.458445, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 51 / 100: cost 0.457629, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 52 / 100: cost 0.456851, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 53 / 100: cost 0.456108, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 54 / 100: cost 0.455398, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 55 / 100: cost 0.454718, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 56 / 100: cost 0.454067, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 57 / 100: cost 0.453443, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 58 / 100: cost 0.452843, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 59 / 100: cost 0.452268, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 60 / 100: cost 0.451714, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 61 / 100: cost 0.451181, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 62 / 100: cost 0.450668, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 63 / 100: cost 0.450173, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 64 / 100: cost 0.449696, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 65 / 100: cost 0.449235, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 66 / 100: cost 0.448790, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 67 / 100: cost 0.448359, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 68 / 100: cost 0.447942, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 69 / 100: cost 0.447539, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 70 / 100: cost 0.447148, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 71 / 100: cost 0.446769, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 72 / 100: cost 0.446402, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 73 / 100: cost 0.446045, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 74 / 100: cost 0.445698, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 75 / 100: cost 0.445361, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 76 / 100: cost 0.445034, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 77 / 100: cost 0.444716, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 78 / 100: cost 0.444406, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 79 / 100: cost 0.444104, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 80 / 100: cost 0.443810, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 81 / 100: cost 0.443524, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 82 / 100: cost 0.443245, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 83 / 100: cost 0.442972, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 84 / 100: cost 0.442707, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 85 / 100: cost 0.442447, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 86 / 100: cost 0.442194, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 87 / 100: cost 0.441947, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 88 / 100: cost 0.441705, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 89 / 100: cost 0.441469, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 90 / 100: cost 0.441238, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 91 / 100: cost 0.441012, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 92 / 100: cost 0.440791, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 93 / 100: cost 0.440575, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 94 / 100: cost 0.440363, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 95 / 100: cost 0.440156, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 96 / 100: cost 0.439953, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 97 / 100: cost 0.439754, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 98 / 100: cost 0.439559, train: 0.800000, val 0.800000, lr 1.000000e-01
Finished epoch 99 / 100: cost 0.439368, train: 0.800000, val 0.800000, lr 1.000000e-01
Final loss with RMSProp: 0.439368. We get: 0.439368

Load the data

Now that you have implemented a two-layer network that passes gradient checks, it's time to load up our favorite CIFAR-10 data so we can use it to train a classifier.


In [9]:
from cs231n.data_utils import load_CIFAR10

def get_CIFAR10_data(num_training=49000, num_validation=1000, num_test=1000):
    """
    Load the CIFAR-10 dataset from disk and perform preprocessing to prepare
    it for the two-layer neural net classifier. These are the same steps as
    we used for the SVM, but condensed to a single function.  
    """
    # Load the raw CIFAR-10 data
    cifar10_dir = 'cs231n/datasets/cifar-10-batches-py'
    X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)
        
    # Subsample the data
    mask = range(num_training, num_training + num_validation)
    X_val = X_train[mask]
    y_val = y_train[mask]
    mask = range(num_training)
    X_train = X_train[mask]
    y_train = y_train[mask]
    mask = range(num_test)
    X_test = X_test[mask]
    y_test = y_test[mask]

    # Normalize the data: subtract the mean image
    mean_image = np.mean(X_train, axis=0)
    X_train -= mean_image
    X_val -= mean_image
    X_test -= mean_image

    # Reshape data to rows
    X_train = X_train.reshape(num_training, -1)
    X_val = X_val.reshape(num_validation, -1)
    X_test = X_test.reshape(num_test, -1)

    return X_train, y_train, X_val, y_val, X_test, y_test


# Invoke the above function to get our data.
X_train, y_train, X_val, y_val, X_test, y_test = get_CIFAR10_data()
print 'Train data shape: ', X_train.shape
print 'Train labels shape: ', y_train.shape
print 'Validation data shape: ', X_val.shape
print 'Validation labels shape: ', y_val.shape
print 'Test data shape: ', X_test.shape
print 'Test labels shape: ', y_test.shape


Train data shape:  (49000, 3072)
Train labels shape:  (49000,)
Validation data shape:  (1000, 3072)
Validation labels shape:  (1000,)
Test data shape:  (1000, 3072)
Test labels shape:  (1000,)

Train a network

To train our network we will use SGD with momentum. In addition, we will adjust the learning rate with an exponential learning rate schedule as optimization proceeds; after each epoch, we will reduce the learning rate by multiplying it by a decay rate.


In [10]:
from cs231n.classifiers.neural_net import init_two_layer_model

model = init_two_layer_model(32*32*3, 50, 10) # input size, hidden size, number of classes
trainer = ClassifierTrainer()
best_model, loss_history, train_acc, val_acc = trainer.train(X_train, y_train, X_val, y_val,
                                             model, two_layer_net,
                                             num_epochs=5, reg=1.0,
                                             momentum=0.9, learning_rate_decay = 0.95,
                                             learning_rate=1e-5, verbose=True)


starting iteration  0
Finished epoch 0 / 5: cost 7.638946, train: 0.152000, val 0.137000, lr 1.000000e-05
starting iteration  10
starting iteration  20
starting iteration  30
starting iteration  40
starting iteration  50
starting iteration  60
starting iteration  70
starting iteration  80
starting iteration  90
starting iteration  100
starting iteration  110
starting iteration  120
starting iteration  130
starting iteration  140
starting iteration  150
starting iteration  160
starting iteration  170
starting iteration  180
starting iteration  190
starting iteration  200
starting iteration  210
starting iteration  220
starting iteration  230
starting iteration  240
starting iteration  250
starting iteration  260
starting iteration  270
starting iteration  280
starting iteration  290
starting iteration  300
starting iteration  310
starting iteration  320
starting iteration  330
starting iteration  340
starting iteration  350
starting iteration  360
starting iteration  370
starting iteration  380
starting iteration  390
starting iteration  400
starting iteration  410
starting iteration  420
starting iteration  430
starting iteration  440
starting iteration  450
starting iteration  460
starting iteration  470
starting iteration  480
Finished epoch 1 / 5: cost 3.847813, train: 0.354000, val 0.338000, lr 9.500000e-06
starting iteration  490
starting iteration  500
starting iteration  510
starting iteration  520
starting iteration  530
starting iteration  540
starting iteration  550
starting iteration  560
starting iteration  570
starting iteration  580
starting iteration  590
starting iteration  600
starting iteration  610
starting iteration  620
starting iteration  630
starting iteration  640
starting iteration  650
starting iteration  660
starting iteration  670
starting iteration  680
starting iteration  690
starting iteration  700
starting iteration  710
starting iteration  720
starting iteration  730
starting iteration  740
starting iteration  750
starting iteration  760
starting iteration  770
starting iteration  780
starting iteration  790
starting iteration  800
starting iteration  810
starting iteration  820
starting iteration  830
starting iteration  840
starting iteration  850
starting iteration  860
starting iteration  870
starting iteration  880
starting iteration  890
starting iteration  900
starting iteration  910
starting iteration  920
starting iteration  930
starting iteration  940
starting iteration  950
starting iteration  960
starting iteration  970
Finished epoch 2 / 5: cost 3.254872, train: 0.384000, val 0.365000, lr 9.025000e-06
starting iteration  980
starting iteration  990
starting iteration  1000
starting iteration  1010
starting iteration  1020
starting iteration  1030
starting iteration  1040
starting iteration  1050
starting iteration  1060
starting iteration  1070
starting iteration  1080
starting iteration  1090
starting iteration  1100
starting iteration  1110
starting iteration  1120
starting iteration  1130
starting iteration  1140
starting iteration  1150
starting iteration  1160
starting iteration  1170
starting iteration  1180
starting iteration  1190
starting iteration  1200
starting iteration  1210
starting iteration  1220
starting iteration  1230
starting iteration  1240
starting iteration  1250
starting iteration  1260
starting iteration  1270
starting iteration  1280
starting iteration  1290
starting iteration  1300
starting iteration  1310
starting iteration  1320
starting iteration  1330
starting iteration  1340
starting iteration  1350
starting iteration  1360
starting iteration  1370
starting iteration  1380
starting iteration  1390
starting iteration  1400
starting iteration  1410
starting iteration  1420
starting iteration  1430
starting iteration  1440
starting iteration  1450
starting iteration  1460
Finished epoch 3 / 5: cost 3.462594, train: 0.410000, val 0.408000, lr 8.573750e-06
starting iteration  1470
starting iteration  1480
starting iteration  1490
starting iteration  1500
starting iteration  1510
starting iteration  1520
starting iteration  1530
starting iteration  1540
starting iteration  1550
starting iteration  1560
starting iteration  1570
starting iteration  1580
starting iteration  1590
starting iteration  1600
starting iteration  1610
starting iteration  1620
starting iteration  1630
starting iteration  1640
starting iteration  1650
starting iteration  1660
starting iteration  1670
starting iteration  1680
starting iteration  1690
starting iteration  1700
starting iteration  1710
starting iteration  1720
starting iteration  1730
starting iteration  1740
starting iteration  1750
starting iteration  1760
starting iteration  1770
starting iteration  1780
starting iteration  1790
starting iteration  1800
starting iteration  1810
starting iteration  1820
starting iteration  1830
starting iteration  1840
starting iteration  1850
starting iteration  1860
starting iteration  1870
starting iteration  1880
starting iteration  1890
starting iteration  1900
starting iteration  1910
starting iteration  1920
starting iteration  1930
starting iteration  1940
starting iteration  1950
Finished epoch 4 / 5: cost 3.143730, train: 0.466000, val 0.408000, lr 8.145063e-06
starting iteration  1960
starting iteration  1970
starting iteration  1980
starting iteration  1990
starting iteration  2000
starting iteration  2010
starting iteration  2020
starting iteration  2030
starting iteration  2040
starting iteration  2050
starting iteration  2060
starting iteration  2070
starting iteration  2080
starting iteration  2090
starting iteration  2100
starting iteration  2110
starting iteration  2120
starting iteration  2130
starting iteration  2140
starting iteration  2150
starting iteration  2160
starting iteration  2170
starting iteration  2180
starting iteration  2190
starting iteration  2200
starting iteration  2210
starting iteration  2220
starting iteration  2230
starting iteration  2240
starting iteration  2250
starting iteration  2260
starting iteration  2270
starting iteration  2280
starting iteration  2290
starting iteration  2300
starting iteration  2310
starting iteration  2320
starting iteration  2330
starting iteration  2340
starting iteration  2350
starting iteration  2360
starting iteration  2370
starting iteration  2380
starting iteration  2390
starting iteration  2400
starting iteration  2410
starting iteration  2420
starting iteration  2430
starting iteration  2440
Finished epoch 5 / 5: cost 3.230495, train: 0.504000, val 0.425000, lr 7.737809e-06
finished optimization. best validation accuracy: 0.425000

Debug the training

With the default parameters we provided above, you should get a validation accuracy of about 0.37 on the validation set. This isn't very good.

One strategy for getting insight into what's wrong is to plot the loss function and the accuracies on the training and validation sets during optimization.

Another strategy is to visualize the weights that were learned in the first layer of the network. In most neural networks trained on visual data, the first layer weights typically show some visible structure when visualized.


In [30]:
# Plot the loss function and train / validation accuracies
plt.subplot(2, 1, 1)
plt.plot(loss_history)
plt.title('Loss history')
plt.xlabel('Iteration')
plt.ylabel('Loss')

plt.subplot(2, 1, 2)
plt.plot(train_acc)
plt.plot(val_acc)
plt.legend(['Training accuracy', 'Validation accuracy'], loc='lower right')
plt.xlabel('Epoch')
plt.ylabel('Clasification accuracy')


Out[30]:
<matplotlib.text.Text at 0x7f45c99e6d50>

In [28]:
from cs231n.vis_utils import visualize_grid

# Visualize the weights of the network

def show_net_weights(model):
    plt.imshow(visualize_grid(model['W1'].T.reshape(-1, 32, 32, 3), padding=3).astype('uint8'))
    plt.gca().axis('off')
    plt.show()

show_net_weights(model)


Tune your hyperparameters

What's wrong?. Looking at the visualizations above, we see that the loss is decreasing more or less linearly, which seems to suggest that the learning rate may be too low. Moreover, there is no gap between the training and validation accuracy, suggesting that the model we used has low capacity, and that we should increase its size. On the other hand, with a very large model we would expect to see more overfitting, which would manifest itself as a very large gap between the training and validation accuracy.

Tuning. Tuning the hyperparameters and developing intuition for how they affect the final performance is a large part of using Neural Networks, so we want you to get a lot of practice. Below, you should experiment with different values of the various hyperparameters, including hidden layer size, learning rate, numer of training epochs, and regularization strength. You might also consider tuning the momentum and learning rate decay parameters, but you should be able to get good performance using the default values.

Approximate results. You should be aim to achieve a classification accuracy of greater than 50% on the validation set. Our best network gets over 56% on the validation set.

Experiment: You goal in this exercise is to get as good of a result on CIFAR-10 as you can, with a fully-connected Neural Network. For every 1% above 56% on the Test set we will award you with one extra bonus point. Feel free implement your own techniques (e.g. PCA to reduce dimensionality, or adding dropout, or adding features to the solver, etc.).


In [34]:
best_model = None # store the best model into this 

#################################################################################
# TODO: Tune hyperparameters using the validation set. Store your best trained  #
# model in best_model.                                                          #
#                                                                               #
# To help debug your network, it may help to use visualizations similar to the  #
# ones we used above; these visualizations will have significant qualitative    #
# differences from the ones we saw above for the poorly tuned network.          #
#                                                                               #
# Tweaking hyperparameters by hand can be fun, but you might find it useful to  #
# write code to sweep through possible combinations of hyperparameters          #
# automatically like we did on the previous assignment.                         #
#################################################################################
best_model = None
best_hyperparameters = {}
for ne in [5,]: # 8, 11]:
  for rg in [1,]: # 2, 4]:
    for lr in [1e-6,]: #, 1e-5, 1e-4]:
      print("lr :: {0} reg :: {1} num_epoch :: {2}".format(lr, rg, ne))
      model, loss, train_acc, val_acc = trainer.train(X_train, y_train, X_val, y_val,
                                             model, two_layer_net,
                                             num_epochs=ne, reg=rg,
                                             momentum=0.9, learning_rate_decay = 0.95,
                                             learning_rate=lr, verbose=False)
      best_hyperparameters[(lr, rg, ne)] = (train_acc, val_acc)
for key in best_hyperparameters.keys():
  print("{0} :: \n\t{1}\n\t{2}".format(key,
                                       np.mean(best_hyperparameters[key][0]),
                                       np.mean(best_hyperparameters[key][1])))


lr :: 1e-06 reg :: 1 num_epoch :: 5
Finished epoch 0 / 5: cost 2.919704, train: 0.496000, val 0.441000, lr 1.000000e-06
Finished epoch 1 / 5: cost 2.887733, train: 0.471000, val 0.442000, lr 9.500000e-07
Finished epoch 2 / 5: cost 2.917715, train: 0.463000, val 0.441000, lr 9.025000e-07
Finished epoch 3 / 5: cost 2.889199, train: 0.497000, val 0.433000, lr 8.573750e-07
Finished epoch 4 / 5: cost 2.928128, train: 0.500000, val 0.432000, lr 8.145062e-07
Finished epoch 5 / 5: cost 3.051972, train: 0.498000, val 0.445000, lr 7.737809e-07
(1e-06, 1, 5) :: 
	0.4875
	0.439

In [18]:
# visualize the weights
show_net_weights(best_model)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-e1a732baeee8> in <module>()
      1 # visualize the weights
----> 2 show_net_weights(best_model)

<ipython-input-13-66ee06bd02c2> in show_net_weights(model)
      4 
      5 def show_net_weights(model):
----> 6     plt.imshow(visualize_grid(model['W1'].T.reshape(-1, 32, 32, 3), padding=3).astype('uint8'))
      7     plt.gca().axis('off')
      8     plt.show()

TypeError: 'NoneType' object has no attribute '__getitem__'

Run on the test set

When you are done experimenting, you should evaluate your final trained network on the test set.

We will give you extra bonus point for every 1% of accuracy above 56%.


In [ ]:
scores_test = two_layer_net(X_test, best_model)
print 'Test accuracy: ', np.mean(np.argmax(scores_test, axis=1) == y_test)

In [ ]: