# Convolutional Networks

So far we have worked with deep fully-connected networks, using them to explore different optimization strategies and network architectures. Fully-connected networks are a good testbed for experimentation because they are very computationally efficient, but in practice all state-of-the-art results use convolutional networks instead.

First you will implement several layer types that are used in convolutional networks. You will then use these layers to train a convolutional network on the CIFAR-10 dataset.

``````

In :

# As usual, a bit of setup

import numpy as np
import matplotlib.pyplot as plt
from cs231n.classifiers.cnn import *
from cs231n.data_utils import get_CIFAR10_data
from cs231n.layers import *
from cs231n.fast_layers import *
from cs231n.solver import Solver

%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'

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))))

``````
``````

In :

# Load the (preprocessed) CIFAR10 data.

data = get_CIFAR10_data()
for k, v in data.iteritems():
print '%s: ' % k, v.shape

``````
``````

X_val:  (1000, 3, 32, 32)
X_train:  (49000, 3, 32, 32)
X_test:  (1000, 3, 32, 32)
y_val:  (1000,)
y_train:  (49000,)
y_test:  (1000,)

``````

# Convolution: Naive forward pass

The core of a convolutional network is the convolution operation. In the file `cs231n/layers.py`, implement the forward pass for the convolution layer in the function `conv_forward_naive`.

You don't have to worry too much about efficiency at this point; just write the code in whatever way you find most clear.

You can test your implementation by running the following:

``````

In :

x_shape = (2, 3, 4, 4)
w_shape = (3, 3, 4, 4)
x = np.linspace(-0.1, 0.5, num=np.prod(x_shape)).reshape(x_shape)
w = np.linspace(-0.2, 0.3, num=np.prod(w_shape)).reshape(w_shape)
b = np.linspace(-0.1, 0.2, num=3)

conv_param = {'stride': 2, 'pad': 1}
out, _ = conv_forward_naive(x, w, b, conv_param)
correct_out = np.array([[[[[-0.08759809, -0.10987781],
[-0.18387192, -0.2109216 ]],
[[ 0.21027089,  0.21661097],
[ 0.22847626,  0.23004637]],
[[ 0.50813986,  0.54309974],
[ 0.64082444,  0.67101435]]],
[[[-0.98053589, -1.03143541],
[-1.19128892, -1.24695841]],
[[ 0.69108355,  0.66880383],
[ 0.59480972,  0.56776003]],
[[ 2.36270298,  2.36904306],
[ 2.38090835,  2.38247847]]]]])

# Compare your output to ours; difference should be around 1e-8
print 'Testing conv_forward_naive'
print 'difference: ', rel_error(out, correct_out)

``````
``````

Testing conv_forward_naive
difference:  2.21214764175e-08

``````

# Aside: Image processing via convolutions

As fun way to both check your implementation and gain a better understanding of the type of operation that convolutional layers can perform, we will set up an input containing two images and manually set up filters that perform common image processing operations (grayscale conversion and edge detection). The convolution forward pass will apply these operations to each of the input images. We can then visualize the results as a sanity check.

``````

In :

# kitten is wide, and puppy is already square
d = kitten.shape - kitten.shape
kitten_cropped = kitten[:, d/2:-d/2, :]

img_size = 200   # Make this smaller if it runs too slow
x = np.zeros((2, 3, img_size, img_size))
x[0, :, :, :] = imresize(puppy, (img_size, img_size)).transpose((2, 0, 1))
x[1, :, :, :] = imresize(kitten_cropped, (img_size, img_size)).transpose((2, 0, 1))

# Set up a convolutional weights holding 2 filters, each 3x3
w = np.zeros((2, 3, 3, 3))

# The first filter converts the image to grayscale.
# Set up the red, green, and blue channels of the filter.
w[0, 0, :, :] = [[0, 0, 0], [0, 0.3, 0], [0, 0, 0]]
w[0, 1, :, :] = [[0, 0, 0], [0, 0.6, 0], [0, 0, 0]]
w[0, 2, :, :] = [[0, 0, 0], [0, 0.1, 0], [0, 0, 0]]

# Second filter detects horizontal edges in the blue channel.
w[1, 2, :, :] = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]]

# Vector of biases. We don't need any bias for the grayscale
# filter, but for the edge detection filter we want to add 128
# to each output so that nothing is negative.
b = np.array([0, 128])

# Compute the result of convolving each input in x with each filter in w,
# offsetting by b, and storing the results in out.
out, _ = conv_forward_naive(x, w, b, {'stride': 1, 'pad': 1})

def imshow_noax(img, normalize=True):
""" Tiny helper to show images as uint8 and remove axis labels """
if normalize:
img_max, img_min = np.max(img), np.min(img)
img = 255.0 * (img - img_min) / (img_max - img_min)
plt.imshow(img.astype('uint8'))
plt.gca().axis('off')

# Show the original images and the results of the conv operation
plt.subplot(2, 3, 1)
imshow_noax(puppy, normalize=False)
plt.title('Original image')
plt.subplot(2, 3, 2)
imshow_noax(out[0, 0])
plt.title('Grayscale')
plt.subplot(2, 3, 3)
imshow_noax(out[0, 1])
plt.title('Edges')
plt.subplot(2, 3, 4)
imshow_noax(kitten_cropped, normalize=False)
plt.subplot(2, 3, 5)
imshow_noax(out[1, 0])
plt.subplot(2, 3, 6)
imshow_noax(out[1, 1])
plt.show()

