Skip to content

Commit 75ef197

Browse files
authored
Add files via upload
Week 9 and 10 added
1 parent f566a2f commit 75ef197

37 files changed

+5359
-0
lines changed

MLiP-week09/09 Advanced Topic in ANN.ipynb

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

MLiP-week09/data_utils.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from six.moves import cPickle as pickle
2+
import numpy as np
3+
import os
4+
from scipy.misc import imread
5+
import platform
6+
7+
def load_pickle(f):
8+
version = platform.python_version_tuple()
9+
if version[0] == '2':
10+
return pickle.load(f)
11+
elif version[0] == '3':
12+
return pickle.load(f, encoding='latin1')
13+
raise ValueError("invalid python version: {}".format(version))
14+
15+
def load_CIFAR_batch(filename):
16+
""" load single batch of cifar """
17+
with open(filename, 'rb') as f:
18+
datadict = load_pickle(f)
19+
X = datadict['data']
20+
Y = datadict['labels']
21+
X = X.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float")
22+
Y = np.array(Y)
23+
return X, Y
24+
25+
def load_CIFAR10(ROOT):
26+
""" load all of cifar """
27+
xs = []
28+
ys = []
29+
for b in range(1,6):
30+
f = os.path.join(ROOT, 'data_batch_%d' % (b, ))
31+
X, Y = load_CIFAR_batch(f)
32+
xs.append(X)
33+
ys.append(Y)
34+
Xtr = np.concatenate(xs)
35+
Ytr = np.concatenate(ys)
36+
del X, Y
37+
Xte, Yte = load_CIFAR_batch(os.path.join(ROOT, 'test_batch'))
38+
return Xtr, Ytr, Xte, Yte
39+
40+
41+
def get_CIFAR10_data(cifar10_dir, num_training=49000, num_validation=1000, num_test=1000,
42+
subtract_mean=True):
43+
"""
44+
Load the CIFAR-10 dataset from disk and perform preprocessing to prepare
45+
it for classifiers. These are the same steps as we used for the SVM, but
46+
condensed to a single function.
47+
"""
48+
# Load the raw CIFAR-10 data
49+
X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)
50+
51+
# Subsample the data
52+
mask = list(range(num_training, num_training + num_validation))
53+
X_val = X_train[mask]
54+
y_val = y_train[mask]
55+
mask = list(range(num_training))
56+
X_train = X_train[mask]
57+
y_train = y_train[mask]
58+
mask = list(range(num_test))
59+
X_test = X_test[mask]
60+
y_test = y_test[mask]
61+
62+
# Normalize the data: subtract the mean image
63+
if subtract_mean:
64+
mean_image = np.mean(X_train, axis=0)
65+
X_train -= mean_image
66+
X_val -= mean_image
67+
X_test -= mean_image
68+
69+
# Transpose so that channels come first
70+
X_train = X_train.transpose(0, 3, 1, 2).copy()
71+
X_val = X_val.transpose(0, 3, 1, 2).copy()
72+
X_test = X_test.transpose(0, 3, 1, 2).copy()
73+
74+
# Package data into a dictionary
75+
return {
76+
'X_train': X_train, 'y_train': y_train,
77+
'X_val': X_val, 'y_val': y_val,
78+
'X_test': X_test, 'y_test': y_test,
79+
}

