Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

Commit 2179aa6

Browse files
committed
follow comment
1 parent 202184b commit 2179aa6

File tree

13 files changed

+283
-429
lines changed

13 files changed

+283
-429
lines changed

recognize_digits/README.md

Lines changed: 186 additions & 249 deletions
Large diffs are not rendered by default.

recognize_digits/cnn_mnist.py

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

recognize_digits/evaluate.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ def get_best_pass(filename):
3030

3131
filename = sys.argv[1]
3232
log = get_best_pass(filename)
33-
predict_error = math.sqrt(float(log[0])) / 2
3433
classification_accuracy = (1 - float(log[1])) * 100
35-
print 'Best pass is %s, error is %s, which means predict get error as %f' % (
36-
log[2], log[0], predict_error)
34+
print 'Best pass is %s, testing Avgcost is %s' % (log[2], log[0])
3735
print 'The classification accuracy is %.2f%%' % classification_accuracy
-10.7 KB
Loading
-2.76 KB
Loading
-6.1 KB
Loading

recognize_digits/load_data.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,17 @@
1717

1818

1919
def read_data(path, filename):
20-
imgf = path + filename + "-images-idx3-ubyte"
21-
labelf = path + filename + "-labels-idx1-ubyte"
22-
f = open(imgf, "rb")
23-
l = open(labelf, "rb")
24-
25-
f.read(16)
26-
l.read(8)
27-
28-
# Define number of samples for train/test
29-
n = 60000 if "train" in filename else 10000
30-
31-
rows = 28
32-
cols = 28
33-
34-
images = np.fromfile(
35-
f, 'ubyte',
36-
count=n * rows * cols).reshape(n, rows, cols).astype('float32')
37-
labels = np.fromfile(l, 'ubyte', count=n).astype("int")
20+
with open(path + filename + "-images-idx3-ubyte",
21+
"rb") as f: # open picture file
22+
magic, n, rows, cols = struct.upack(">IIII", f.read(16))
23+
images = np.fromfile(
24+
f, 'ubyte',
25+
count=n * rows * cols).reshape(n, rows, cols).astype('float32')
26+
27+
with open(path + filename + "-labels-idx1-ubyte",
28+
"rb") as l: # open label file
29+
magic, n = struct.upack(">II", l.read(8))
30+
labels = np.fromfile(l, 'ubyte', count=n).astype("int")
3831

3932
return images, labels
4033

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,53 @@
3939
label_size = 10
4040
img = data_layer(name='pixel', size=data_size)
4141

42-
# The first fully-connected layer
43-
hidden1 = fc_layer(input=img, size=128, act=ReluActivation())
44-
# The second fully-connected layer and the according activation function
45-
hidden2 = fc_layer(input=hidden1, size=64, act=ReluActivation())
46-
# The thrid fully-connected layer, note that the hidden size should be 10,
47-
# which is the number of unique digits
48-
predict = fc_layer(input=hidden2, size=10, act=SoftmaxActivation())
42+
43+
def softmax_regression(img):
44+
predict = fc_layer(input=img, size=10, act=SoftmaxActivation())
45+
return predict
46+
47+
48+
def multilayer_perceptron(img):
49+
# The first fully-connected layer
50+
hidden1 = fc_layer(input=img, size=128, act=ReluActivation())
51+
# The second fully-connected layer and the according activation function
52+
hidden2 = fc_layer(input=hidden1, size=64, act=ReluActivation())
53+
# The thrid fully-connected layer, note that the hidden size should be 10,
54+
# which is the number of unique digits
55+
predict = fc_layer(input=hidden2, size=10, act=SoftmaxActivation())
56+
return predict
57+
58+
59+
def convolutional_neural_network(img):
60+
# first conv layer
61+
conv_pool_1 = simple_img_conv_pool(
62+
input=img,
63+
filter_size=5,
64+
num_filters=20,
65+
num_channel=1,
66+
pool_size=2,
67+
pool_stride=2,
68+
act=TanhActivation())
69+
# second conv layer
70+
conv_pool_2 = simple_img_conv_pool(
71+
input=conv_pool_1,
72+
filter_size=5,
73+
num_filters=50,
74+
num_channel=20,
75+
pool_size=2,
76+
pool_stride=2,
77+
act=TanhActivation())
78+
# The first fully-connected layer
79+
fc1 = fc_layer(input=conv_pool_2, size=128, act=TanhActivation())
80+
# The softmax layer, note that the hidden size should be 10,
81+
# which is the number of unique digits
82+
predict = fc_layer(input=fc1, size=10, act=SoftmaxActivation())
83+
return predict
84+
85+
86+
predict = softmax_regression(img)
87+
#predict = multilayer_perceptron(img)
88+
#predict = convolutional_neural_network(img)
4989