``````
``````

``````

# Convolution: Naive backward pass

Implement the backward pass for the convolution operation in the function `conv_backward_naive` in the file `cs231n/layers.py`. Again, you don't need to worry too much about computational efficiency.

When you are done, run the following to check your backward pass with a numeric gradient check.

``````

In :

x = np.random.randn(4, 3, 5, 5)
w = np.random.randn(2, 3, 3, 3)
b = np.random.randn(2,)
dout = np.random.randn(4, 2, 5, 5)
conv_param = {'stride': 1, 'pad': 1}

dx_num = eval_numerical_gradient_array(lambda x: conv_forward_naive(x, w, b, conv_param), x, dout)
dw_num = eval_numerical_gradient_array(lambda w: conv_forward_naive(x, w, b, conv_param), w, dout)
db_num = eval_numerical_gradient_array(lambda b: conv_forward_naive(x, w, b, conv_param), b, dout)

out, cache = conv_forward_naive(x, w, b, conv_param)
dx, dw, db = conv_backward_naive(dout, cache)

# Your errors should be around 1e-9'
print 'Testing conv_backward_naive function'
print 'dx error: ', rel_error(dx, dx_num)
print 'dw error: ', rel_error(dw, dw_num)
print 'db error: ', rel_error(db, db_num)

``````
``````

Testing conv_backward_naive function
dx error:  8.17118428078e-09
dw error:  2.55330546829e-10
db error:  1.77568288299e-11

``````

# Max pooling: Naive forward

Implement the forward pass for the max-pooling operation in the function `max_pool_forward_naive` in the file `cs231n/layers.py`. Again, don't worry too much about computational efficiency.

Check your implementation by running the following:

``````

In :

x_shape = (2, 3, 4, 4)
x = np.linspace(-0.3, 0.4, num=np.prod(x_shape)).reshape(x_shape)
pool_param = {'pool_width': 2, 'pool_height': 2, 'stride': 2}

out, _ = max_pool_forward_naive(x, pool_param)

correct_out = np.array([[[[-0.26315789, -0.24842105],
[-0.20421053, -0.18947368]],
[[-0.14526316, -0.13052632],
[-0.08631579, -0.07157895]],
[[-0.02736842, -0.01263158],
[ 0.03157895,  0.04631579]]],
[[[ 0.09052632,  0.10526316],
[ 0.14947368,  0.16421053]],
[[ 0.20842105,  0.22315789],
[ 0.26736842,  0.28210526]],
[[ 0.32631579,  0.34105263],
[ 0.38526316,  0.4       ]]]])

# Compare your output with ours. Difference should be around 1e-8.
print 'Testing max_pool_forward_naive function:'
print 'difference: ', rel_error(out, correct_out)

``````
``````

Testing max_pool_forward_naive function:
difference:  4.16666651573e-08

``````

# Max pooling: Naive backward

Implement the backward pass for the max-pooling operation in the function `max_pool_backward_naive` in the file `cs231n/layers.py`. You don't need to worry about computational efficiency.

``````

In :

x = np.random.randn(3, 2, 8, 8)
dout = np.random.randn(3, 2, 4, 4)
pool_param = {'pool_height': 2, 'pool_width': 2, 'stride': 2}

dx_num = eval_numerical_gradient_array(lambda x: max_pool_forward_naive(x, pool_param), x, dout)

out, cache = max_pool_forward_naive(x, pool_param)
dx = max_pool_backward_naive(dout, cache)

# Your error should be around 1e-12
print 'Testing max_pool_backward_naive function:'
print 'dx error: ', rel_error(dx, dx_num)

``````
``````

Testing max_pool_backward_naive function:
dx error:  3.27562899588e-12

``````

# Fast layers

Making convolution and pooling layers fast can be challenging. To spare you the pain, we've provided fast implementations of the forward and backward passes for convolution and pooling layers in the file `cs231n/fast_layers.py`.

The fast convolution implementation depends on a Cython extension; to compile it you need to run the following from the `cs231n` directory:

```python setup.py build_ext --inplace
```

The API for the fast versions of the convolution and pooling layers is exactly the same as the naive versions that you implemented above: the forward pass receives data, weights, and parameters and produces outputs and a cache object; the backward pass recieves upstream derivatives and the cache object and produces gradients with respect to the data and weights.

NOTE: The fast implementation for pooling will only perform optimally if the pooling regions are non-overlapping and tile the input. If these conditions are not met then the fast pooling implementation will not be much faster than the naive implementation.

You can compare the performance of the naive and fast versions of these layers by running the following:

``````

In :

from cs231n.fast_layers import conv_forward_fast, conv_backward_fast
from time import time

x = np.random.randn(100, 3, 31, 31)
w = np.random.randn(25, 3, 3, 3)
b = np.random.randn(25,)
dout = np.random.randn(100, 25, 16, 16)
conv_param = {'stride': 2, 'pad': 1}