MLiP-week09/fc_net.py

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import numpy as np
2+
from layers import *
3+
4+
5+
class FullyConnectedNet(object):
6+
"""
7+
A fully-connected neural network with an arbitrary number of hidden layers,
8+
ReLU nonlinearities, and a softmax loss function. This will also implement
9+
dropout and batch normalization as options. For a network with L layers,
10+
the architecture will be
11+
12+
{affine - [batch norm] - relu - [dropout]} x (L - 1) - affine - softmax
13+
14+
where batch normalization and dropout are optional, and the {...} block is
15+
repeated L - 1 times.
16+
17+
Similar to the TwoLayerNet above, learn-able parameters are stored in the
18+
self.params dictionary and will be learned using the Solver class.
19+
"""
20+
21+
def __init__(self, hidden_dims, input_dim=3*32*32, num_classes=10,
22+
dropout=0, use_batchnorm=False, reg=0.0,
23+
weight_scale=1e-2, dtype=np.float32, seed=None):
24+
"""
25+
Initialize a new FullyConnectedNet.
26+
27+
Inputs:
28+
- hidden_dims: A list of integers giving the size of each hidden layer.
29+
- input_dim: An integer giving the size of the input.
30+
- num_classes: An integer giving the number of classes to classify.
31+
- dropout: Scalar between 0 and 1 giving dropout strength. If dropout=0 then
32+
the network should not use dropout at all.
33+
- use_batchnorm: Whether or not the network should use batch normalization.
34+
- reg: Scalar giving L2 regularization strength.
35+
- weight_scale: Scalar giving the standard deviation for random
36+
initialization of the weights.
37+
- dtype: A numpy data-type object; all computations will be performed using
38+
this data-type. float32 is faster but less accurate, so you should use
39+
float64 for numeric gradient checking.
40+
- seed: If not None, then pass this random seed to the dropout layers. This
41+
will make the dropout layers deterministic so we can gradient check the
42+
model.
43+
"""
44+
self.use_batchnorm = use_batchnorm
45+
self.use_dropout = dropout > 0
46+
self.reg = reg
47+
self.num_layers = 1 + len(hidden_dims)
48+
self.dtype = dtype
49+
self.params = {}
50+
51+
dims = [input_dim] + hidden_dims + [num_classes]
52+
for i in range(1, self.num_layers + 1):
53+
self.params['W%d' %i] = weight_scale * np.random.randn(dims[i - 1], dims[i])
54+
self.params['b%d' %i] = np.zeros(dims[i])
55+
if i < self.num_layers and self.use_batchnorm:
56+
self.params['gamma%d' %i] = np.ones(dims[i])
57+
self.params['beta%d' %i] = np.zeros(dims[i])
58+
59+
# When using dropout we need to pass a dropout_param dictionary to each
60+
# dropout layer so that the layer knows the dropout probability and the mode
61+
# (train / test). You can pass the same dropout_param to each dropout layer.
62+
self.dropout_param = {}
63+
if self.use_dropout:
64+
self.dropout_param = {'mode': 'train', 'p': dropout}
65+
if seed is not None:
66+
self.dropout_param['seed'] = seed
67+
68+
# With batch normalization we need to keep track of running means and
69+
# variances, so we need to pass a special bn_param object to each batch
70+
# normalization layer. You should pass self.bn_params[0] to the forward pass
71+
# of the first batch normalization layer, self.bn_params[1] to the forward
72+
# pass of the second batch normalization layer, etc.
73+
self.bn_params = []
74+
if self.use_batchnorm:
75+
self.bn_params = [{'mode': 'train'} for i in range(self.num_layers - 1)]
76+
77+
# Cast all parameters to the correct data-type
78+
for k, v in self.params.items():
79+
self.params[k] = v.astype(dtype)
80+
81+
def loss(self, X, y=None):
82+
"""
83+
Compute loss and gradient for the fully-connected net.
84+
85+
Input / output: Same as TwoLayerNet above.
86+
"""
87+
X = X.astype(self.dtype)
88+
mode = 'test' if y is None else 'train'
89+
90+
# Set train/test mode for batchnorm params and dropout param since they
91+
# behave differently during training and testing.
92+
if self.use_dropout:
93+
self.dropout_param['mode'] = mode
94+
if self.use_batchnorm:
95+
for bn_param in self.bn_params:
96+
bn_param['mode'] = mode
97+
98+
scores = None
99+
100+
cache = {}
101+
a_cache, relu_cache, bn_cache, d_cache = {}, {}, {}, {}
102+
h = X
103+
for i in range(1, self.num_layers + 1):
104+
W, b = self.params['W%d' % i], self.params['b%d' % i]
105+
if i < self.num_layers:
106+
if self.use_batchnorm:
107+
gamma, beta = self.params['gamma%d' % i], self.params['beta%d' % i]
108+
h, a_cache[i] = affine_forward(h, W, b)
109+
h, bn_cache[i] = batchnorm_forward(h, gamma, beta, self.bn_params[i - 1])
110+
h, relu_cache[i] = relu_forward(h)
111+
else:
112+
h, cache[i] = affine_relu_forward(h, W, b)
113+
if self.use_dropout:
114+
h, d_cache[i] = dropout_forward(h, self.dropout_param)
115+
else:
116+
scores, cache[i] = affine_forward(h, W, b)
117+
118+
# If test mode return early
119+
if mode == 'test':
120+
return scores
121+
122+
loss, grads = 0.0, {}
123+
124+
loss, dscores = softmax_loss(scores, y)
125+
126+
# backward pass
127+
dout = dscores
128+
for i in reversed(range(1, self.num_layers + 1)):
129+
if i < self.num_layers:
130+
if self.use_dropout:
131+
dout = dropout_backward(dout, d_cache[i])
132+
if self.use_batchnorm:
133+
dout = relu_backward(dout, relu_cache[i])
134+
dout, grads['gamma%d' % i], grads['beta%d' % i] = batchnorm_backward(dout, bn_cache[i])
135+
dout, grads['W%d' % i], grads['b%d' % i] = affine_backward(dout, a_cache[i])
136+
else:
137+
dout, grads['W%d' % i], grads['b%d' % i] = affine_relu_backward(dout, cache[i])
138+
else:
139+
dout, grads['W%d' % i], grads['b%d' %i] = affine_backward(dout, cache[i])
140+
141+
for i in range(1, self.num_layers):
142+
W = self.params['W%d' % i]
143+
loss += 0.5 * self.reg * np.sum(W * W)
144+
grads['W%d' % i] += self.reg * W
145+
146+
return loss, grads

MLiP-week09/imgs/09-Augmentation.jpg

119 KB
Loading
140 KB
Loading
591 KB
Loading

MLiP-week09/imgs/09-Dropout.jpg

131 KB
Loading

MLiP-week09/imgs/09-SGD_problem1.jpg

75.8 KB
Loading

MLiP-week09/imgs/09-SGD_problem2.jpg

97.3 KB
Loading

MLiP-week09/imgs/09-color_jitter.jpg

65.2 KB
Loading

MLiP-week09/imgs/09-crop.jpg

116 KB
Loading

MLiP-week09/imgs/09-flip.jpg

95.2 KB
Loading

MLiP-week09/imgs/09-momentum.jpg

102 KB
Loading
39.9 KB
Loading

MLiP-week09/imgs/09-two-layer-NN.jpg

29.6 KB
Loading

MLiP-week09/imgs/09_batch_norm.jpg

68 KB
Loading

MLiP-week09/imgs/09_batch_norm.png

79.5 KB
Loading
61.6 KB
Loading

MLiP-week09/imgs/next_week.png

10.4 KB
Loading

0 commit comments

Comments
 (0)