### Riferimenti bibliografici:

* Hastie, T.; Tibshirani, R. & Friedman, J. (2009), [The Elements of Statistical Learning](https://web.stanford.edu/~hastie/ElemStatLearn/).

# Componenti e curve principali

## Indice

1. [Analisi delle componenti principali (PCA)](#pca)

    1.1 [Relazione tra PCA e decomposizione a valori singolari (SVD)](#svd)

    1.2 [Studiare l'effetto della centratura di X](#centratura)

2. [Spirale logaritmica](#spirale)

    2.1 [Generare le osservazioni](#generare)

    2.2 [Visualizzare le osservazioni](#visualizzare)

3. [Curve principali](#curve_principali)

```python
import inspect
import math
import matplotlib.pyplot as plt
import numpy as np

%load_ext autoreload
%autoreload 2
```

# 1. Analisi delle componenti principali (PCA)

## 1.1 Relazione tra PCA e decomposizione a valori singolari (SVD)

```python
np.random.seed(42)

x1 = np.random.uniform(3, 6, size=100)
x2 = 10 - x1 + np.random.normal(scale=0.5, size=100)
X = np.c_[x1, x2]
```

$\mathbf{X}$ è una matrice $N\times p$.

$\mathbf{X} = \mathbf{U}\mathbf{D}\mathbf{V}^\top$ è detta *decomposizione a valori singolari* (SVD).

$\mathbf{U}$ è una matrice $N\times p$ ortogonale le cui colonne vengono chiamate *vettori singolari sinistri*.

$\mathbf{V}$ è una matrice $p\times p$ ortogonale le cui colonne vengono chiamate *vettori singolari destri*.

$\mathbf{D}$ è una matrice $p\times p$ diagonale i cui valori vengono chiamati *valori singolari* e corrispondono alla radice quadrata degli autovalori di $\mathbf{X}^\top\mathbf{X}$.

$\mathbf{U}\mathbf{D}$ vengono dette le *componenti principali* di $\mathbf{X}$.

$\mathbf{V}_q$ è la matrice composta dalle prime $q$ colonne di $\mathbf{V}$.

$\mathbf{H}_q = \mathbf{V}_q\mathbf{V}_q^\top$ è una *matrice di proiezione*, mappa ogni riga di $X$ (vista come vettore colonna), $x_i$, nella sua ricostruzione di ordine $q$, $\mathbf{H}_qx_i$ (quindi $\mathbf{X}_q = \mathbf{X}\mathbf{H}_q$).

```python
np.random.seed(42)

x = np.linspace(3, 6, num=100)
X = np.c_[x, 10 - x] + np.random.multivariate_normal(np.zeros(2), 0.1 * np.eye(2), size=len(x))
print("Dimensioni di X (N X p): {} X {}".format(*X.shape))
```

### Esercizio

1. Ottenere $\mathbf{U}$, $\mathbf{D}$, $\mathbf{V^\top}$ utilizzando la funzione [`numpy.linalg.svd()`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.svd.html) ponendo `full_matrices=False` (nota: la funzione restituisce $\mathbf{U}$, $s$, $\mathbf{V^\top}$, dove $s$ sono gli elementi sulla diagonale di $D$);
2. Verificare che $\mathbf{X} = \mathbf{U}\mathbf{D}\mathbf{V}^\top$ utilizzando la funzione [`numpy.allclose()`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.allclose.html);
3. Verificare che `U.dot(D)`è equivalente a `U * s`;
4. Dire grazie a che proprietà in NumPy è possibile moltiplicare (prodotto di Hadamard) un array $(N, K)$ con un array $(K,)$.

```python

```

## 1.2 Studiare l'effetto della centratura di X

> Nota: questa parte è ispirata a una [domanda](https://stats.stackexchange.com/questions/22329/how-does-centering-the-data-get-rid-of-the-intercept-in-regression-and-pca/22331#22331) chiesta su [Cross Validated](https://stats.stackexchange.com/) ([StackExchange](https://stackexchange.com/))

```python
from msbd.grafici import grafico_proiezione_sul_primo_asse_principale

print(inspect.getsource(grafico_proiezione_sul_primo_asse_principale))
```

```python
plt.figure(figsize=(6, 6))

grafico_proiezione_sul_primo_asse_principale(X)

plt.show()
```

### Esercizio

Riottenere il grafico sui dati centrati.

```python

```

# 2. Spirale logaritmica

### Rappresentazione parametrica della spirale logaritmica

$
\begin{align}
x(\lambda)&=a{\text{e}}^{b\lambda}\cos(\lambda)\\
y(\lambda)&=a{\text{e}}^{b\lambda}\sin(\lambda)
\end{align}
$

```python
from msbd.datasets import SpiraleLogaritmica

print(inspect.getsource(SpiraleLogaritmica))
```

## 2.1 Generare le osservazioni

```python
sl = SpiraleLogaritmica(0.5, 0.5)

l = np.random.uniform(-math.pi / 4, math.pi, 100)
l.sort()
X = sl.f(l)
X_oss = X + np.random.multivariate_normal(np.zeros(2), 0.01 * np.eye(2), size=len(X))
X -= X_oss.mean(axis=0)
X_oss -= X_oss.mean(axis=0)
```

## 2.2 Visualizzare le osservazioni

```python
plt.figure(figsize=(8, 5))

plt.plot(X[:, 0], X[:, 1], lw=2, label="Spirale logaritmica")
plt.scatter(X_oss[:, 0], X_oss[:, 1], facecolor="none", edgecolor="tab:blue", label="Osservazioni")
plt.xlim([-2.5, 1.5])
plt.ylim([-1.5, 1])
plt.axis("off")
plt.tight_layout()
plt.legend()

plt.show()
```

# 3. Curve principali

Passaggi per ottenere la curva principale $f(\lambda)$ (da [The Elements of
Statistical Learning](https://web.stanford.edu/~hastie/ElemStatLearn/) paragrafo 14.5.2):

$f(\lambda) = [f_1(\lambda),\, f_2(\lambda),\, \cdots,\, f_p(\lambda)]$.

$X^\top = [X_1,\, X_2,\, \cdots,\, X_p]$ vettore casuale.

$\hat{\lambda}_f(x)$ definisce il punto sulla curva più vicino a $x$.

$\hat{f}(\lambda)\leftarrow E(X\mid\lambda_f(X) = \lambda)$ è chiamata *curva principale* per la distribuzione del vettore casuale $X$.

Passaggi da alternare fino a convergenza:

1. $\hat{f}_j(\lambda)\leftarrow E(X_j\mid\lambda(X) = \lambda);\; j=1, 2, \cdots,p,$
2. $\hat{\lambda}_f(x)\leftarrow \mathrm{argmin}_{\lambda^\prime}\Vert x - \hat{f}(\lambda^\prime)\Vert$.

```python
from msbd.varieta import CurvaPrincipale

print(inspect.getsource(CurvaPrincipale))
```

```python
from msbd.grafici import grafico_curva_principale

print(inspect.getsource(grafico_curva_principale))
```

```python
cp = CurvaPrincipale()
it = 0

while True:
    it += 1

    cp.partial_fit(X_oss)

    plt.figure(figsize=(8, 5))

    plt.title("Curva principale dopo {} iterazioni".format(it))
    plt.plot(X[:, 0], X[:, 1], lw=2, label="Spirale logaritmica")
    grafico_curva_principale(X_oss, cp)
    plt.xlim([-2.5, 1.5])
    plt.ylim([-1.5, 1])
    plt.axis("off")
    plt.tight_layout()
    plt.legend()

    plt.show()

    i = input("Premere invio per continuare, q (quit) per uscire:")

    if i == "q":
        break
```

### Esercizio

1. Definire una nuova classe con la stessa struttura di `SpiraleLogaritmica`;
2. Ripetere tutti i passaggi visti nel notebook.

```python

```


```python
# This mounts your Google Drive to the Colab VM.
from google.colab import drive
drive.mount('/content/drive')

# TODO: Enter the foldername in your Drive where you have saved the unzipped
# assignment folder, e.g. 'cs231n/assignments/assignment1/'
FOLDERNAME = 'cs231n/assignments/assignment2'
assert FOLDERNAME is not None, "[!] Enter the foldername."

# Now that we've mounted your Drive, this ensures that
# the Python interpreter of the Colab VM can load
# python files from within it.
import sys
sys.path.append('/content/drive/My Drive/{}'.format(FOLDERNAME))

# This downloads the CIFAR-10 dataset to your Drive
# if it doesn't already exist.
%cd /content/drive/My\ Drive/$FOLDERNAME/cs231n/datasets/
!bash get_datasets.sh
%cd /content/drive/My\ Drive/$FOLDERNAME
```

    Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
    /content/drive/My Drive/cs231n/assignments/assignment2/cs231n/datasets
    /content/drive/My Drive/cs231n/assignments/assignment2

# Batch Normalization
One way to make deep networks easier to train is to use more sophisticated optimization procedures such as SGD+momentum, RMSProp, or Adam. Another strategy is to change the architecture of the network to make it easier to train. One idea along these lines is batch normalization, proposed by [1] in 2015.

To understand the goal of batch normalization, it is important to first recognize that machine learning methods tend to perform better with input data consisting of uncorrelated features with zero mean and unit variance. When training a neural network, we can preprocess the data before feeding it to the network to explicitly decorrelate its features. This will ensure that the first layer of the network sees data that follows a nice distribution. However, even if we preprocess the input data, the activations at deeper layers of the network will likely no longer be decorrelated and will no longer have zero mean or unit variance, since they are output from earlier layers in the network. Even worse, during the training process the distribution of features at each layer of the network will shift as the weights of each layer are updated.

The authors of [1] hypothesize that the shifting distribution of features inside deep neural networks may make training deep networks more difficult. To overcome this problem, they propose to insert into the network layers that normalize batches. At training time, such a layer uses a minibatch of data to estimate the mean and standard deviation of each feature. These estimated means and standard deviations are then used to center and normalize the features of the minibatch. A running average of these means and standard deviations is kept during training, and at test time these running averages are used to center and normalize features.

It is possible that this normalization strategy could reduce the representational power of the network, since it may sometimes be optimal for certain layers to have features that are not zero-mean or unit variance. To this end, the batch normalization layer includes learnable shift and scale parameters for each feature dimension.

[1] [Sergey Ioffe and Christian Szegedy, "Batch Normalization: Accelerating Deep Network Training by Reducing
Internal Covariate Shift", ICML 2015.](https://arxiv.org/abs/1502.03167)

```python
# Setup cell.
import time
import numpy as np
import matplotlib.pyplot as plt
from cs231n.classifiers.fc_net import *
from cs231n.data_utils import get_CIFAR10_data
from cs231n.gradient_check import eval_numerical_gradient, eval_numerical_gradient_array
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"

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

def print_mean_std(x,axis=0):
    print(f"  means: {x.mean(axis=axis)}")
    print(f"  stds:  {x.std(axis=axis)}\n")
```

    The autoreload extension is already loaded. To reload it, use:
      %reload_ext autoreload

```python
# Load the (preprocessed) CIFAR-10 data.
data = get_CIFAR10_data()
for k, v in list(data.items()):
    print(f"{k}: {v.shape}")
```

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

# Batch Normalization: Forward Pass
In the file `cs231n/layers.py`, implement the batch normalization forward pass in the function `batchnorm_forward`. Once you have done so, run the following to test your implementation.

Referencing the paper linked to above in [1] may be helpful!

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

# Simulate the forward pass for a two-layer network.
np.random.seed(231)
N, D1, D2, D3 = 200, 50, 60, 3
X = np.random.randn(N, D1)
W1 = np.random.randn(D1, D2)
W2 = np.random.randn(D2, D3)
a = np.maximum(0, X.dot(W1)).dot(W2)

print('Before batch normalization:')
print_mean_std(a,axis=0)

gamma = np.ones((D3,))
beta = np.zeros((D3,))

# Means should be close to zero and stds close to one.
print('After batch normalization (gamma=1, beta=0)')
a_norm, _ = batchnorm_forward(a, gamma, beta, {'mode': 'train'})
print_mean_std(a_norm,axis=0)

gamma = np.asarray([1.0, 2.0, 3.0])
beta = np.asarray([11.0, 12.0, 13.0])

# Now means should be close to beta and stds close to gamma.
print('After batch normalization (gamma=', gamma, ', beta=', beta, ')')
a_norm, _ = batchnorm_forward(a, gamma, beta, {'mode': 'train'})
print_mean_std(a_norm,axis=0)
```

    Before batch normalization:
      means: [ -2.3814598  -13.18038246   1.91780462]
      stds:  [27.18502186 34.21455511 37.68611762]

    After batch normalization (gamma=1, beta=0)
      means: [5.99520433e-17 6.93889390e-17 8.32667268e-19]
      stds:  [0.99999999 1.         1.        ]

    After batch normalization (gamma= [1. 2. 3.] , beta= [11. 12. 13.] )
      means: [11. 12. 13.]
      stds:  [0.99999999 1.99999999 2.99999999]

```python
# 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.

np.random.seed(231)
N, D1, D2, D3 = 200, 50, 60, 3
W1 = np.random.randn(D1, D2)
W2 = np.random.randn(D2, D3)

bn_param = {'mode': 'train'}
gamma = np.ones(D3)
beta = np.zeros(D3)

for t in range(50):
  X = np.random.randn(N, D1)
  a = np.maximum(0, X.dot(W1)).dot(W2)
  batchnorm_forward(a, gamma, beta, bn_param)

bn_param['mode'] = 'test'
X = np.random.randn(N, D1)
a = np.maximum(0, X.dot(W1)).dot(W2)
a_norm, _ = batchnorm_forward(a, 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 batch normalization (test-time):')
print_mean_std(a_norm,axis=0)
```

    After batch normalization (test-time):
      means: [-0.03927354 -0.04349152 -0.10452688]
      stds:  [1.01531428 1.01238373 0.97819988]

# Batch Normalization: Backward Pass
Now implement the backward pass for batch normalization in the function `batchnorm_backward`.

To derive the backward pass you should write out the computation graph for batch normalization and backprop through each of the intermediate nodes. Some intermediates may have multiple outgoing branches; make sure to sum gradients across these branches in the backward pass.

Once you have finished, run the following to numerically check your backward pass.

```python
# Gradient check batchnorm backward pass.
np.random.seed(231)
N, D = 4, 5
x = 5 * np.random.randn(N, D) + 12
gamma = np.random.randn(D)
beta = np.random.randn(D)
dout = np.random.randn(N, D)

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

dx_num = eval_numerical_gradient_array(fx, x, dout)
da_num = eval_numerical_gradient_array(fg, gamma.copy(), dout)
db_num = eval_numerical_gradient_array(fb, beta.copy(), dout)

_, cache = batchnorm_forward(x, gamma, beta, bn_param)
dx, dgamma, dbeta = batchnorm_backward(dout, cache)

# You should expect to see relative errors between 1e-13 and 1e-8.
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.6674604875341426e-09
    dgamma error:  7.417225040694815e-13
    dbeta error:  2.379446949959628e-12

# Batch Normalization: Alternative Backward Pass
In class we talked about two different implementations for the sigmoid backward pass. One strategy is to write out a computation graph composed of simple operations and backprop through all intermediate values. Another strategy is to work out the derivatives on paper. For example, you can derive a very simple formula for the sigmoid function's backward pass by simplifying gradients on paper.

Surprisingly, it turns out that you can do a similar simplification for the batch normalization backward pass too!

In the forward pass, given a set of inputs $X=\begin{bmatrix}x_1\\x_2\\...\\x_N\end{bmatrix}$,

we first calculate the mean $\mu$ and variance $v$.
With $\mu$ and $v$ calculated, we can calculate the standard deviation $\sigma$  and normalized data $Y$.
The equations and graph illustration below describe the computation ($y_i$ is the i-th element of the vector $Y$).

\begin{align}
& \mu=\frac{1}{N}\sum_{k=1}^N x_k  &  v=\frac{1}{N}\sum_{k=1}^N (x_k-\mu)^2 \\
& \sigma=\sqrt{v+\epsilon}         &  y_i=\frac{x_i-\mu}{\sigma}
\end{align}

The meat of our problem during backpropagation is to compute $\frac{\partial L}{\partial X}$, given the upstream gradient we receive, $\frac{\partial L}{\partial Y}.$ To do this, recall the chain rule in calculus gives us $\frac{\partial L}{\partial X} = \frac{\partial L}{\partial Y} \cdot \frac{\partial Y}{\partial X}$.

The unknown/hard part is $\frac{\partial Y}{\partial X}$. We can find this by first deriving step-by-step our local gradients at
$\frac{\partial v}{\partial X}$, $\frac{\partial \mu}{\partial X}$,
$\frac{\partial \sigma}{\partial v}$,
$\frac{\partial Y}{\partial \sigma}$, and $\frac{\partial Y}{\partial \mu}$,
and then use the chain rule to compose these gradients (which appear in the form of vectors!) appropriately to compute $\frac{\partial Y}{\partial X}$.

If it's challenging to directly reason about the gradients over $X$ and $Y$ which require matrix multiplication, try reasoning about the gradients in terms of individual elements $x_i$ and $y_i$ first: in that case, you will need to come up with the derivations for $\frac{\partial L}{\partial x_i}$, by relying on the Chain Rule to first calculate the intermediate $\frac{\partial \mu}{\partial x_i}, \frac{\partial v}{\partial x_i}, \frac{\partial \sigma}{\partial x_i},$ then assemble these pieces to calculate $\frac{\partial y_i}{\partial x_i}$.

You should make sure each of the intermediary gradient derivations are all as simplified as possible, for ease of implementation.

After doing so, implement the simplified batch normalization backward pass in the function `batchnorm_backward_alt` and compare the two implementations by running the following. Your two implementations should compute nearly identical results, but the alternative implementation should be a bit faster.

```python
np.random.seed(231)
N, D = 100, 500
x = 5 * np.random.randn(N, D) + 12
gamma = np.random.randn(D)
beta = np.random.randn(D)
dout = np.random.randn(N, D)

bn_param = {'mode': 'train'}
out, cache = batchnorm_forward(x, gamma, beta, bn_param)

t1 = time.time()
dx1, dgamma1, dbeta1 = batchnorm_backward(dout, cache)
t2 = time.time()
dx2, dgamma2, dbeta2 = batchnorm_backward_alt(dout, cache)
t3 = time.time()

print('dx difference: ', rel_error(dx1, dx2))
print('dgamma difference: ', rel_error(dgamma1, dgamma2))
print('dbeta difference: ', rel_error(dbeta1, dbeta2))
print('speedup: %.2fx' % ((t2 - t1) / (t3 - t2)))
```

    dx difference:  9.890497291190823e-13
    dgamma difference:  0.0
    dbeta difference:  0.0
    speedup: 5.40x

# Fully Connected Networks with Batch Normalization
Now that you have a working implementation for batch normalization, go back to your `FullyConnectedNet` in the file `cs231n/classifiers/fc_net.py`. Modify your implementation to add batch normalization.

Concretely, when the `normalization` flag is set to `"batchnorm"` in the constructor, you should insert a batch normalization layer before each ReLU nonlinearity. The outputs from the last layer of the network should not be normalized. Once you are done, run the following to gradient-check your implementation.

**Hint:** You might find it useful to define an additional helper layer similar to those in the file `cs231n/layer_utils.py`.

```python
np.random.seed(231)
N, D, H1, H2, C = 2, 15, 20, 30, 10
X = np.random.randn(N, D)
y = np.random.randint(C, size=(N,))

# You should expect losses between 1e-4~1e-10 for W,
# losses between 1e-08~1e-10 for b,
# and losses between 1e-08~1e-09 for beta and gammas.
for reg in [0, 3.14]:
  print('Running check with reg = ', reg)
  model = FullyConnectedNet([H1, H2], input_dim=D, num_classes=C,
                            reg=reg, weight_scale=5e-2, dtype=np.float64,
                            normalization='batchnorm')

  loss, grads = model.loss(X, y)
  print('Initial loss: ', loss)

  for name in sorted(grads):
    f = lambda _: model.loss(X, y)[0]
    grad_num = eval_numerical_gradient(f, model.params[name], verbose=False, h=1e-5)
    print('%s relative error: %.2e' % (name, rel_error(grad_num, grads[name])))
  if reg == 0: print()
```

    Running check with reg =  0
    Initial loss:  2.2611955101340957
    W1 relative error: 1.10e-04
    W2 relative error: 5.76e-06
    W3 relative error: 3.25e-10
    b1 relative error: 3.11e-07
    b2 relative error: 2.72e-07
    b3 relative error: 1.01e-10
    beta1 relative error: 6.94e-09
    beta2 relative error: 2.40e-09
    gamma1 relative error: 7.57e-09
    gamma2 relative error: 3.35e-09

    Running check with reg =  3.14
    Initial loss:  6.996533220108303
    W1 relative error: 1.98e-06
    W2 relative error: 2.28e-06
    W3 relative error: 1.11e-08
    b1 relative error: 1.38e-08
    b2 relative error: 7.99e-07
    b3 relative error: 1.42e-10
    beta1 relative error: 6.65e-09
    beta2 relative error: 3.39e-09
    gamma1 relative error: 5.94e-09
    gamma2 relative error: 5.28e-09

# Batch Normalization for Deep Networks
Run the following to train a six-layer network on a subset of 1000 training examples both with and without batch normalization.

```python
np.random.seed(231)

# Try training a very deep net with batchnorm.
hidden_dims = [100, 100, 100, 100, 100, 100, 100] # Original: [100, 100, 100, 100, 100]

num_train = 1000
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'],
}

weight_scale = 2e-2
bn_model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization='batchnorm')
model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization=None)

print('Solver with batch norm:')
bn_solver = Solver(bn_model, small_data,
                num_epochs=10, batch_size=50,
                update_rule='adam',
                optim_config={
                  'learning_rate': 1e-3,
                },
                verbose=True,print_every=20)
bn_solver.train()

print('\nSolver without batch norm:')
solver = Solver(model, small_data,
                num_epochs=10, batch_size=50,
                update_rule='adam',
                optim_config={
                  'learning_rate': 1e-3,
                },
                verbose=True, print_every=20)
solver.train()
```

    Solver with batch norm:
    (Iteration 1 / 200) loss: 2.283911
    (Epoch 0 / 10) train acc: 0.121000; val_acc: 0.130000
    (Epoch 1 / 10) train acc: 0.249000; val_acc: 0.221000
    (Iteration 21 / 200) loss: 2.052474
    (Epoch 2 / 10) train acc: 0.344000; val_acc: 0.268000
    (Iteration 41 / 200) loss: 1.727148
    (Epoch 3 / 10) train acc: 0.403000; val_acc: 0.277000
    (Iteration 61 / 200) loss: 1.559967
    (Epoch 4 / 10) train acc: 0.470000; val_acc: 0.297000
    (Iteration 81 / 200) loss: 1.371887
    (Epoch 5 / 10) train acc: 0.522000; val_acc: 0.296000
    (Iteration 101 / 200) loss: 1.741498
    (Epoch 6 / 10) train acc: 0.546000; val_acc: 0.301000
    (Iteration 121 / 200) loss: 1.430474
    (Epoch 7 / 10) train acc: 0.589000; val_acc: 0.319000
    (Iteration 141 / 200) loss: 1.483275
    (Epoch 8 / 10) train acc: 0.575000; val_acc: 0.281000
    (Iteration 161 / 200) loss: 1.253456
    (Epoch 9 / 10) train acc: 0.627000; val_acc: 0.308000
    (Iteration 181 / 200) loss: 1.189213
    (Epoch 10 / 10) train acc: 0.647000; val_acc: 0.276000

    Solver without batch norm:
    (Iteration 1 / 200) loss: 2.302586
    (Epoch 0 / 10) train acc: 0.132000; val_acc: 0.125000
    (Epoch 1 / 10) train acc: 0.182000; val_acc: 0.174000
    (Iteration 21 / 200) loss: 2.160158
    (Epoch 2 / 10) train acc: 0.259000; val_acc: 0.229000
    (Iteration 41 / 200) loss: 2.114211
    (Epoch 3 / 10) train acc: 0.280000; val_acc: 0.242000
    (Iteration 61 / 200) loss: 1.785834
    (Epoch 4 / 10) train acc: 0.283000; val_acc: 0.243000
    (Iteration 81 / 200) loss: 1.802308
    (Epoch 5 / 10) train acc: 0.298000; val_acc: 0.242000
    (Iteration 101 / 200) loss: 1.514012
    (Epoch 6 / 10) train acc: 0.288000; val_acc: 0.222000
    (Iteration 121 / 200) loss: 1.726458
    (Epoch 7 / 10) train acc: 0.351000; val_acc: 0.240000
    (Iteration 141 / 200) loss: 1.680810
    (Epoch 8 / 10) train acc: 0.340000; val_acc: 0.234000
    (Iteration 161 / 200) loss: 1.519239
    (Epoch 9 / 10) train acc: 0.357000; val_acc: 0.246000
    (Iteration 181 / 200) loss: 1.635694
    (Epoch 10 / 10) train acc: 0.375000; val_acc: 0.229000

Run the following to visualize the results from two networks trained above. You should find that using batch normalization helps the network to converge much faster.

```python
def plot_training_history(title, label, baseline, bn_solvers, plot_fn, bl_marker='.', bn_marker='.', labels=None):
    """utility function for plotting training history"""
    plt.title(title)
    plt.xlabel(label)
    bn_plots = [plot_fn(bn_solver) for bn_solver in bn_solvers]
    bl_plot = plot_fn(baseline)
    num_bn = len(bn_plots)
    for i in range(num_bn):
        label='with_norm'
        if labels is not None:
            label += str(labels[i])
        plt.plot(bn_plots[i], bn_marker, label=label)
    label='baseline'
    if labels is not None:
        label += str(labels[0])
    plt.plot(bl_plot, bl_marker, label=label)
    plt.legend(loc='lower center', ncol=num_bn+1)

plt.subplot(3, 1, 1)
plot_training_history('Training loss','Iteration', solver, [bn_solver], \
                      lambda x: x.loss_history, bl_marker='o', bn_marker='o')
plt.subplot(3, 1, 2)
plot_training_history('Training accuracy','Epoch', solver, [bn_solver], \
                      lambda x: x.train_acc_history, bl_marker='-o', bn_marker='-o')
plt.subplot(3, 1, 3)
plot_training_history('Validation accuracy','Epoch', solver, [bn_solver], \
                      lambda x: x.val_acc_history, bl_marker='-o', bn_marker='-o')

plt.gcf().set_size_inches(15, 15)
plt.show()
```

# Batch Normalization and Initialization
We will now run a small experiment to study the interaction of batch normalization and weight initialization.

The first cell will train eight-layer networks both with and without batch normalization using different scales for weight initialization. The second layer will plot training accuracy, validation set accuracy, and training loss as a function of the weight initialization scale.

```python
np.random.seed(231)

# Try training a very deep net with batchnorm.
hidden_dims = [50, 50, 50, 50, 50, 50, 50]
num_train = 1000
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'],
}

bn_solvers_ws = {}
solvers_ws = {}
weight_scales = np.logspace(-4, 0, num=20)
for i, weight_scale in enumerate(weight_scales):
    print('Running weight scale %d / %d' % (i + 1, len(weight_scales)))
    bn_model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization='batchnorm')
    model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization=None)

    bn_solver = Solver(bn_model, small_data,
                  num_epochs=10, batch_size=50,
                  update_rule='adam',
                  optim_config={
                    'learning_rate': 1e-3,
                  },
                  verbose=False, print_every=200)
    bn_solver.train()
    bn_solvers_ws[weight_scale] = bn_solver

    solver = Solver(model, small_data,
                  num_epochs=10, batch_size=50,
                  update_rule='adam',
                  optim_config={
                    'learning_rate': 1e-3,
                  },
                  verbose=False, print_every=200)
    solver.train()
    solvers_ws[weight_scale] = solver
```

    Running weight scale 1 / 20
    Running weight scale 2 / 20
    Running weight scale 3 / 20
    Running weight scale 4 / 20
    Running weight scale 5 / 20
    Running weight scale 6 / 20
    Running weight scale 7 / 20
    Running weight scale 8 / 20
    Running weight scale 9 / 20
    Running weight scale 10 / 20
    Running weight scale 11 / 20
    Running weight scale 12 / 20
    Running weight scale 13 / 20
    Running weight scale 14 / 20
    Running weight scale 15 / 20
    Running weight scale 16 / 20
    Running weight scale 17 / 20
    Running weight scale 18 / 20
    Running weight scale 19 / 20
    Running weight scale 20 / 20

```python
# Plot results of weight scale experiment.
best_train_accs, bn_best_train_accs = [], []
best_val_accs, bn_best_val_accs = [], []
final_train_loss, bn_final_train_loss = [], []

for ws in weight_scales:
  best_train_accs.append(max(solvers_ws[ws].train_acc_history))
  bn_best_train_accs.append(max(bn_solvers_ws[ws].train_acc_history))

  best_val_accs.append(max(solvers_ws[ws].val_acc_history))
  bn_best_val_accs.append(max(bn_solvers_ws[ws].val_acc_history))

  final_train_loss.append(np.mean(solvers_ws[ws].loss_history[-100:]))
  bn_final_train_loss.append(np.mean(bn_solvers_ws[ws].loss_history[-100:]))

plt.subplot(3, 1, 1)
plt.title('Best val accuracy vs. weight initialization scale')
plt.xlabel('Weight initialization scale')
plt.ylabel('Best val accuracy')
plt.semilogx(weight_scales, best_val_accs, '-o', label='baseline')
plt.semilogx(weight_scales, bn_best_val_accs, '-o', label='batchnorm')
plt.legend(ncol=2, loc='lower right')

plt.subplot(3, 1, 2)
plt.title('Best train accuracy vs. weight initialization scale')
plt.xlabel('Weight initialization scale')
plt.ylabel('Best training accuracy')
plt.semilogx(weight_scales, best_train_accs, '-o', label='baseline')
plt.semilogx(weight_scales, bn_best_train_accs, '-o', label='batchnorm')
plt.legend()

plt.subplot(3, 1, 3)
plt.title('Final training loss vs. weight initialization scale')
plt.xlabel('Weight initialization scale')
plt.ylabel('Final training loss')
plt.semilogx(weight_scales, final_train_loss, '-o', label='baseline')
plt.semilogx(weight_scales, bn_final_train_loss, '-o', label='batchnorm')
plt.legend()
plt.gca().set_ylim(1.0, 3.5)

plt.gcf().set_size_inches(15, 15)
plt.show()
```

## Inline Question 1:
Describe the results of this experiment. How does the weight initialization scale affect models with/without batch normalization differently, and why?

## Answer:
Batch normalization keeps results less independent of weight scale initialization.

# Batch Normalization and Batch Size
We will now run a small experiment to study the interaction of batch normalization and batch size.

The first cell will train 6-layer networks both with and without batch normalization using different batch sizes. The second layer will plot training accuracy and validation set accuracy over time.

```python
def run_batchsize_experiments(normalization_mode):
    np.random.seed(231)

    # Try training a very deep net with batchnorm.
    hidden_dims = [100, 100, 100, 100, 100]
    num_train = 1000
    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'],
    }
    n_epochs=10
    weight_scale = 2e-2
    batch_sizes = [5,10,50]
    lr = 10**(-3.5)
    solver_bsize = batch_sizes[0]

    print('No normalization: batch size = ',solver_bsize)
    model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization=None)
    solver = Solver(model, small_data,
                    num_epochs=n_epochs, batch_size=solver_bsize,
                    update_rule='adam',
                    optim_config={
                      'learning_rate': lr,
                    },
                    verbose=False)
    solver.train()

    bn_solvers = []
    for i in range(len(batch_sizes)):
        b_size=batch_sizes[i]
        print('Normalization: batch size = ',b_size)
        bn_model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization=normalization_mode)
        bn_solver = Solver(bn_model, small_data,
                        num_epochs=n_epochs, batch_size=b_size,
                        update_rule='adam',
                        optim_config={
                          'learning_rate': lr,
                        },
                        verbose=False)
        bn_solver.train()
        bn_solvers.append(bn_solver)

    return bn_solvers, solver, batch_sizes

batch_sizes = [5,10,50]
bn_solvers_bsize, solver_bsize, batch_sizes = run_batchsize_experiments('batchnorm')
```

    No normalization: batch size =  5
    Normalization: batch size =  5
    Normalization: batch size =  10
    Normalization: batch size =  50

```python
plt.subplot(2, 1, 1)
plot_training_history('Training accuracy (Batch Normalization)','Epoch', solver_bsize, bn_solvers_bsize, \
                      lambda x: x.train_acc_history, bl_marker='-^', bn_marker='-o', labels=batch_sizes)
plt.subplot(2, 1, 2)
plot_training_history('Validation accuracy (Batch Normalization)','Epoch', solver_bsize, bn_solvers_bsize, \
                      lambda x: x.val_acc_history, bl_marker='-^', bn_marker='-o', labels=batch_sizes)

plt.gcf().set_size_inches(15, 10)
plt.show()
```

## Inline Question 2:
Describe the results of this experiment. What does this imply about the relationship between batch normalization and batch size? Why is this relationship observed?

## Answer:
The larger the batch size, the faster the training. Larger batch size approximates better the average and stddev over entire data set.

# Layer Normalization
Batch normalization has proved to be effective in making networks easier to train, but the dependency on batch size makes it less useful in complex networks which have a cap on the input batch size due to hardware limitations.

Several alternatives to batch normalization have been proposed to mitigate this problem; one such technique is Layer Normalization [2]. Instead of normalizing over the batch, we normalize over the features. In other words, when using Layer Normalization, each feature vector corresponding to a single datapoint is normalized based on the sum of all terms within that feature vector.

[2] [Ba, Jimmy Lei, Jamie Ryan Kiros, and Geoffrey E. Hinton. "Layer Normalization." stat 1050 (2016): 21.](https://arxiv.org/pdf/1607.06450.pdf)

## Inline Question 3:
Which of these data preprocessing steps is analogous to batch normalization, and which is analogous to layer normalization?

1. Scaling each image in the dataset, so that the RGB channels for each row of pixels within an image sums up to 1.
2. Scaling each image in the dataset, so that the RGB channels for all pixels within an image sums up to 1.
3. Subtracting the mean image of the dataset from each image in the dataset.
4. Setting all RGB values to either 0 or 1 depending on a given threshold.

## Answer:
1. Batch normalization
2. Batch normalization
3. Layer normalizarion
4. Layer normalization

# Layer Normalization: Implementation

Now you'll implement layer normalization. This step should be relatively straightforward, as conceptually the implementation is almost identical to that of batch normalization. One significant difference though is that for layer normalization, we do not keep track of the moving moments, and the testing phase is identical to the training phase, where the mean and variance are directly calculated per datapoint.

Here's what you need to do:

* In `cs231n/layers.py`, implement the forward pass for layer normalization in the function `layernorm_forward`.

Run the cell below to check your results.
* In `cs231n/layers.py`, implement the backward pass for layer normalization in the function `layernorm_backward`.

Run the second cell below to check your results.
* Modify `cs231n/classifiers/fc_net.py` to add layer normalization to the `FullyConnectedNet`. When the `normalization` flag is set to `"layernorm"` in the constructor, you should insert a layer normalization layer before each ReLU nonlinearity.

Run the third cell below to run the batch size experiment on layer normalization.

```python
# Check the training-time forward pass by checking means and variances
# of features both before and after layer normalization.

# Simulate the forward pass for a two-layer network.
np.random.seed(231)
N, D1, D2, D3 =4, 50, 60, 3
X = np.random.randn(N, D1)
W1 = np.random.randn(D1, D2)
W2 = np.random.randn(D2, D3)
a = np.maximum(0, X.dot(W1)).dot(W2)

print('Before layer normalization:')
print_mean_std(a,axis=1)

gamma = np.ones(D3)
beta = np.zeros(D3)

# Means should be close to zero and stds close to one.
print('After layer normalization (gamma=1, beta=0)')
a_norm, _ = layernorm_forward(a, gamma, beta, {'mode': 'train'})
print_mean_std(a_norm,axis=1)

gamma = np.asarray([3.0,3.0,3.0])
beta = np.asarray([5.0,5.0,5.0])

# Now means should be close to beta and stds close to gamma.
print('After layer normalization (gamma=', gamma, ', beta=', beta, ')')
a_norm, _ = layernorm_forward(a, gamma, beta, {'mode': 'train'})
print_mean_std(a_norm,axis=1)
```

    Before layer normalization:
      means: [-59.06673243 -47.60782686 -43.31137368 -26.40991744]
      stds:  [10.07429373 28.39478981 35.28360729  4.01831507]

    After layer normalization (gamma=1, beta=0)
      means: [ 4.81096644e-16 -7.40148683e-17  2.22044605e-16 -5.92118946e-16]
      stds:  [0.99999995 0.99999999 1.         0.99999969]

    After layer normalization (gamma= [3. 3. 3.] , beta= [5. 5. 5.] )
      means: [5. 5. 5. 5.]
      stds:  [2.99999985 2.99999998 2.99999999 2.99999907]

```python
# Gradient check batchnorm backward pass.
np.random.seed(231)
N, D = 4, 5
x = 5 * np.random.randn(N, D) + 12
gamma = np.random.randn(D)
beta = np.random.randn(D)
dout = np.random.randn(N, D)

ln_param = {}
fx = lambda x: layernorm_forward(x, gamma, beta, ln_param)[0]
fg = lambda a: layernorm_forward(x, a, beta, ln_param)[0]
fb = lambda b: layernorm_forward(x, gamma, b, ln_param)[0]

dx_num = eval_numerical_gradient_array(fx, x, dout)
da_num = eval_numerical_gradient_array(fg, gamma.copy(), dout)
db_num = eval_numerical_gradient_array(fb, beta.copy(), dout)

_, cache = layernorm_forward(x, gamma, beta, ln_param)
dx, dgamma, dbeta = layernorm_backward(dout, cache)

# You should expect to see relative errors between 1e-12 and 1e-8.
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:  2.107279147162234e-09
    dgamma error:  4.519489546032799e-12
    dbeta error:  2.5842537629899423e-12

# Layer Normalization and Batch Size

We will now run the previous batch size experiment with layer normalization instead of batch normalization. Compared to the previous experiment, you should see a markedly smaller influence of batch size on the training history!

```python
ln_solvers_bsize, solver_bsize, batch_sizes = run_batchsize_experiments('layernorm')

plt.subplot(2, 1, 1)
plot_training_history('Training accuracy (Layer Normalization)','Epoch', solver_bsize, ln_solvers_bsize, \
                      lambda x: x.train_acc_history, bl_marker='-^', bn_marker='-o', labels=batch_sizes)
plt.subplot(2, 1, 2)
plot_training_history('Validation accuracy (Layer Normalization)','Epoch', solver_bsize, ln_solvers_bsize, \
                      lambda x: x.val_acc_history, bl_marker='-^', bn_marker='-o', labels=batch_sizes)

plt.gcf().set_size_inches(15, 10)
plt.show()
```

## Inline Question 4:
When is layer normalization likely to not work well, and why?

1. Using it in a very deep network
2. Having a very small dimension of features
3. Having a high regularization term

## Answer:
See below.

```python
np.random.seed(231)

# Try training a very deep net with and without layer norm.
hidden_dims = [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
num_train = 1000
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'],
}
n_epochs=100
weight_scale = 2e-2
batch_size = 50
lr = 10**(-3.5)

model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale)
model_norm = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization='layernorm')
solver = Solver(model, small_data,
                num_epochs=n_epochs, batch_size=batch_size,
                update_rule='adam',
                optim_config={
                  'learning_rate': lr,
                },
                verbose=False)
solver_norm = Solver(model_norm, small_data,
                num_epochs=n_epochs, batch_size=batch_size,
                update_rule='adam',
                optim_config={
                  'learning_rate': lr,
                },
                verbose=False)
solver.train()
solver_norm.train()

```

```python
plt.subplot(2, 1, 1)
plt.title('Training loss')
plt.plot(solver.loss_history, label='loss history baseline')
plt.plot(solver_norm.loss_history, label='loss history layer normalization')
plt.xlabel('Iteration')
plt.legend(loc='lower left')
plt.grid(linestyle='--', linewidth=0.5)

plt.subplot(2, 1, 2)
plt.title('Accuracy')
plt.plot(solver.train_acc_history, '-o', label='train baseline')
plt.plot(solver.val_acc_history, '-o', label='val baseline')
plt.plot(solver_norm.train_acc_history, '-o', label='train layer norm')
plt.plot(solver_norm.val_acc_history, '-o', label='val layer norm')

plt.xlabel('Epoch')
plt.legend(loc='lower right')
plt.gcf().set_size_inches(15, 12)
plt.grid(linestyle='--', linewidth=0.5)
plt.show()
```

Layer normalization not working well in very deep network, b/c presumably of vanishing gradients.

```python
np.random.seed(231)

# Try training a network with narrow features.
hidden_dims = [10, 10, 10, 10, 10]
num_train = 1000
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'],
}
n_epochs=100
weight_scale = 2e-2
batch_size = 50
lr = 10**(-3.5)

model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale)
model_norm = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization='layernorm')
solver = Solver(model, small_data,
                num_epochs=n_epochs, batch_size=batch_size,
                update_rule='adam',
                optim_config={
                  'learning_rate': lr,
                },
                verbose=False)
solver_norm = Solver(model_norm, small_data,
                num_epochs=n_epochs, batch_size=batch_size,
                update_rule='adam',
                optim_config={
                  'learning_rate': lr,
                },
                verbose=False)
solver.train()
solver_norm.train()

```

```python
plt.subplot(2, 1, 1)
plt.title('Training loss')
plt.plot(solver.loss_history, label='loss history baseline')
plt.plot(solver_norm.loss_history, label='loss history layer normalization')
plt.xlabel('Iteration')
plt.legend(loc='lower left')
plt.grid(linestyle='--', linewidth=0.5)

plt.subplot(2, 1, 2)
plt.title('Accuracy')
plt.plot(solver.train_acc_history, '-o', label='train baseline')
plt.plot(solver.val_acc_history, '-o', label='val baseline')
plt.plot(solver_norm.train_acc_history, '-o', label='train layer norm')
plt.plot(solver_norm.val_acc_history, '-o', label='val layer norm')

plt.xlabel('Epoch')
plt.legend(loc='lower right')
plt.gcf().set_size_inches(15, 12)
plt.grid(linestyle='--', linewidth=0.5)
plt.show()
```

Layer normalization works well with small number of features, b/c computing avg, stddev does not change behavior if features are wide or narrow.

```python
np.random.seed(231)

# Try training a network with narrow features.
hidden_dims = [100, 100, 100, 100, 100]
num_train = 1000
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'],
}
n_epochs=10
weight_scale = 2e-2
batch_size = 50
lr = 10**(-3.5)
reg = 1e-1

model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, reg=reg)
model_norm = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization='layernorm', reg=reg)

solver = Solver(model, small_data,
                num_epochs=n_epochs, batch_size=batch_size,
                update_rule='adam',
                optim_config={
                  'learning_rate': lr,
                },
                verbose=True)
solver_norm = Solver(model_norm, small_data,
                num_epochs=n_epochs, batch_size=batch_size,
                update_rule='adam',
                optim_config={
                  'learning_rate': lr,
                },
                verbose=True)

solver.train()
solver_norm.train()
```

    (Iteration 1 / 200) loss: 9.249775
    (Epoch 0 / 10) train acc: 0.100000; val_acc: 0.106000
    (Iteration 11 / 200) loss: 7.671534
    (Epoch 1 / 10) train acc: 0.160000; val_acc: 0.140000
    (Iteration 21 / 200) loss: 6.424086
    (Iteration 31 / 200) loss: 5.461351
    (Epoch 2 / 10) train acc: 0.107000; val_acc: 0.102000
    (Iteration 41 / 200) loss: 4.725373
    (Iteration 51 / 200) loss: 4.165270
    (Epoch 3 / 10) train acc: 0.102000; val_acc: 0.107000
    (Iteration 61 / 200) loss: 3.738790
    (Iteration 71 / 200) loss: 3.411483
    (Epoch 4 / 10) train acc: 0.102000; val_acc: 0.107000
    (Iteration 81 / 200) loss: 3.162345
    (Iteration 91 / 200) loss: 2.969687
    (Epoch 5 / 10) train acc: 0.099000; val_acc: 0.078000
    (Iteration 101 / 200) loss: 2.820878
    (Iteration 111 / 200) loss: 2.704743
    (Epoch 6 / 10) train acc: 0.099000; val_acc: 0.078000
    (Iteration 121 / 200) loss: 2.616207
    (Iteration 131 / 200) loss: 2.547250
    (Epoch 7 / 10) train acc: 0.112000; val_acc: 0.119000
    (Iteration 141 / 200) loss: 2.492123
    (Iteration 151 / 200) loss: 2.451685
    (Epoch 8 / 10) train acc: 0.112000; val_acc: 0.119000
    (Iteration 161 / 200) loss: 2.419063
    (Iteration 171 / 200) loss: 2.392647
    (Epoch 9 / 10) train acc: 0.112000; val_acc: 0.119000
    (Iteration 181 / 200) loss: 2.372319
    (Iteration 191 / 200) loss: 2.356901
    (Epoch 10 / 10) train acc: 0.112000; val_acc: 0.119000
    (Iteration 1 / 200) loss: 9.231841
    (Epoch 0 / 10) train acc: 0.106000; val_acc: 0.121000
    (Iteration 11 / 200) loss: 8.008963
    (Epoch 1 / 10) train acc: 0.293000; val_acc: 0.218000
    (Iteration 21 / 200) loss: 6.890008
    (Iteration 31 / 200) loss: 5.937561
    (Epoch 2 / 10) train acc: 0.382000; val_acc: 0.260000
    (Iteration 41 / 200) loss: 5.242765
    (Iteration 51 / 200) loss: 4.587539
    (Epoch 3 / 10) train acc: 0.483000; val_acc: 0.336000
    (Iteration 61 / 200) loss: 4.154358
    (Iteration 71 / 200) loss: 3.817811
    (Epoch 4 / 10) train acc: 0.510000; val_acc: 0.294000
    (Iteration 81 / 200) loss: 3.452722
    (Iteration 91 / 200) loss: 3.180981
    (Epoch 5 / 10) train acc: 0.558000; val_acc: 0.311000
    (Iteration 101 / 200) loss: 3.326202
    (Iteration 111 / 200) loss: 2.814975
    (Epoch 6 / 10) train acc: 0.621000; val_acc: 0.336000
    (Iteration 121 / 200) loss: 2.666224
    (Iteration 131 / 200) loss: 2.618610
    (Epoch 7 / 10) train acc: 0.601000; val_acc: 0.326000
    (Iteration 141 / 200) loss: 2.450693
    (Iteration 151 / 200) loss: 2.388245
    (Epoch 8 / 10) train acc: 0.630000; val_acc: 0.301000
    (Iteration 161 / 200) loss: 2.179345
    (Iteration 171 / 200) loss: 2.221115
    (Epoch 9 / 10) train acc: 0.692000; val_acc: 0.343000
    (Iteration 181 / 200) loss: 2.141735
    (Iteration 191 / 200) loss: 1.992628
    (Epoch 10 / 10) train acc: 0.699000; val_acc: 0.316000

```python
plt.subplot(2, 1, 1)
plt.title('Training loss')
plt.plot(solver.loss_history, label='loss history baseline')
plt.plot(solver_norm.loss_history, label='loss history layer normalization')
plt.xlabel('Iteration')
plt.legend(loc='lower left')
plt.grid(linestyle='--', linewidth=0.5)

plt.subplot(2, 1, 2)
plt.title('Accuracy')
plt.plot(solver.train_acc_history, '-o', label='train baseline')
plt.plot(solver.val_acc_history, '-o', label='val baseline')
plt.plot(solver_norm.train_acc_history, '-o', label='train layer norm')
plt.plot(solver_norm.val_acc_history, '-o', label='val layer norm')

plt.xlabel('Epoch')
plt.legend(loc='lower right')
plt.gcf().set_size_inches(15, 12)
plt.grid(linestyle='--', linewidth=0.5)
plt.show()
```

Experiment shows layer normalization will work well with high regularization input.


# Day 9 Assignment 1

write a python function for finding is a given number prime or not do unit Testing on it using
pylint and unit testing

```python
#%%write prime.py

"To check prime number"
! pip install pylint
import sympy
def checkprimenumber(num):

    "function to check whether a given a given number is prime or not"
    if sympy.isprime(num):
        print("prime number")
    else:
        print("not a prime number")

```

    Requirement already satisfied: pylint in c:\users\supp\anaconda3\lib\site-packages (2.5.3)
    Requirement already satisfied: astroid<=2.5,>=2.4.0 in c:\users\supp\anaconda3\lib\site-packages (from pylint) (2.4.2)
    Requirement already satisfied: toml>=0.7.1 in c:\users\supp\anaconda3\lib\site-packages (from pylint) (0.10.1)
    Requirement already satisfied: mccabe<0.7,>=0.6 in c:\users\supp\anaconda3\lib\site-packages (from pylint) (0.6.1)
    Requirement already satisfied: isort<5,>=4.2.5 in c:\users\supp\anaconda3\lib\site-packages (from pylint) (4.3.21)
    Requirement already satisfied: colorama; sys_platform == "win32" in c:\users\supp\anaconda3\lib\site-packages (from pylint) (0.4.3)
    Requirement already satisfied: six~=1.12 in c:\users\supp\anaconda3\lib\site-packages (from astroid<=2.5,>=2.4.0->pylint) (1.15.0)
    Requirement already satisfied: lazy-object-proxy==1.4.* in c:\users\supp\anaconda3\lib\site-packages (from astroid<=2.5,>=2.4.0->pylint) (1.4.3)
    Requirement already satisfied: wrapt~=1.11 in c:\users\supp\anaconda3\lib\site-packages (from astroid<=2.5,>=2.4.0->pylint) (1.11.2)

```python
%%writefile checkprime.py
import unittest
import prime
class TestPrime(unittest.TestCase):
    def primenumber(self):
        num = 7
        result = prime.checkprimenumber(num)
        self.assertEqual(result, "Prime Number")
    def notprimenumber(self):
        num = 8
        result = prime.checkprimenumber(num)
        self.assertEqual(result, "Not A Prime Number")
if _name_ == "_main_":
    unittest.main()
```

    Writing checkprime.py

```python

```