t0 = time()
out_naive, cache_naive = conv_forward_naive(x, w, b, conv_param)
t1 = time()
out_fast, cache_fast = conv_forward_fast(x, w, b, conv_param)
t2 = time()

print 'Testing conv_forward_fast:'
print 'Naive: %fs' % (t1 - t0)
print 'Fast: %fs' % (t2 - t1)
print 'Speedup: %fx' % ((t1 - t0) / (t2 - t1))
print 'Difference: ', rel_error(out_naive, out_fast)

t0 = time()
dx_naive, dw_naive, db_naive = conv_backward_naive(dout, cache_naive)
t1 = time()
dx_fast, dw_fast, db_fast = conv_backward_fast(dout, cache_fast)
t2 = time()

print '\nTesting conv_backward_fast:'
print 'Naive: %fs' % (t1 - t0)
print 'Fast: %fs' % (t2 - t1)
print 'Speedup: %fx' % ((t1 - t0) / (t2 - t1))
print 'dx difference: ', rel_error(dx_naive, dx_fast)
print 'dw difference: ', rel_error(dw_naive, dw_fast)
print 'db difference: ', rel_error(db_naive, db_fast)

``````
``````

Testing conv_forward_fast:
Naive: 3.847852s
Fast: 0.015002s
Speedup: 256.489058x
Difference:  1.09790981514e-10

Testing conv_backward_fast:
Naive: 4.950332s
Fast: 0.010506s
Speedup: 471.194780x
dx difference:  1.33531761526e-11
dw difference:  1.16801170692e-12
db difference:  3.2139219791e-14

``````
``````

In :

from cs231n.fast_layers import max_pool_forward_fast, max_pool_backward_fast

x = np.random.randn(100, 3, 32, 32)
dout = np.random.randn(100, 3, 16, 16)
pool_param = {'pool_height': 2, 'pool_width': 2, 'stride': 2}

t0 = time()
out_naive, cache_naive = max_pool_forward_naive(x, pool_param)
t1 = time()
out_fast, cache_fast = max_pool_forward_fast(x, pool_param)
t2 = time()

print 'Testing pool_forward_fast:'
print 'Naive: %fs' % (t1 - t0)
print 'fast: %fs' % (t2 - t1)
print 'speedup: %fx' % ((t1 - t0) / (t2 - t1))
print 'difference: ', rel_error(out_naive, out_fast)

t0 = time()
dx_naive = max_pool_backward_naive(dout, cache_naive)
t1 = time()
dx_fast = max_pool_backward_fast(dout, cache_fast)
t2 = time()

print '\nTesting pool_backward_fast:'
print 'Naive: %fs' % (t1 - t0)
print 'speedup: %fx' % ((t1 - t0) / (t2 - t1))
print 'dx difference: ', rel_error(dx_naive, dx_fast)

``````
``````

Testing pool_forward_fast:
Naive: 0.159214s
fast: 0.004454s
speedup: 35.747123x
difference:  0.0

Testing pool_backward_fast:
Naive: 0.589297s
speedup: 46.559252x
dx difference:  0.0

``````

# Convolutional "sandwich" layers

Previously we introduced the concept of "sandwich" layers that combine multiple operations into commonly used patterns. In the file `cs231n/layer_utils.py` you will find sandwich layers that implement a few commonly used patterns for convolutional networks.

``````

In :

from cs231n.layer_utils import conv_relu_pool_forward, conv_relu_pool_backward

x = np.random.randn(2, 3, 16, 16)
w = np.random.randn(3, 3, 3, 3)
b = np.random.randn(3,)
dout = np.random.randn(2, 3, 8, 8)
conv_param = {'stride': 1, 'pad': 1}
pool_param = {'pool_height': 2, 'pool_width': 2, 'stride': 2}

out, cache = conv_relu_pool_forward(x, w, b, conv_param, pool_param)
dx, dw, db = conv_relu_pool_backward(dout, cache)

dx_num = eval_numerical_gradient_array(lambda x: conv_relu_pool_forward(x, w, b, conv_param, pool_param), x, dout)
dw_num = eval_numerical_gradient_array(lambda w: conv_relu_pool_forward(x, w, b, conv_param, pool_param), w, dout)
db_num = eval_numerical_gradient_array(lambda b: conv_relu_pool_forward(x, w, b, conv_param, pool_param), b, dout)

print 'Testing conv_relu_pool'
print 'dx error: ', rel_error(dx_num, dx)
print 'dw error: ', rel_error(dw_num, dw)
print 'db error: ', rel_error(db_num, db)

``````
``````

Testing conv_relu_pool
dx error:  2.95088877797e-08
dw error:  4.27417761597e-09
db error:  6.35668396851e-10

``````
``````

In :

from cs231n.layer_utils import conv_relu_forward, conv_relu_backward

x = np.random.randn(2, 3, 8, 8)
w = np.random.randn(3, 3, 3, 3)
b = np.random.randn(3,)
dout = np.random.randn(2, 3, 8, 8)
conv_param = {'stride': 1, 'pad': 1}

out, cache = conv_relu_forward(x, w, b, conv_param)
dx, dw, db = conv_relu_backward(dout, cache)

