You've written a lot of code in this assignment to provide a whole host of neural network functionality. Dropout, Batch Norm, and 2D convolutions are some of the workhorses of deep learning in computer vision. You've also worked hard to make your code efficient and vectorized.

For the last part of this assignment, though, we're going to leave behind your beautiful codebase and instead migrate to one of two popular deep learning frameworks: in this instance, TensorFlow (or PyTorch, if you switch over to that notebook)

TensorFlow is a system for executing computational graphs over Tensor objects, with native support for performing backpropogation for its Variables. In it, we work with Tensors which are n-dimensional arrays analogous to the numpy ndarray.

- Our code will now run on GPUs! Much faster training. Writing your own modules to run on GPUs is beyond the scope of this class, unfortunately.
- We want you to be ready to use one of these frameworks for your project so you can experiment more efficiently than if you were writing every feature you want to use by hand.
- We want you to stand on the shoulders of giants! TensorFlow and PyTorch are both excellent frameworks that will make your lives a lot easier, and now that you understand their guts, you are free to use them :)
- We want you to be exposed to the sort of deep learning code you might run into in academia or industry.

TensorFlow has many excellent tutorials available, including those from Google themselves.

Otherwise, this notebook will walk you through much of what you need to do to train models in TensorFlow. See the end of the notebook for some links to helpful tutorials if you want to learn more or need further clarification on topics that aren't fully explained here.

```
In [1]:
```import tensorflow as tf
import numpy as np
import math
import timeit
import matplotlib.pyplot as plt
%matplotlib inline

```
```

