Skip to content

Commit 9320724

Browse files
committed
Refactoring
1 parent b61bfc0 commit 9320724

File tree

5 files changed

+32
-35
lines changed

5 files changed

+32
-35
lines changed

load_MNIST_images.py renamed to load_MNIST.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,23 @@ def load_MNIST_images(filename):
2020

2121
f.close()
2222

23-
return images
23+
return images
24+
25+
26+
def load_MNIST_labels(filename):
27+
"""
28+
returns a [number of MNIST images]x1 matrix containing
29+
the labels for the MNIST images
30+
31+
:param filename: input file with labels
32+
"""
33+
with open(filename, 'r') as f:
34+
magic = np.fromfile(f, dtype=np.dtype('>i4'), count=1)
35+
36+
num_labels = np.fromfile(f, dtype=np.dtype('>i4'), count=1)
37+
38+
labels = np.fromfile(f, dtype=np.ubyte)
39+
40+
f.close()
41+
42+
return labels

load_MNIST_labels.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

softmax_exercise.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import load_MNIST_images
2-
import load_MNIST_labels
1+
import load_MNIST
32
import numpy as np
43
import softmax
54
import compute_gradient
@@ -33,8 +32,8 @@
3332
# On some platforms, the files might be saved as
3433
# train-images.idx3-ubyte / train-labels.idx1-ubyte
3534

36-
images = load_MNIST_images.load_MNIST_images('data/mnist/train-images-idx3-ubyte')
37-
labels = load_MNIST_labels.load_MNIST_labels('data/mnist/train-labels-idx1-ubyte')
35+
images = load_MNIST.load_MNIST_images('data/mnist/train-images-idx3-ubyte')
36+
labels = load_MNIST.load_MNIST_labels('data/mnist/train-labels-idx1-ubyte')
3837

3938
if debug:
4039
input_size = 8 * 8
@@ -93,7 +92,7 @@
9392
# (in softmaxPredict.m), which should return predictions
9493
# given a softmax model and the input data.
9594

96-
test_images = load_MNIST_images.load_MNIST_images('data/mnist/t10k-images.idx3-ubyte')
97-
test_labels = load_MNIST_labels.load_MNIST_labels('data/mnist/t10k-labels.idx1-ubyte')
95+
test_images = load_MNIST.load_MNIST_images('data/mnist/t10k-images.idx3-ubyte')
96+
test_labels = load_MNIST_labels('data/mnist/t10k-labels.idx1-ubyte')
9897
predictions = softmax.softmax_predict((opt_theta, input_size, num_classes), test_images)
9998
print "Accuracy: {0:.2f}%".format(100 * np.sum(predictions == test_labels, dtype=np.float64) / test_labels.shape[0])

stl_exercise.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import load_MNIST_labels
2-
import load_MNIST_images
1+
import load_MNIST
32
import numpy as np
43
import sparse_autoencoder
54
import scipy.optimize
@@ -26,8 +25,8 @@
2625
# We have sorted the data for you in this so that you will not have to
2726
# change it.
2827

29-
images = load_MNIST_images.load_MNIST_images('data/mnist/train-images-idx3-ubyte')
30-
labels = load_MNIST_labels.load_MNIST_labels('data/mnist/train-labels-idx1-ubyte')
28+
images = load_MNIST.load_MNIST_images('data/mnist/train-images-idx3-ubyte')
29+
labels = load_MNIST.load_MNIST_labels('data/mnist/train-labels-idx1-ubyte')
3130

3231
unlabeled_index = np.argwhere(labels >= 5).flatten()
3332
labeled_index = np.argwhere(labels < 5).flatten()
@@ -60,7 +59,7 @@
6059
lambda_, sparsity_param,
6160
beta, unlabeled_data)
6261

63-
options_ = {'maxiter': 400, 'disp': True}
62+
options_ = {'maxiter': 100, 'disp': True}
6463
result = scipy.optimize.minimize(J, theta, method='L-BFGS-B', jac=True, options=options_)
6564
opt_theta = result.x
6665

@@ -86,7 +85,7 @@
8685
## STEP 4: Train the softmax classifier
8786

8887
lambda_ = 1e-4
89-
options_ = {'maxiter': 400, 'disp': True}
88+
options_ = {'maxiter': 100, 'disp': True}
9089

9190
opt_theta, input_size, num_classes = softmax.softmax_train(hidden_size, num_labels,
9291
lambda_, train_features,

train.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import check_gradient
77
import compute_gradient
88
import display_network
9-
import load_MNIST_images
9+
import load_MNIST
1010

1111

1212
##======================================================================
@@ -41,7 +41,7 @@
4141
# patches = sample_images.sample_images()
4242

4343
# Loading 10K images from MNIST database
44-
images = load_MNIST_images.load_MNIST_images('data/mnist/train-images-idx3-ubyte')
44+
images = load_MNIST.load_MNIST_images('data/mnist/train-images-idx3-ubyte')
4545
patches = images[:, 0:10000]
4646

4747
# Obtain random parameters theta

0 commit comments

Comments
 (0)