dx_num = eval_numerical_gradient_array(lambda x: conv_relu_forward(x, w, b, conv_param), x, dout)
dw_num = eval_numerical_gradient_array(lambda w: conv_relu_forward(x, w, b, conv_param), w, dout)
db_num = eval_numerical_gradient_array(lambda b: conv_relu_forward(x, w, b, conv_param), b, dout)

print 'Testing conv_relu:'
print 'dx error: ', rel_error(dx_num, dx)
print 'dw error: ', rel_error(dw_num, dw)
print 'db error: ', rel_error(db_num, db)

``````
``````

Testing conv_relu:
dx error:  2.68464630729e-08
dw error:  7.27395579694e-10
db error:  3.0152979866e-10

``````

# Three-layer ConvNet

Now that you have implemented all the necessary layers, we can put them together into a simple convolutional network.

Open the file `cs231n/cnn.py` and complete the implementation of the `ThreeLayerConvNet` class. Run the following cells to help you debug:

## Sanity check loss

After you build a new network, one of the first things you should do is sanity check the loss. When we use the softmax loss, we expect the loss for random weights (and no regularization) to be about `log(C)` for `C` classes. When we add regularization this should go up.

``````

In :

model = ThreeLayerConvNet()

N = 50
X = np.random.randn(N, 3, 32, 32)
y = np.random.randint(10, size=N)

print 'Initial loss (no regularization): ', loss

model.reg = 0.5
print 'Initial loss (with regularization): ', loss

``````
``````

Initial loss (no regularization):  2.30258493084
Initial loss (with regularization):  2.50848436421

``````

After the loss looks reasonable, use numeric gradient checking to make sure that your backward pass is correct. When you use numeric gradient checking you should use a small amount of artifical data and a small number of neurons at each layer.

``````

In :

num_inputs = 2
input_dim = (3, 16, 16)
reg = 0.0
num_classes = 10
X = np.random.randn(num_inputs, *input_dim)
y = np.random.randint(num_classes, size=num_inputs)

model = ThreeLayerConvNet(num_filters=3, filter_size=3,
input_dim=input_dim, hidden_dim=7,
dtype=np.float64)
f = lambda _: model.loss(X, y)

``````
``````

W1 max relative error: 1.674134e-02
W2 max relative error: 3.342731e-03
W3 max relative error: 7.967610e-05
b1 max relative error: 2.156803e-05
b2 max relative error: 1.958839e-07
b3 max relative error: 1.800704e-09

``````

## Overfit small data

A nice trick is to train your model with just a few training samples. You should be able to overfit small datasets, which will result in very high training accuracy and comparatively low validation accuracy.

``````

In :

num_train = 100
small_data = {
'X_train': data['X_train'][:num_train],
'y_train': data['y_train'][:num_train],
'X_val': data['X_val'],
'y_val': data['y_val'],
}

model = ThreeLayerConvNet(weight_scale=1e-2)

solver = Solver(model, small_data,
num_epochs=10, batch_size=50,
optim_config={
'learning_rate': 1e-4,
},
verbose=True, print_every=1)
solver.train()

``````
``````

(Iteration 1 / 20) loss: 2.389356
(Epoch 0 / 10) train acc: 0.260000; val_acc: 0.146000
(Iteration 2 / 20) loss: 2.026838
(Epoch 1 / 10) train acc: 0.390000; val_acc: 0.154000
(Iteration 3 / 20) loss: 2.006660
(Iteration 4 / 20) loss: 1.734480
(Epoch 2 / 10) train acc: 0.570000; val_acc: 0.154000
(Iteration 5 / 20) loss: 1.642624
(Iteration 6 / 20) loss: 1.230399
(Epoch 3 / 10) train acc: 0.610000; val_acc: 0.202000
(Iteration 7 / 20) loss: 1.461262
(Iteration 8 / 20) loss: 0.698694
(Epoch 4 / 10) train acc: 0.750000; val_acc: 0.181000
(Iteration 9 / 20) loss: 0.755920
(Iteration 10 / 20) loss: 0.571869
(Epoch 5 / 10) train acc: 0.800000; val_acc: 0.187000
(Iteration 11 / 20) loss: 0.570079
(Iteration 12 / 20) loss: 0.794093
(Epoch 6 / 10) train acc: 0.920000; val_acc: 0.194000
(Iteration 13 / 20) loss: 0.252591
(Iteration 14 / 20) loss: 0.373355
(Epoch 7 / 10) train acc: 0.950000; val_acc: 0.233000
(Iteration 15 / 20) loss: 0.244716
(Iteration 16 / 20) loss: 0.159695
(Epoch 8 / 10) train acc: 0.940000; val_acc: 0.228000
(Iteration 17 / 20) loss: 0.197496
(Iteration 18 / 20) loss: 0.079863
(Epoch 9 / 10) train acc: 0.970000; val_acc: 0.207000
(Iteration 19 / 20) loss: 0.092530
(Iteration 20 / 20) loss: 0.177864
(Epoch 10 / 10) train acc: 0.980000; val_acc: 0.200000

``````

Plotting the loss, training accuracy, and validation accuracy should show clear overfitting:

``````

In :

plt.subplot(2, 1, 1)
plt.plot(solver.loss_history, 'o')
plt.xlabel('iteration')
plt.ylabel('loss')

