Skip to content

Commit 8eabc05

Browse files
authored
Add files via upload
week 8 added
1 parent 7b7123a commit 8eabc05

8 files changed

+1228
-0
lines changed

MLiP-week08/08 Non-Linear Classification (ANN).ipynb

+1,010
Large diffs are not rendered by default.

MLiP-week08/data_utils.py

+79
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+
}
39.9 KB
Loading

MLiP-week08/imgs/8-1NN.jpg

29.6 KB
Loading
591 KB
Loading
140 KB
Loading

MLiP-week08/imgs/8-4Dropout.jpg

131 KB
Loading

MLiP-week08/layers.py

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import numpy as np
2+
3+
4+
def affine_forward(x, W, b):
5+
"""
6+
A linear mapping from inputs to scores.
7+
8+
Inputs:
9+
- x: input matrix (N, d_1, ..., d_k)
10+
- W: weigh matrix (D, C)
11+
- b: bias vector (C, )
12+
13+
Outputs:
14+
- out: output of linear layer (N, C)
15+
"""
16+
x2d = np.reshape(x, (x.shape[0], -1)) # convert 4D input matrix to 2D
17+
out = np.dot(x2d, W) + b # linear transformation
18+
cache = (x, W, b) # keep for backward step (stay with us)
19+
return out, cache
20+
21+
22+
def affine_backward(dout, cache):
23+
"""
24+
Computes the backward pass for an affine layer.
25+
26+
Inputs:
27+
- dout: Upstream derivative, of shape (N, C)
28+
- cache: Tuple of:
29+
- x: Input data, of shape (N, d_1, ... d_k)
30+
- w: Weights, of shape (D, C)
31+
- b: biases, of shape (C,)
32+
33+
Outputs:
34+
- dx: Gradient with respect to x, of shape (N, d1, ..., d_k)
35+
- dw: Gradient with respect to w, of shape (D, C)
36+
- db: Gradient with respect to b, of shape (C,)
37+
"""
38+
x, w, b = cache
39+
x2d = np.reshape(x, (x.shape[0], -1))
40+
41+
# compute gradients
42+
db = np.sum(dout, axis=0)
43+
dw = np.dot(x2d.T, dout)
44+
dx = np.dot(dout, w.T)
45+
46+
# reshape dx to match the size of x
47+
dx = dx.reshape(x.shape)
48+
49+
return dx, dw, db
50+
51+
def relu_forward(x):
52+
"""Forward pass for a layer of rectified linear units.
53+
54+
Inputs:
55+
- x: a numpy array of any shape
56+
57+
Outputs:
58+
- out: output of relu, same shape as x
59+
- cache: x
60+
"""
61+
cache = x
62+
out = np.maximum(0, x)
63+
return out, cache
64+
65+
def relu_backward(dout, cache):
66+
"""Backward pass for a layer of rectified linear units.
67+
68+
Inputs:
69+
- dout: upstream derevatives, of any shape
70+
- cache: x, same shape as dout
71+
72+
Outputs:
73+
- dx: gradient of loss w.r.t x
74+
"""
75+
x = cache
76+
dx = dout * (x > 0)
77+
return dx
78+
79+
def svm_loss(scores, y):
80+
"""
81+
Fully-vectorized implementation of SVM loss function.
82+
83+
Inputs:
84+
- scores: scores for all training data (N, C)
85+
- y: correct labels for the training data of shape (N,)
86+
87+
Outputs:
88+
- loss: data loss plus L2 regularization loss
89+
- grads: graidents of loss w.r.t scores
90+
"""
91+
92+
N = scores.shape[0]
93+
94+
# Compute svm data loss
95+
correct_class_scores = scores[range(N), y]
96+
margins = np.maximum(0.0, scores - correct_class_scores[:, None] + 1.0)
97+
margins[range(N), y] = 0.0
98+
loss = np.sum(margins) / N
99+
100+
# Compute gradient off loss function w.r.t. scores
101+
num_pos = np.sum(margins > 0, axis=1)
102+
dscores = np.zeros(scores.shape)
103+
dscores[margins > 0] = 1
104+
dscores[range(N), y] -= num_pos
105+
dscores /= N
106+
107+
return loss, dscores
108+
109+
110+
def softmax_loss(scores, y):
111+
"""
112+
Softmax loss function, fully vectorized implementation.
113+
114+
Inputs have dimension D, there are C classes, and we operate on minibatches
115+
of N examples.
116+
117+
Inputs:
118+
- scores: A numpy array of shape (N, C).
119+
- y: A numpy array of shape (N,) containing training labels;
120+
121+
Outputs:
122+
- loss as single float
123+
- gradient with respect to scores
124+
"""
125+
N = scores.shape[0] # number of input data
126+
127+
# compute data loss
128+
shifted_logits = scores - np.max(scores, axis=1, keepdims=True)
129+
Z = np.sum(np.exp(shifted_logits), axis=1, keepdims=True)
130+
log_probs = shifted_logits - np.log(Z)
131+
probs = np.exp(log_probs)
132+
loss = -np.sum(log_probs[range(N), y]) / N
133+
134+
# Compute gradient of loss function w.r.t. scores
135+
dscores = probs.copy()
136+
dscores[range(N), y] -= 1
137+
dscores /= N
138+
139+
return loss, dscores

0 commit comments

Comments
 (0)