Skip to content

Commit 13b00fa

Browse files
committed
start exploration of dropout
1 parent 328a899 commit 13b00fa

File tree

16 files changed

+6705
-306
lines changed

16 files changed

+6705
-306
lines changed

assignment2/TensorFlow.ipynb

Lines changed: 1819 additions & 306 deletions
Large diffs are not rendered by default.

assignment2/cifar/__init__.py

Whitespace-only changes.

assignment2/cifar/loader.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import numpy as np
2+
from cs231n.data_utils import load_CIFAR10
3+
4+
5+
def get_CIFAR10_data(num_training=49000, num_validation=1000, num_test=10000):
6+
"""
7+
Load the CIFAR-10 dataset from disk and perform preprocessing to prepare
8+
it for the two-layer neural net classifier. These are the same steps as
9+
we used for the SVM, but condensed to a single function.
10+
"""
11+
# Load the raw CIFAR-10 data
12+
cifar10_dir = './cs231n/datasets/cifar-10-batches-py'
13+
X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)
14+
15+
# Subsample the data
16+
mask = range(num_training, num_training + num_validation)
17+
X_val = X_train[mask]
18+
y_val = y_train[mask]
19+
mask = range(num_training)
20+
X_train = X_train[mask]
21+
y_train = y_train[mask]
22+
mask = range(num_test)
23+
X_test = X_test[mask]
24+
y_test = y_test[mask]
25+
26+
# Normalize the data: subtract the mean image
27+
mean_image = np.mean(X_train, axis=0)
28+
X_train -= mean_image
29+
X_val -= mean_image
30+
X_test -= mean_image
31+
32+
return X_train, y_train, X_val, y_val, X_test, y_test

assignment2/cifar10_exploration_dropout.ipynb

Lines changed: 4510 additions & 0 deletions
Large diffs are not rendered by default.

assignment2/nn/__init__.py

Whitespace-only changes.

assignment2/nn/affine.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import numpy as np
2+
import tensorflow as tf
3+
4+
5+
def affine(idx, X, output_size):
6+
input_size = np.prod(X.shape[1:])
7+
W = tf.get_variable(f'W{idx}', shape=[input_size, output_size])
8+
b = tf.get_variable(f'b{idx}', shape=[output_size])
9+
if len(X.shape) > 3:
10+
out = tf.reshape(X, [-1, input_size])
11+
else:
12+
out = X
13+
out = tf.matmul(out, W) + b
14+
return out, [W, b]

assignment2/nn/cnn.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import numpy as np
2+
import tensorflow as tf
3+
4+
5+
def cnn(idx, X, filters, kernel_size, is_training, strides=(1, 1, 1, 1), padding='SAME', use_batchnorm=True):
6+
input_filter_size = X.shape[-1]
7+
Wconv = tf.get_variable(f'Wconv{idx}', shape=[*kernel_size, input_filter_size, filters])
8+
bconv = tf.get_variable(f'bconv{idx}', shape=[filters])
9+
out = tf.nn.conv2d(X, filter=Wconv, strides=strides, padding=padding) + bconv
10+
11+
# ReLU Activation Layer
12+
out = tf.nn.relu(out)
13+
14+
# Spatial Batch Normalization Layer (trainable parameters, with scale and centering)
15+
# axis=3 channel axis
16+
if use_batchnorm:
17+
out = tf.layers.batch_normalization(out, axis=3, training=is_training)
18+
return out, [Wconv, bconv]

assignment2/nn_visualization/__init__.py

Whitespace-only changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#
2+
# links for inspiration
3+
# - https://medium.com/@awjuliani/visualizing-neural-network-layer-activation-tensorflow-tutorial-d45f8bf7bbc4
4+
# simple snippet to train and visualize cnn layers of simple model
5+
#
6+
7+
import math
8+
import matplotlib.pyplot as plt
9+
from nn_visualization.models import get_model_name
10+
11+
12+
def draw_layers_of_models(models, **kwargs):
13+
img_idx = 0
14+
for m in models:
15+
img_idx = draw_layers(m, img_idx, **kwargs)
16+
17+
18+
def draw_layers(m, img_idx):
19+
if 'params' not in m:
20+
return
21+
22+
print(f'Model {get_model_name(m)}')
23+
for (layer_tf, layer) in m['params'].items():
24+
draw_layer(layer, str(layer_tf.name), img_idx)
25+
img_idx += layer.shape[3]
26+
27+
return img_idx
28+
29+
30+
def draw_layer(layer, title, img_idx):
31+
filters = layer.shape[3]
32+
n_columns = 6
33+
n_rows = math.ceil(filters / n_columns) + 1
34+
35+
fig = plt.figure(figsize=(20, 20))
36+
fig.suptitle(title, size=16)
37+
for idx in range(filters):
38+
ax = fig.add_subplot(n_rows, n_columns, idx + 1)
39+
ax.set_title(f'Filter {idx}')
40+
img = layer[:, :, :, idx]
41+
img_min = img.min()
42+
delta = img.max() - img_min
43+
img = (img - img_min) / delta
44+
plt.imshow(img, interpolation='nearest')
45+
plt.show()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# import tensorflow as tf
2+
# import numpy as np
3+
# import math
4+
# import time
5+
# import timeit
6+
import matplotlib.pyplot as plt
7+
from IPython.display import display, HTML
8+
9+
from nn_visualization.models import get_model_name
10+
11+
12+
def show_graphs(models, ymax=5):
13+
"""
14+
TODO:
15+
- show 2 graphs: training and validation
16+
- show accuracy
17+
"""
18+
19+
# plt.gcf().set_size_inches(15, 12)
20+
21+
# training dynamic
22+
plt.grid(True)
23+
plt.title('Training Dynamics')
24+
plt.xlabel('minibatch iteration')
25+
plt.ylabel('minibatch loss')
26+
# plt.xlim(xmin=50)
27+
plt.ylim(ymax=ymax)
28+
29+
for m in models:
30+
name = get_model_name(m)
31+
plt.plot(m['res']['training']['losses'], label=name)
32+
33+
plt.legend()
34+
plt.show()

0 commit comments

Comments
 (0)