plt.subplot(2, 1, 2)
plt.plot(solver.train_acc_history, '-o')
plt.plot(solver.val_acc_history, '-o')
plt.legend(['train', 'val'], loc='upper left')
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.show()

``````
``````

``````

## Train the net

By training the three-layer convolutional network for one epoch, you should achieve greater than 40% accuracy on the training set:

The initial settings with learning_rate equal to 1e-3 doesn't work well with me, why? I have to tune the learning_rate to 1e-4 to overfit small data.

``````

In :

model = ThreeLayerConvNet(weight_scale=0.001, hidden_dim=500, reg=0.001)

solver = Solver(model, data,
num_epochs=1, batch_size=50,
optim_config={
'learning_rate': 1e-4,
},
verbose=True, print_every=100)
solver.train()

``````
``````

(Iteration 1 / 980) loss: 2.304588
(Epoch 0 / 1) train acc: 0.090000; val_acc: 0.123000
(Iteration 101 / 980) loss: 1.746251
(Iteration 201 / 980) loss: 1.537676
(Iteration 301 / 980) loss: 1.256284
(Iteration 401 / 980) loss: 1.116357
(Iteration 501 / 980) loss: 1.632303
(Iteration 601 / 980) loss: 1.223760
(Iteration 701 / 980) loss: 1.188374
(Iteration 801 / 980) loss: 1.514778
(Iteration 901 / 980) loss: 1.352297
(Epoch 1 / 1) train acc: 0.574000; val_acc: 0.567000

``````

## Visualize Filters

You can visualize the first-layer convolutional filters from the trained network by running the following:

``````

In :

from cs231n.vis_utils import visualize_grid

grid = visualize_grid(model.params['W1'].transpose(0, 2, 3, 1))
plt.imshow(grid.astype('uint8'))
plt.axis('off')
plt.gcf().set_size_inches(5, 5)
plt.show()

``````
``````

``````

# Spatial Batch Normalization

We already saw that batch normalization is a very useful technique for training deep fully-connected networks. Batch normalization can also be used for convolutional networks, but we need to tweak it a bit; the modification will be called "spatial batch normalization."

Normally batch-normalization accepts inputs of shape `(N, D)` and produces outputs of shape `(N, D)`, where we normalize across the minibatch dimension `N`. For data coming from convolutional layers, batch normalization needs to accept inputs of shape `(N, C, H, W)` and produce outputs of shape `(N, C, H, W)` where the `N` dimension gives the minibatch size and the `(H, W)` dimensions give the spatial size of the feature map.

If the feature map was produced using convolutions, then we expect the statistics of each feature channel to be relatively consistent both between different images and different locations within the same image. Therefore spatial batch normalization computes a mean and variance for each of the `C` feature channels by computing statistics over both the minibatch dimension `N` and the spatial dimensions `H` and `W`.

## Spatial batch normalization: forward

In the file `cs231n/layers.py`, implement the forward pass for spatial batch normalization in the function `spatial_batchnorm_forward`. Check your implementation by running the following:

``````

In :

# Check the training-time forward pass by checking means and variances
# of features both before and after spatial batch normalization

N, C, H, W = 2, 3, 4, 5
x = 4 * np.random.randn(N, C, H, W) + 10

print 'Before spatial batch normalization:'
print '  Shape: ', x.shape
print '  Means: ', x.mean(axis=(0, 2, 3))
print '  Stds: ', x.std(axis=(0, 2, 3))

# Means should be close to zero and stds close to one
gamma, beta = np.ones(C), np.zeros(C)
bn_param = {'mode': 'train'}
out, _ = spatial_batchnorm_forward(x, gamma, beta, bn_param)
print 'After spatial batch normalization:'
print '  Shape: ', out.shape
print '  Means: ', out.mean(axis=(0, 2, 3))
print '  Stds: ', out.std(axis=(0, 2, 3))

# Means should be close to beta and stds close to gamma
gamma, beta = np.asarray([3, 4, 5]), np.asarray([6, 7, 8])
out, _ = spatial_batchnorm_forward(x, gamma, beta, bn_param)
print 'After spatial batch normalization (nontrivial gamma, beta):'
print '  Shape: ', out.shape
print '  Means: ', out.mean(axis=(0, 2, 3))
print '  Stds: ', out.std(axis=(0, 2, 3))

``````
``````

Before spatial batch normalization:
Shape:  (2, 3, 4, 5)
Means:  [ 10.4568251   10.50865473   9.85255352]
Stds:  [ 4.21229079  4.03679974  3.9069199 ]
After spatial batch normalization:
Shape:  (2, 3, 4, 5)
Means:  [ -7.43849426e-16   2.13717932e-16   2.09554596e-16]
Stds:  [ 0.99999972  0.99999969  0.99999967]
After spatial batch normalization (nontrivial gamma, beta):
Shape:  (2, 3, 4, 5)
Means:  [ 6.  7.  8.]
Stds:  [ 2.99999915  3.99999877  4.99999836]

``````
``````

In :

# Check the test-time forward pass by running the training-time
# forward pass many times to warm up the running averages, and then
# checking the means and variances of activations after a test-time
# forward pass.