```
In [2]:
```from cs231n.data_utils import load_CIFAR10
def get_CIFAR10_data(num_training=49000, num_validation=1000, num_test=10000):
"""
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
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)

```
```

. Remember that our image data is initially N x H x W x C, where:

- N is the number of datapoints
- H is the height of each image in pixels
- W is the height of each image in pixels
- C is the number of channels (usually 3: R, G, B)

This is the right way to represent the data when we are doing something like a 2D convolution, which needs spatial understanding of where the pixels are relative to each other. When we input image data into fully connected affine layers, however, we want each data example to be represented by a single vector -- it's no longer useful to segregate the different channels, rows, and columns of the data.

The first step to training your own model is defining its architecture.

Here's an example of a convolutional neural network defined in TensorFlow -- try to understand what each line is doing, remembering that each layer is composed upon the previous layer. We haven't trained anything yet - that'll come next - for now, we want you to understand how everything gets set up.

In that example, you see 2D convolutional layers (Conv2d), ReLU activations, and fully-connected layers (Linear). You also see the Hinge loss function, and the Adam optimizer being used.

Make sure you understand why the parameters of the Linear layer are 5408 and 10.

In TensorFlow, much like in our previous notebooks, we'll first specifically initialize our variables, and then our network model.

```
In [3]:
```# clear old variables
tf.reset_default_graph()
# setup input (e.g. the data that changes every batch)
# The first dim is None, and gets sets automatically based on batch size fed in
X = tf.placeholder(tf.float32, [None, 32, 32, 3])
y = tf.placeholder(tf.int64, [None])
is_training = tf.placeholder(tf.bool)
def simple_model(X,y):
# define our weights (e.g. init_two_layer_convnet)
# setup variables
Wconv1 = tf.get_variable("Wconv1", shape=[7, 7, 3, 32])
bconv1 = tf.get_variable("bconv1", shape=[32])
W1 = tf.get_variable("W1", shape=[5408, 10])
b1 = tf.get_variable("b1", shape=[10])
# define our graph (e.g. two_layer_convnet)
a1 = tf.nn.conv2d(X, Wconv1, strides=[1,2,2,1], padding='VALID') + bconv1
h1 = tf.nn.relu(a1)
h1_flat = tf.reshape(h1, [-1, 5408])
y_out = tf.matmul(h1_flat, W1) + b1
return y_out
y_out = simple_model(X, y)
# define our loss
total_loss = tf.losses.hinge_loss(tf.one_hot(y, 10), logits=y_out)
mean_loss = tf.reduce_mean(total_loss)
# define our optimizer
optimizer = tf.train.AdamOptimizer(5e-4) # select optimizer and set learning rate
train_step = optimizer.minimize(mean_loss)

TensorFlow supports many other layer types, loss functions, and optimizers - you will experiment with these next. Here's the official API documentation for these (if any of the parameters used above were unclear, this resource will also be helpful).

- Layers, Activations, Loss functions : https://www.tensorflow.org/api_guides/python/nn
- Optimizers: https://www.tensorflow.org/api_guides/python/train#Optimizers
- BatchNorm: https://www.tensorflow.org/api_docs/python/tf/layers/batch_normalization

While we have defined a graph of operations above, in order to execute TensorFlow Graphs, by feeding them input data and computing the results, we first need to create a `tf.Session`

object. A session encapsulates the control and state of the TensorFlow runtime. For more information, see the TensorFlow Getting started guide.

Optionally we can also specify a device context such as `/cpu:0`

or `/gpu:0`

. For documentation on this behavior see this TensorFlow guide

You should see a validation loss of around 0.4 to 0.6 and an accuracy of 0.30 to 0.35 below

```
In [4]:
```def run_model(session, predict, loss_val, Xd, yd,
epochs=1, batch_size=64, print_every=100,
training=None, plot_losses=False):
# have tensorflow compute accuracy
correct_prediction = tf.equal(tf.argmax(predict, axis=1), y)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# shuffle indicies
train_indicies = np.arange(Xd.shape[0])
np.random.shuffle(train_indicies)
training_now = training is not None
# setting up variables we want to compute (and optimizing)
# if we have a training function, add that to things we compute
variables = [mean_loss, correct_prediction, accuracy]
if training_now:
variables[-1] = training
# counter
iter_cnt = 0
for e in range(epochs):
# keep track of losses and accuracy
correct = 0
losses = []
# make sure we iterate over the dataset once
for i in range(Xd.shape[0] // batch_size):
# generate indicies for the batch
start_idx = (i * batch_size) % Xd.shape[0]
idx = train_indicies[start_idx:start_idx + batch_size]
# create a feed dictionary for this batch
feed_dict = {
X: Xd[idx,:],
y: yd[idx],
is_training: training_now,
}
# get batch size
actual_batch_size = yd[idx].shape[0]
# have tensorflow compute loss and correct predictions
# and (if given) perform a training step
loss, corr, _ = session.run(variables, feed_dict=feed_dict)
# aggregate performance stats
losses.append(loss * actual_batch_size)
correct += np.sum(corr)
# print every now and then
if training_now and (iter_cnt % print_every) == 0:
print("Iteration {0}: with minibatch training loss = {1:.3g} and accuracy of {2:.2g}"\
.format(iter_cnt,loss,np.sum(corr)/actual_batch_size))
iter_cnt += 1
total_correct = correct / Xd.shape[0]
total_loss = np.sum(losses) / Xd.shape[0]
print("Epoch {2}, Overall loss = {0:.3g} and accuracy of {1:.3g}"\
.format(total_loss,total_correct,e+1))
if plot_losses:
plt.plot(losses)
plt.grid(True)
plt.title('Epoch {} Loss'.format(e+1))
plt.xlabel('minibatch number')
plt.ylabel('minibatch loss')
plt.show()
return total_loss,total_correct
with tf.Session() as sess:
with tf.device("/cpu:0"): #"/cpu:0" or "/gpu:0"
sess.run(tf.global_variables_initializer())
print('Training')
run_model(sess, y_out, mean_loss, X_train, y_train, 1, 64, 100, train_step, True)
print('Validation')
run_model(sess, y_out, mean_loss, X_val, y_val, 1, 64)

```
```

In this section, we're going to specify a model for you to construct. The goal here isn't to get good performance (that'll be next), but instead to get comfortable with understanding the TensorFlow documentation and configuring your own model.

Using the code provided above as guidance, and using the following TensorFlow documentation, specify a model with the following architecture:

- 7x7 Convolutional Layer with 32 filters and stride of 1
- ReLU Activation Layer
- Spatial Batch Normalization Layer (trainable parameters, with scale and centering)
- 2x2 Max Pooling layer with a stride of 2
- Affine layer with 1024 output units
- ReLU Activation Layer
- Affine layer from 1024 input units to 10 outputs

```
In [6]:
```# clear old variables
tf.reset_default_graph()
# define our input (e.g. the data that changes every batch)
# The first dim is None, and gets sets automatically based on batch size fed in
X = tf.placeholder(tf.float32, [None, 32, 32, 3])
y = tf.placeholder(tf.int64, [None])
is_training = tf.placeholder(tf.bool)
# define model
def complex_model(X,y,is_training):
# setup variables
Wconv1 = tf.get_variable("Wconv1", shape=[7, 7, 3, 32])
bconv1 = tf.get_variable("bconv1", shape=[32])
W1 = tf.get_variable("W1", shape=[8192, 1024])
b1 = tf.get_variable("b1", shape=[1024])
W2 = tf.get_variable("W2", shape=[1024, 10])
b2 = tf.get_variable("b2", shape=[10])
# define our graph (e.g. two_layer_convnet)
a1 = tf.nn.conv2d(X, Wconv1, strides=[1, 1, 1, 1], padding='SAME') + bconv1
h1 = tf.nn.relu(a1)
h1_normalized = tf.layers.batch_normalization(h1)
h1_pooled = tf.layers.MaxPooling2D(pool_size=2,
strides=2,
padding='VALID')(h1_normalized)
h1_flat = tf.reshape(h1_pooled, [-1, 8192])
a2 = tf.matmul(h1_flat, W1) + b1
h2 = tf.nn.relu(a2)
y_out = tf.matmul(h2, W2) + b2
return y_out
y_out = complex_model(X, y, is_training)

```
In [7]:
```# Now we're going to feed a random batch into the model
# and make sure the output is the right size
x = np.random.randn(64, 32, 32, 3)
with tf.Session() as sess:
with tf.device("/cpu:0"): #"/cpu:0" or "/gpu:0"
tf.global_variables_initializer().run()
ans = sess.run(y_out,feed_dict={X:x,is_training:True})
%timeit sess.run(y_out,feed_dict={X:x,is_training:True})
print(ans.shape)
print(np.array_equal(ans.shape, np.array([64, 10])))

```
```

You should see the following from the run above

`(64, 10)`

`True`

Now, we're going to try and start the model under the GPU device, the rest of the code stays unchanged and all our variables and operations will be computed using accelerated code paths. However, if there is no GPU, we get a Python exception and have to rebuild our graph. On a dual-core CPU, you might see around 50-80ms/batch running the above, while the Google Cloud GPUs (run below) should be around 2-5ms/batch.

```
In [8]:
```try:
with tf.Session() as sess:
with tf.device("/gpu:0") as dev: #"/cpu:0" or "/gpu:0"
tf.global_variables_initializer().run()
ans = sess.run(y_out,feed_dict={X:x,is_training:True})
%timeit sess.run(y_out,feed_dict={X:x,is_training:True})
except tf.errors.InvalidArgumentError:
print("no gpu found, please use Google Cloud if you want GPU acceleration")
# rebuild the graph
# trying to start a GPU throws an exception
# and also trashes the original graph
tf.reset_default_graph()
X = tf.placeholder(tf.float32, [None, 32, 32, 3])
y = tf.placeholder(tf.int64, [None])
is_training = tf.placeholder(tf.bool)
y_out = complex_model(X,y,is_training)

```
```

Now that you've seen how to define a model and do a single forward pass of some data through it, let's walk through how you'd actually train one whole epoch over your training data (using the complex_model you created provided above).

Make sure you understand how each TensorFlow function used below corresponds to what you implemented in your custom neural network implementation.

First, set up an **RMSprop optimizer** (using a 1e-3 learning rate) and a **cross-entropy loss** function. See the TensorFlow documentation for more information

- Layers, Activations, Loss functions : https://www.tensorflow.org/api_guides/python/nn
- Optimizers: https://www.tensorflow.org/api_guides/python/train#Optimizers

```
In [13]:
```# Inputs
# y_out: is what your model computes
# y: is your TensorFlow variable with label information
# Outputs
# mean_loss: a TensorFlow variable (scalar) with numerical loss
# optimizer: a TensorFlow optimizer
# This should be ~3 lines of code!
total_loss = tf.losses.softmax_cross_entropy(tf.one_hot(y, 10), logits=y_out)
mean_loss = tf.reduce_mean(total_loss)
optimizer = tf.train.RMSPropOptimizer(1e-3)

```
In [14]:
```# batch normalization in tensorflow requires this extra dependency
extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(extra_update_ops):
train_step = optimizer.minimize(mean_loss)

```
In [15]:
```sess = tf.Session()
sess.run(tf.global_variables_initializer())
print('Training')
run_model(sess, y_out, mean_loss, X_train, y_train, 1, 64, 100, train_step)

```
Out[15]:
```

```
In [16]:
```print('Validation')
run_model(sess, y_out, mean_loss, X_val, y_val, 1, 64)

```
Out[16]:
```

**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?**Pooling vs Strided Convolution**: Do you use max pooling or just stride convolutions?**Batch normalization**: Try adding spatial batch normalization after convolution layers and vanilla batch normalization after affine layers. Do your networks train faster?**Network architecture**: The network above has two layers of trainable parameters. Can you do better with a deep network? Good architectures to try include:- [conv-relu-pool]xN -> [affine]xM -> [softmax or SVM]
- [conv-relu-conv-relu-pool]xN -> [affine]xM -> [softmax or SVM]
- [batchnorm-relu-conv]xN -> [affine]xM -> [softmax or SVM]

**Use TensorFlow Scope**: Use TensorFlow scope and/or tf.layers to make it easier to write deeper networks. See this tutorial for how to use`tf.layers`

.**Use Learning Rate Decay**: As the notes point out, decaying the learning rate might help the model converge. Feel free to decay every epoch, when loss doesn't change over an entire epoch, or any other heuristic you find appropriate. See the Tensorflow documentation for learning rate decay.**Global Average Pooling**: Instead of flattening and then having multiple affine layers, perform convolutions until your image gets small (7x7 or so) and then perform an average pooling operation to get to a 1x1 image picture (1, 1 , Filter#), which is then reshaped into a (Filter#) vector. This is used in Google's Inception Network (See Table 1 for their architecture).**Regularization**: Add l2 weight regularization, or perhaps use Dropout as in the TensorFlow MNIST tutorial

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 coarse-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.
- You should use the validation set for hyperparameter search, and we'll save the test set for evaluating your architecture on the best parameters as selected by the validation set.

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 update steps: For the assignment we implemented SGD+momentum, RMSprop, and Adam; you could try alternatives like AdaGrad or AdaDelta.
- Alternative activation functions such as leaky ReLU, parametric ReLU, ELU, or MaxOut.
- Model ensembles
- Data augmentation
- New Architectures
- ResNets where the input from the previous layer is added to the output.
- DenseNets where inputs into previous layers are concatenated together.
- This blog has an in-depth overview

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

At the very least, you should be able to train a ConvNet that gets at **>= 70% 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 and validation set accuracies for your final trained network.

Have fun and happy training!

```
In [109]:
```# Feel free to play with this cell
def conv_relu_bn(X, filters_shape, stride, scope_name, regulizers):
"""
Define block of layers
Conv2d - BN - Leaky-ReLU
Args:
X(tf.tensor): input tensor (N, H, W, F)
filters_shape(tuple): filtes shapes (H, W, filters_num)
stride(int): strides for convolution
scope_name(str): prefix for scope name
regulizers(list): list of tf.tensors regulizers
Returns:
out(tf.tensor): output tensor(N, H, W, filters_num)
"""
_, H, W, F = X.shape
filter_H, filter_W, filters_num = filters_shape
conv_weights = tf.get_variable(scope_name + '/conv_weights', shape=[filter_H, filter_W, F, filters_num])
conv_bias = tf.get_variable(scope_name + '/bias_weights', shape=[filters_num])
activation = tf.nn.conv2d(X,
conv_weights,
strides=[1, stride, stride, 1],
padding='SAME')
activation += conv_bias
activation_normilized = tf.layers.batch_normalization(activation)
out = tf.nn.leaky_relu(activation_normilized, alpha=0.4)
regulizers.append(tf.nn.l2_loss(conv_weights))
return out
def resnext_block(X, branch_num, scope_name, regulizers):
"""
Define ResNeXt network block
Args:
X(tf.tensor): 4-D tensor (N, H, W, F) where
N - batch size, F - filter number
branch_num(int): numbers of branches
scope_name(str): name of scope
regulizers(list): list of tf.tensors regulizers
Returns:
out(tf.tensor): 4-D tensor (N, H, W, F)
"""
_, H, W, F = X.shape
assert F % branch_num, 'Number of filters in inception block must divide by branch_num'
scope_name = scope_name + '/branch_%d/conv_%d'
branch_filt = F // branch_num
conv1 = [conv_relu_bn(X, (1, 1, branch_filt), 1, scope_name % (idx, 1), regulizers)
for idx in range(branch_num)]
conv2 = [conv_relu_bn(conv1[idx], (3, 3, branch_filt), 1, scope_name % (idx, 2), regulizers)
for idx in range(branch_num)]
conv3 = [conv_relu_bn(conv2[idx], (1, 1, F), 1, scope_name % (idx, 3), regulizers)
for idx in range(branch_num)]
combined_convs = tf.add_n(conv3)
out = combined_convs + X
return out
def my_model(X, y, is_training):
regulizers = []
x = X
x = conv_relu_bn(X, filters_shape=[3, 3, 64],
stride=1, scope_name='conv1',
regulizers=regulizers)
x = resnext_block(x, branch_num=4,
scope_name='resnext_1',
regulizers=regulizers)
x = conv_relu_bn(X, filters_shape=[3, 3, 128],
stride=2, scope_name='conv2',
regulizers=regulizers)
x = resnext_block(x, branch_num=4,
scope_name='resnext_2',
regulizers=regulizers)
x = conv_relu_bn(X, filters_shape=[3, 3, 256],
stride=2, scope_name='conv3',
regulizers=regulizers)
x = resnext_block(x, branch_num=4,
scope_name='resnext_3',
regulizers=regulizers)
x = conv_relu_bn(X, filters_shape=[3, 3, 512],
stride=2, scope_name='conv4',
regulizers=regulizers)
x = resnext_block(x, branch_num=4,
scope_name='resnext_4',
regulizers=regulizers)
x = tf.layers.average_pooling2d(x, pool_size=(x.shape[1], x.shape[2]), strides=1)
x = tf.reshape(x, [-1, 512])
W1 = tf.get_variable("W1", shape=[512, 10])
b1 = tf.get_variable("b1", shape=[10])
regulizers.append(tf.nn.l2_loss(W1))
out = tf.matmul(x, W1) + b1
return out, regulizers
tf.reset_default_graph()
X = tf.placeholder(tf.float32, [None, 32, 32, 3])
y = tf.placeholder(tf.int64, [None])
is_training = tf.placeholder(tf.bool)
REG_COEF = 5e-5
y_out, regulizers = my_model(X, y, is_training)
total_loss = tf.losses.softmax_cross_entropy(tf.one_hot(y, 10), logits=y_out)
mean_loss = tf.reduce_mean(total_loss + REG_COEF * tf.add_n(regulizers))
# batch normalization in tensorflow requires this extra dependency
extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(extra_update_ops):
global_step = tf.Variable(0, trainable=False)
learning_rate = tf.train.natural_exp_decay(learning_rate=3e-3,
global_step=global_step,
decay_steps=10000,
decay_rate=0.5)
optimizer = tf.train.AdamOptimizer(learning_rate)
train_step = optimizer.minimize(mean_loss, global_step=global_step)

```
In [110]:
```# Feel free to play with this cell
# This default code creates a session
# and trains your model for 10 epochs
# then prints the validation set accuracy
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for _ in range(10):
print('Training: ', end='')
run_model(sess, y_out, mean_loss, X_train, y_train,
epochs=5, batch_size=256, print_every=100000,
training=train_step, plot_losses=False)
print('Validation: ', end='')
run_model(sess, y_out, mean_loss, X_val, y_val, 1, 64)

```
```

```
In [111]:
```# Test your model here, and make sure
# the output of this cell is the accuracy
# of your best model on the training and val sets
# We're looking for >= 70% accuracy on Validation
print('Training')
run_model(sess,y_out,mean_loss,X_train,y_train,1,64)
print('Validation')
run_model(sess,y_out,mean_loss,X_val,y_val,1,64)

```
Out[111]:
```

*Tell us here*

```
In [112]:
```print('Test')
run_model(sess,y_out,mean_loss,X_test,y_test,1,64)

```
Out[112]:
```