Skip to content

Commit 7b7123a

Browse files
authored
Add files via upload
week 6 and 7 added
1 parent cc5af67 commit 7b7123a

20 files changed

+2289
-0
lines changed

MLiP-week06-07/06-07 Linear Classification.ipynb

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

MLiP-week06-07/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-week06-07/imgs/Linear2.png

134 KB
Loading

MLiP-week06-07/imgs/Linear3.png

137 KB
Loading
Loading

MLiP-week06-07/imgs/Linear_4.png

424 KB
Loading
112 KB
Loading

MLiP-week06-07/imgs/classifydemo.jpeg

72.8 KB
Loading
105 KB
Loading
108 KB
Loading
88.7 KB
Loading
Loading

MLiP-week06-07/imgs/linear1.png

125 KB
Loading

MLiP-week06-07/imgs/loss.png

166 KB
Loading

MLiP-week06-07/imgs/margin.jpg

13 KB
Loading
128 KB
Loading

MLiP-week06-07/imgs/svm-loss.jpg

143 KB
Loading
146 KB
Loading

MLiP-week06-07/imgs/wb.jpeg

82.4 KB
Loading

MLiP-week06-07/layers.py

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
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 svm_loss_naive(scores, y, W, reg=1e-3):
52+
"""
53+
Naive implementation of SVM loss function.
54+
55+
Inputs:
56+
- scores: scores for all training data (N, C)
57+
- y: correct labels for the training data
58+
- reg: regularization strength (lambd)
59+
60+
Outputs:
61+
- loss: data loss plus L2 regularization loss
62+
- grads: graidents of loss wrt scores
63+
"""
64+
65+
N, C = scores.shape
66+
67+
# Compute svm data loss
68+
loss = 0.0
69+
for i in range(N):
70+
s = scores[i] # scores for the ith data
71+
correct_class = y[i] # correct class score
72+
73+
for j in range(C):
74+
if j == y[i]:
75+
continue
76+
else:
77+
# loss += max(0, s[j] - s[correct_class] + 1.0)
78+
margin = s[j] - s[correct_class] + 1.0
79+
if margin > 0:
80+
loss += margin
81+
loss /= N
82+
83+
# Adding L2-regularization loss
84+
loss += 0.5 * reg * np.sum(W * W)
85+
86+
# Compute gradient off loss function w.r.t. scores
87+
# We will write this part later
88+
grads = {}
89+
90+
return loss, grads
91+
92+
def svm_loss_half_vectorized(scores, y, W, reg=1e-3):
93+
"""
94+
Half-vectorized implementation of SVM loss function.
95+
96+
Inputs:
97+
- scores: scores for all training data (N, C)
98+
- y: correct labels for the training data
99+
- reg: regularization strength (lambd)
100+
101+
Outputs:
102+
- loss: data loss plus L2 regularization loss
103+
- grads: graidents of loss wrt scores
104+
"""
105+
106+
N, C = scores.shape
107+
108+
# Compute svm data loss
109+
loss = 0.0
110+
for i in range(N):
111+
s = scores[i] # scores for the ith data
112+
correct_class = y[i] # correct class score
113+
114+
margins = np.maximum(0.0, s - s[correct_class] + 1.0)
115+
margins[correct_class] = 0.0
116+
loss += np.sum(margins)
117+
118+
loss /= N
119+
120+
# Adding L2-regularization loss
121+
loss += 0.5 * reg * np.sum(W * W)
122+
123+
# Compute gradient off loss function w.r.t. scores
124+
# We will write this part later
125+
grads = {}
126+
127+
return loss, grads
128+
129+
130+
def svm_loss(scores, y, W, reg=1e-3):
131+
"""
132+
Fully-vectorized implementation of SVM loss function.
133+
134+
Inputs:
135+
- scores: scores for all training data (N, C)
136+
- y: correct labels for the training data
137+
- reg: regularization strength (lambd)
138+
139+
Outputs:
140+
- loss: data loss plus L2 regularization loss
141+
- grads: graidents of loss wrt scores
142+
"""
143+
144+
N = scores.shape[0]
145+
146+
# Compute svm data loss
147+
correct_class_scores = scores[range(N), y]
148+
margins = np.maximum(0.0, scores - correct_class_scores[:, None] + 1.0)
149+
margins[range(N), y] = 0.0
150+
loss = np.sum(margins) / N
151+
152+
# Adding L2-regularization loss
153+
loss += 0.5 * reg * np.sum(W * W)
154+
155+
# Compute gradient off loss function w.r.t. scores
156+
# We will write this part later
157+
grads = {}
158+
159+
return loss, grads
160+
161+
162+
def softmax_loss_naive(scores, y, W, reg=1e-3):
163+
"""
164+
Softmax loss function, naive implementation (with loops)
165+
166+
Inputs have dimension D, there are C classes, and we operate on minibatches
167+
of N examples.
168+
169+
Inputs:
170+
- scores: A numpy array of shape (N, C).
171+
- y: A numpy array of shape (N,) containing training labels;
172+
- W: A numpy array of shape (D, C) containing weights.
173+
- reg: (float) regularization strength
174+
175+
Outputs:
176+
- loss as single float
177+
- gradient with respect to weights W; an array of same shape as W
178+
"""
179+
N, C = scores.shape
180+
181+
# compute data loss
182+
loss = 0.0
183+
for i in range(N):
184+
correct_class = y[i]
185+
score = scores[i]
186+
score -= np.max(scores)
187+
exp_score = np.exp(score)
188+
probs = exp_score / np.sum(exp_score)
189+
loss += -np.log(probs[correct_class])
190+
191+
loss /= N
192+
193+
# compute regularization loss
194+
loss += 0.5 * reg * np.sum(W * W)
195+
196+
# Compute gradient off loss function w.r.t. scores
197+
# We will write this part later
198+
grads = {}
199+
200+
return loss, grads
201+
202+
203+
def softmax_loss(scores, y, W, reg=1e-3):
204+
"""
205+
Softmax loss function, naive implementation (with loops)
206+
207+
Inputs have dimension D, there are C classes, and we operate on minibatches
208+
of N examples.
209+
210+
Inputs:
211+
- scores: A numpy array of shape (N, C).
212+
- y: A numpy array of shape (N,) containing training labels;
213+
- W: A numpy array of shape (D, C) containing weights.
214+
- reg: (float) regularization strength
215+
216+
Outputs:
217+
- loss as single float
218+
- gradient with respect to weights W; an array of same shape as W
219+
"""
220+
N = scores.shape[0] # number of input data
221+
222+
# compute data loss
223+
scores -= np.max(scores, axis=1, keepdims=True)
224+
exp_scores = np.exp(scores)
225+
probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
226+
loss = -np.sum(np.log(probs[range(N), y])) / N
227+
228+
# compute regularization loss
229+
loss += 0.5 * reg * np.sum(W * W)
230+
231+
# Compute gradient off loss function w.r.t. scores
232+
# We will write this part later
233+
grads = {}
234+
235+
return loss, grads

0 commit comments

Comments
 (0)