N, C, H, W = 10, 4, 11, 12

bn_param = {'mode': 'train'}
gamma = np.ones(C)
beta = np.zeros(C)
for t in xrange(50):
x = 2.3 * np.random.randn(N, C, H, W) + 13
spatial_batchnorm_forward(x, gamma, beta, bn_param)
bn_param['mode'] = 'test'
x = 2.3 * np.random.randn(N, C, H, W) + 13
a_norm, _ = spatial_batchnorm_forward(x, gamma, beta, bn_param)

# Means should be close to zero and stds close to one, but will be
# noisier than training-time forward passes.
print 'After spatial batch normalization (test-time):'
print '  means: ', a_norm.mean(axis=(0, 2, 3))
print '  stds: ', a_norm.std(axis=(0, 2, 3))

``````
``````

After spatial batch normalization (test-time):
means:  [ 0.00404204  0.04540302  0.02423007  0.0079857 ]
stds:  [ 1.02384267  0.9665815   1.01211544  1.00353837]

``````

## Spatial batch normalization: backward

In the file `cs231n/layers.py`, implement the backward pass for spatial batch normalization in the function `spatial_batchnorm_backward`. Run the following to check your implementation using a numeric gradient check:

``````

In :

N, C, H, W = 2, 3, 4, 5
x = 5 * np.random.randn(N, C, H, W) + 12
gamma = np.random.randn(C)
beta = np.random.randn(C)
dout = np.random.randn(N, C, H, W)

bn_param = {'mode': 'train'}
fx = lambda x: spatial_batchnorm_forward(x, gamma, beta, bn_param)
fg = lambda a: spatial_batchnorm_forward(x, gamma, beta, bn_param)
fb = lambda b: spatial_batchnorm_forward(x, gamma, beta, bn_param)

_, cache = spatial_batchnorm_forward(x, gamma, beta, bn_param)
dx, dgamma, dbeta = spatial_batchnorm_backward(dout, cache)
print 'dx error: ', rel_error(dx_num, dx)
print 'dgamma error: ', rel_error(da_num, dgamma)
print 'dbeta error: ', rel_error(db_num, dbeta)

``````
``````

dx error:  1.66726608167e-06
dgamma error:  5.18737823532e-12
dbeta error:  3.27574083724e-12

``````

# Experiment!

Experiment and try to get the best performance that you can on CIFAR-10 using a ConvNet. Here are some ideas to get you started:

### Things you should try:

• Filter size: Above we used 7x7; this makes pretty pictures but smaller filters may be more efficient
• Number of filters: Above we used 32 filters. Do more or fewer do better?
• Batch normalization: Try adding spatial batch normalization after convolution layers and vanilla batch normalization aafter affine layers. Do your networks train faster?
• Network architecture: The network above has two layers of trainable parameters. Can you do better with a deeper network? You can implement alternative architectures in the file `cs231n/classifiers/convnet.py`. Some good architectures to try include:
• [conv-relu-pool]xN - conv - relu - [affine]xM - [softmax or SVM]
• [conv-relu-pool]XN - [affine]XM - [softmax or SVM]
• [conv-relu-conv-relu-pool]xN - [affine]xM - [softmax or SVM]

### Tips for training

For each network architecture that you try, you should tune the learning rate and regularization strength. When doing this there are a couple important things to keep in mind:

• If the parameters are working well, you should see improvement within a few hundred iterations
• Remember the course-to-fine approach for hyperparameter tuning: start by testing a large range of hyperparameters for just a few training iterations to find the combinations of parameters that are working at all.
• Once you have found some sets of parameters that seem to work, search more finely around these parameters. You may need to train for more epochs.

### Going above and beyond

If you are feeling adventurous there are many other features you can implement to try and improve your performance. You are not required to implement any of these; however they would be good things to try for extra credit.

• Alternative activation functions such as leaky ReLU, parametric ReLU, or MaxOut.
• Model ensembles
• Data augmentation

If you do decide to implement something extra, clearly describe it in the "Extra Credit Description" cell below.

### What we expect

At the very least, you should be able to train a ConvNet that gets at least 65% accuracy on the validation set. This is just a lower bound - if you are careful it should be possible to get accuracies much higher than that! Extra credit points will be awarded for particularly high-scoring models or unique approaches.

You should use the space below to experiment and train your network. The final cell in this notebook should contain the training, validation, and test set accuracies for your final trained network. In this notebook you should also write an explanation of what you did, any additional features that you implemented, and any visualizations or graphs that you make in the process of training and evaluating your network.

Have fun and happy training!

``````

In :

# Train a really good model on CIFAR-10
# FunConvNet
print("sanity check")
model = FunConvNet(conv_params=[
{
'num_filters': 3, 'filter_size': 3,
'stride': 1, #'pad': (filter_size - 1) / 2
}
],
hidden_dims=, num_classes=10,
dropout=0.9, use_batchnorm=True,
weight_scale=1e-2, reg=0.0,
dtype=np.float32, seed=None)

N = 50
X = np.random.randn(N, 3, 32, 32)
y = np.random.randint(10, size=N)

print 'Initial loss (no regularization): ', loss
model.reg = 0.5
print 'Initial loss (with regularization): ', loss

``````
``````