5090
if not is_predict:
5191
lbl = data_layer(name="label", size=label_size)

recognize_digits/mnist_provider.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,17 @@
1919
# Define a py data provider
2020
@provider(
2121
input_types={'pixel': dense_vector(28 * 28),
22-
'label': integer_value(10)},
23-
cache=CacheType.CACHE_PASS_IN_MEM)
22+
'label': integer_value(10)})
2423
def process(settings, filename): # settings is not used currently.
25-
imgf = filename + "-images-idx3-ubyte"
26-
labelf = filename + "-labels-idx1-ubyte"
27-
f = open(imgf, "rb")
28-
l = open(labelf, "rb")
24+
with open(filename + "-images-idx3-ubyte", "rb") as f: # open picture file
25+
magic, n, rows, cols = struct.upack(">IIII", f.read(16))
26+
images = np.fromfile(
27+
f, 'ubyte',
28+
count=n * rows * cols).reshape(n, rows, cols).astype('float32')
2929

30-
f.read(16)
31-
l.read(8)
32-
33-
# Define number of samples for train/test
34-
if "train" in filename:
35-
n = 60000
36-
else:
37-
n = 10000
38-
39-
images = numpy.fromfile(
40-
f, 'ubyte', count=n * 28 * 28).reshape((n, 28 * 28)).astype('float32')
41-
images = images / 255.0 * 2.0 - 1.0
42-
labels = numpy.fromfile(l, 'ubyte', count=n).astype("int")
30+
with open(filename + "-labels-idx1-ubyte", "rb") as l: # open label file
31+
magic, n = struct.upack(">II", l.read(8))
32+
labels = np.fromfile(l, 'ubyte', count=n).astype("int")
4333

4434
for i in xrange(n):
4535
yield {"pixel": images[i, :], 'label': labels[i]}
46-
47-
f.close()
48-
l.close()

recognize_digits/plot_error.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,33 @@
2020
def plot_log(filename):
2121
with open(filename, 'r') as f:
2222
text = f.read()
23-
pattern = re.compile('Test.*? cost=([0-9]+\.[0-9]+).*?pass-([0-9]+)',
24-
re.S)
23+
pattern = re.compile(
24+
'AvgCost=([0-9]+\.[0-9]+).*?Test.*? cost=([0-9]+\.[0-9]+).*?pass-([0-9]+)',
25+
re.S)
2526
results = re.findall(pattern, text)
26-
cost, pass_ = zip(*results)
27-
cost_float = map(float, cost)
27+
train_cost, test_cost, pass_ = zip(*results)
28+
train_cost_float = map(float, train_cost)
29+
test_cost_float = map(float, test_cost)
2830
pass_int = map(int, pass_)
29-
plt.plot(pass_int, cost_float, 'bo', pass_, cost_float, 'k')
31+
plt.plot(pass_int, train_cost_float, 'red', label='Train Error')
32+
plt.plot(pass_int, test_cost_float, 'green', label='Test Error')
3033
plt.ylabel('AvgCost')
3134
plt.xlabel('epoch')
35+
36+
# Now add the legend with some customizations.
37+
legend = plt.legend(loc='upper center', shadow=True)
38+
39+
# The frame is matplotlib.patches.Rectangle instance surrounding the legend.
40+
frame = legend.get_frame()
41+
frame.set_facecolor('0.90')
42+
43+
# Set the fontsize
44+
for label in legend.get_texts():
45+
label.set_fontsize('large')
46+
47+
for label in legend.get_lines():
48+
label.set_linewidth(1.5) # the legend line width
49+
3250
plt.show()
3351

3452

0 commit comments

Comments
 (0)