-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist_loader.py
148 lines (121 loc) · 6.11 KB
/
mnist_loader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
mnist_loader
~~~~~~~~~~~~
A library to load the MNIST image data. For details of the data
structures that are returned, see the doc strings for ``load_data``
and ``load_data_wrapper``. In practice, ``load_data_wrapper`` is the
function usually called by our neural network code.
"""
#### Libraries
# Standard library
import cPickle
import gzip
from matplotlib import pyplot as plt
import network
import threading
import parameter_server
# Third-party libraries
import numpy as np
def load_data():
"""Return the MNIST data as a tuple containing the training data,
the validation data, and the test data.
The ``training_data`` is returned as a tuple with two entries.
The first entry contains the actual training images. This is a
numpy ndarray with 50,000 entries. Each entry is, in turn, a
numpy ndarray with 784 values, representing the 28 * 28 = 784
pixels in a single MNIST image.
The second entry in the ``training_data`` tuple is a numpy ndarray
containing 50,000 entries. Those entries are just the digit
values (0...9) for the corresponding images contained in the first
entry of the tuple.
The ``validation_data`` and ``test_data`` are similar, except
each contains only 10,000 images.
This is a nice data format, but for use in neural networks it's
helpful to modify the format of the ``training_data`` a little.
That's done in the wrapper function ``load_data_wrapper()``, see
below.
"""
f = gzip.open('data/mnist.pkl.gz', 'rb')
training_data, validation_data, test_data = cPickle.load(f)
f.close()
return (training_data, validation_data, test_data)
def load_data_wrapper():
"""Return a tuple containing ``(training_data, validation_data,
test_data)``. Based on ``load_data``, but the format is more
convenient for use in our implementation of neural networks.
In particular, ``training_data`` is a list containing 50,000
2-tuples ``(x, y)``. ``x`` is a 784-dimensional numpy.ndarray
containing the input image. ``y`` is a 10-dimensional
numpy.ndarray representing the unit vector corresponding to the
correct digit for ``x``.
``validation_data`` and ``test_data`` are lists containing 10,000
2-tuples ``(x, y)``. In each case, ``x`` is a 784-dimensional
numpy.ndarry containing the input image, and ``y`` is the
corresponding classification, i.e., the digit values (integers)
corresponding to ``x``.
Obviously, this means we're using slightly different formats for
the training data and the validation / test data. These formats
turn out to be the most convenient for use in our neural network
code."""
tr_d, va_d, te_d = load_data()
training_inputs = [np.reshape(x, (784, 1)) for x in tr_d[0]]
# for x in tr_d[0]:
# print x.shape
print len(training_inputs)
imageSample = training_inputs[10]
plt.imshow(np.reshape(imageSample, (28,28)))
plt.show()
training_results = [vectorized_result(y) for y in tr_d[1]]
print training_results[10]
training_data = zip(training_inputs, training_results)
validation_inputs = [np.reshape(x, (784, 1)) for x in va_d[0]]
validation_data = zip(validation_inputs, va_d[1])
test_inputs = [np.reshape(x, (784, 1)) for x in te_d[0]]
test_data = zip(test_inputs, te_d[1])
return (training_data, validation_data, test_data)
def vectorized_result(j):
"""Return a 10-dimensional unit vector with a 1.0 in the jth
position and zeroes elsewhere. This is used to convert a digit
(0...9) into a corresponding desired output from the neural
network."""
e = np.zeros((10, 1))
e[j] = 1.0
return e
def participant_manager():
training_data, validation_data, test_data = load_data_wrapper()
server = parameter_server.ParameterServer()
training_data_participant1 = training_data[:5000]
training_data_participant2 = training_data[5000:10000]
training_data_participant3 = training_data[10000:15000]
training_data_participant4 = training_data[15000:20000]
training_data_participant5 = training_data[20000:25000]
training_data_participant6 = training_data[25000:30000]
training_data_participant7 = training_data[30000:35000]
training_data_participant8 = training_data[35000:40000]
training_data_participant9 = training_data[40000:45000]
training_data_participant10 = training_data[45000:50000]
participant1 = threading.Thread(name='participant1', target=ffn, args = (training_data_participant1, test_data, server))
participant2 = threading.Thread(name='participant2', target=ffn, args = (training_data_participant2, test_data, server))
participant3 = threading.Thread(name='participant3', target=ffn, args = (training_data_participant3, test_data, server))
participant4 = threading.Thread(name='participant4', target=ffn, args = (training_data_participant4, test_data, server))
participant5 = threading.Thread(name='participant5', target=ffn, args = (training_data_participant5, test_data, server))
participant6 = threading.Thread(name='participant6', target=ffn, args = (training_data_participant6, test_data, server))
participant7 = threading.Thread(name='participant7', target=ffn, args = (training_data_participant7, test_data, server))
participant8 = threading.Thread(name='participant8', target=ffn, args = (training_data_participant8, test_data, server))
participant9 = threading.Thread(name='participant9', target=ffn, args = (training_data_participant9, test_data, server))
participant10 = threading.Thread(name='participant10', target=ffn, args = (training_data_participant10, test_data, server))
participant1.start()
participant2.start()
participant3.start()
participant4.start()
participant5.start()
participant6.start()
participant7.start()
participant8.start()
participant9.start()
participant10.start()
def ffn(training_data, test_data, server):
#0.1 is the upload ratio
net = network.Network([784, 30, 10], 0.1, 0.1, server)
net.SGD(training_data, 50, 1, 1, test_data=test_data)
participant_manager()