sanity check
Initial loss (no regularization):  2.30332092285
Initial loss (with regularization):  3.27055983137

``````
``````

In :

# FIXME: gradient check fails here
num_inputs = 2
input_dim = (3, 16, 16)
reg = 0.0
num_classes = 10
X = np.random.randn(num_inputs, *input_dim)
y = np.random.randint(num_classes, size=num_inputs)

model = FunConvNet(input_dim=input_dim,
conv_params=[
{
'num_filters': 3, 'filter_size': 3,
'stride': 1, #'pad': (filter_size - 1) / 2
}
],
hidden_dims=, num_classes=10,
dropout=0.7, use_batchnorm=False,
weight_scale=1e-2, reg=0.0,
dtype=np.float32, seed=None)
f = lambda _: model.loss(X, y)
f, model.params[param_name], verbose=False, h=1e-6)
print '%s max relative error: %e' % (

``````
``````

W1 max relative error: 1.000000e+00
W2 max relative error: 1.000000e+00
W3 max relative error: 1.000000e+00
b1 max relative error: 9.999700e-01
b2 max relative error: 1.000000e+00
b3 max relative error: 1.000000e+00

``````
``````

In :

print ("overfit small data")
model_param = dict(conv_params=[
{
'num_filters': 32, 'filter_size': 5,
'stride': 1, #'pad': (filter_size - 1) / 2
},
{
'num_filters': 64, 'filter_size': 3,
'stride': 1, #'pad': (filter_size - 1) / 2
},
],
hidden_dims=, num_classes=10,
dropout=0, use_batchnorm=True,
weight_scale=1e-2, reg=0.0,
dtype=np.float32, seed=None)

solver_param = dict(num_epochs=10, batch_size=50,
optim_config={
'learning_rate': 1e-4,
},
verbose=True, print_every=10)

model_ex = FunConvNet(**model_param)

solver_ex = Solver(model_ex, small_data, **solver_param)
solver_ex.train()

print 'without batch normalization or dropout'
model = FunConvNet(**model_param)

solver = Solver(model, small_data, **solver_param)
solver.train()

``````
``````

overfit small data
(Iteration 1 / 20) loss: 2.328506
(Epoch 0 / 10) train acc: 0.180000; val_acc: 0.122000
(Epoch 1 / 10) train acc: 0.160000; val_acc: 0.120000
(Epoch 2 / 10) train acc: 0.400000; val_acc: 0.179000
(Epoch 3 / 10) train acc: 0.500000; val_acc: 0.164000
(Epoch 4 / 10) train acc: 0.500000; val_acc: 0.174000
(Epoch 5 / 10) train acc: 0.370000; val_acc: 0.150000
(Iteration 11 / 20) loss: 1.251969
(Epoch 6 / 10) train acc: 0.590000; val_acc: 0.201000
(Epoch 7 / 10) train acc: 0.750000; val_acc: 0.246000
(Epoch 8 / 10) train acc: 0.820000; val_acc: 0.260000
(Epoch 9 / 10) train acc: 0.840000; val_acc: 0.248000
(Epoch 10 / 10) train acc: 0.850000; val_acc: 0.211000
without batch normalization or dropout
(Iteration 1 / 20) loss: 2.322073
(Epoch 0 / 10) train acc: 0.160000; val_acc: 0.119000
(Epoch 1 / 10) train acc: 0.160000; val_acc: 0.119000
(Epoch 2 / 10) train acc: 0.270000; val_acc: 0.094000
(Epoch 3 / 10) train acc: 0.460000; val_acc: 0.174000
(Epoch 4 / 10) train acc: 0.470000; val_acc: 0.186000
(Epoch 5 / 10) train acc: 0.590000; val_acc: 0.187000
(Iteration 11 / 20) loss: 1.116946
(Epoch 6 / 10) train acc: 0.640000; val_acc: 0.221000
(Epoch 7 / 10) train acc: 0.770000; val_acc: 0.207000
(Epoch 8 / 10) train acc: 0.780000; val_acc: 0.207000
(Epoch 9 / 10) train acc: 0.850000; val_acc: 0.240000
(Epoch 10 / 10) train acc: 0.920000; val_acc: 0.243000

``````
``````

In :

# (Epoch 5 / 5) train acc: 0.881000; val_acc: 0.800000; test accuracy: 0.800000
model_hyperparameters = dict(conv_params=[
{
'num_filters': 64, 'filter_size': 5,
'stride': 1, #'pad': (filter_size - 1) / 2
},
{
'num_filters': 128, 'filter_size': 3,
'stride': 1, #'pad': (filter_size - 1) / 2
},
{
'num_filters': 256, 'filter_size': 3,
'stride': 1, #'pad': (filter_size - 1) / 2
},
{
'num_filters': 512, 'filter_size': 3,
'stride': 1, #'pad': (filter_size - 1) / 2
},
],
hidden_dims=[1024, 512], num_classes=10,
dropout=0, use_batchnorm=True,
weight_scale=1e-2, reg=1e-3,
dtype=np.float32, seed=None)
# change num_epochs to at least 5 to get around 80% test accuracy
solver_hyperparameters = dict(num_epochs=5, batch_size=50,
optim_config={
'learning_rate': 1e-4,
},
verbose=True, print_every=100)

model = FunConvNet(**model_hyperparameters)
solver = Solver(model, data, **solver_hyperparameters)
solver.train()

``````
``````

(Iteration 1 / 4900) loss: 2.515417
(Epoch 0 / 5) train acc: 0.108000; val_acc: 0.107000
(Iteration 101 / 4900) loss: 2.021231
(Iteration 201 / 4900) loss: 1.399539
(Iteration 301 / 4900) loss: 1.448887
(Iteration 401 / 4900) loss: 1.269396
(Iteration 501 / 4900) loss: 1.218577
(Iteration 601 / 4900) loss: 1.131239
(Iteration 701 / 4900) loss: 1.388965
(Iteration 801 / 4900) loss: 1.025385
(Iteration 901 / 4900) loss: 0.996903
(Epoch 1 / 5) train acc: 0.681000; val_acc: 0.666000
(Iteration 1001 / 4900) loss: 1.166503
(Iteration 1101 / 4900) loss: 0.792987
(Iteration 1201 / 4900) loss: 0.708963
(Iteration 1301 / 4900) loss: 1.049529
(Iteration 1401 / 4900) loss: 0.968234
(Iteration 1501 / 4900) loss: 0.880891
(Iteration 1601 / 4900) loss: 0.996185
(Iteration 1701 / 4900) loss: 1.147132
(Iteration 1801 / 4900) loss: 0.686450
(Iteration 1901 / 4900) loss: 0.799620
(Epoch 2 / 5) train acc: 0.759000; val_acc: 0.724000
(Iteration 2001 / 4900) loss: 0.766748
(Iteration 2101 / 4900) loss: 0.779377
(Iteration 2201 / 4900) loss: 0.845572
(Iteration 2301 / 4900) loss: 0.721645
(Iteration 2401 / 4900) loss: 0.970272
(Iteration 2501 / 4900) loss: 0.514922
(Iteration 2601 / 4900) loss: 0.558132
(Iteration 2701 / 4900) loss: 0.683264
(Iteration 2801 / 4900) loss: 0.640164
(Iteration 2901 / 4900) loss: 0.609870
(Epoch 3 / 5) train acc: 0.800000; val_acc: 0.775000
(Iteration 3001 / 4900) loss: 0.609490
(Iteration 3101 / 4900) loss: 0.610374
(Iteration 3201 / 4900) loss: 0.655179
(Iteration 3301 / 4900) loss: 0.418024
(Iteration 3401 / 4900) loss: 0.782864
(Iteration 3501 / 4900) loss: 0.545809
(Iteration 3601 / 4900) loss: 0.503284
(Iteration 3701 / 4900) loss: 0.737549
(Iteration 3801 / 4900) loss: 0.584542
(Iteration 3901 / 4900) loss: 0.626441
(Epoch 4 / 5) train acc: 0.830000; val_acc: 0.765000
(Iteration 4001 / 4900) loss: 0.835663
(Iteration 4101 / 4900) loss: 0.573013
(Iteration 4201 / 4900) loss: 0.646219
(Iteration 4301 / 4900) loss: 0.423381
(Iteration 4401 / 4900) loss: 0.375198
(Iteration 4501 / 4900) loss: 0.432864
(Iteration 4601 / 4900) loss: 0.490017
(Iteration 4701 / 4900) loss: 0.304033
(Iteration 4801 / 4900) loss: 0.650676
(Epoch 5 / 5) train acc: 0.892000; val_acc: 0.781000

``````
``````

In :

plt.subplot(2, 1, 1)
plt.plot(solver.loss_history)
plt.title('loss history')
plt.xlabel('iteration')
plt.ylabel('loss')

plt.subplot(2, 1, 2)
plt.plot(solver.train_acc_history, '-o')
plt.plot(solver.val_acc_history, '-o')
plt.legend(['train', 'val'], loc='upper left')
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.show()

``````
``````

``````
``````

In :

y_test_pred = np.argmax(model.loss(data['X_test']), axis=-1)
acc_test = np.mean(y_test_pred == data['y_test'])
print 'test accuracy: %f' % acc_test

``````
``````

test accuracy: 0.790000

``````
``````

In :

import pickle
import copy

best_solver = None
best_val_acc = None
file_pickle = 'best_cnn.pkl'
try:
with open(file_pickle, 'rb') as f:
best_acc_test = np.mean(np.argmax(
best_solver.model.loss(data['X_test']), axis=-1)== data['y_test'])
print 'best_acc_test so far: %f' % (best_acc_test)
except Exception as e:
print('Unpickling or testing failed!', e)
if not best_solver or acc_test >= best_acc_test:
print 'pickling new model and solver to file %s' % (file_pickle,)
# shallow copy the solver object
solver_pkl = copy.copy(solver)
solver_pkl.X_train, solver_pkl.X_val, solver_pkl.y_train, solver_pkl.y_val = \
None, None, None, None
with open(file_pickle, 'wb') as f:
pickle.dump(solver_pkl, f)

``````
``````

best_acc_test so far: 0.800000

``````

# Extra Credit Description

If you implement any additional features for extra credit, clearly describe them here with pointers to any code in this or other files if applicable.