-
Notifications
You must be signed in to change notification settings - Fork 9
/
deepnet.py
303 lines (266 loc) · 11.1 KB
/
deepnet.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import sys
import os
home = os.path.expanduser("~")
sys.path.append(os.path.join(home, 'gnumpy'))
import gnumpy as gp
import numpy as np
class RBM(object):
'''
This class implements a restricted Bolzmann machine using gnumpy,
which runs on a gpu if cudamat is installed
args:
int n_visible: the number of visible units
int n_hidden: the number of hidden units, default is n_visible
string vistype: type of units for visible layer, default 'sigmoid'
string hidtype: type of units for hidden layer, default 'sigmoid'
array W: the 2d weight matrix, default None
array hbias: the bias weights for the hidden layer, default None
array vbias: the bias weights for the visible layer, default None
int batch_size: default 128
if W, hbias, vbias are left as None (default), they will be created and
initialized automatically.
methods:
train(int num_epochs, array hidden, bool sample)
prop_up(array data)
prop_down(array data)
hidden_state(array data)
variables:
array wu_vh: the weight update array which can be reused
array wu_v: the update array for vbias
array wu_h: the update array for hbias
'''
def __init__(self, n_visible, n_hidden=None, vistype='sigmoid',
hidtype='sigmoid', W=None, hbias=None, vbias=None, batch_size=128):
# initialize parameters
self.SIZE_LIMIT = 80000000 # the size of the largest gpu array
self.vistype = vistype
self.hidtype = hidtype
self.batch_size = batch_size
self.n_visible = n_visible
if n_hidden is None:
n_hidden = self.n_visible
self.n_hidden = n_hidden
n = self.n_visible*self.n_hidden + self.n_hidden
bound = 2.38 / np.sqrt(n)
if W is None:
W = np.zeros((self.n_visible, self.n_hidden))
for i in range(self.n_visible):
for j in range(self.n_hidden):
W[i,j] = np.random.uniform(-bound, bound)
W = gp.garray(W)
self.W = W
if vbias is None:
vbias = gp.zeros(self.n_visible)
else:
vbias = gp.garray(vbias)
self.vbias = vbias
if hbias is None:
hbias = np.zeros((self.n_hidden,))
for i in range(self.n_hidden):
hbias[i] = np.random.uniform(-bound, bound)
hbias = gp.garray(hbias)
self.hbias = hbias
#initialize updates
self.wu_vh = gp.zeros((self.n_visible, self.n_hidden))
self.wu_v = gp.zeros(self.n_visible)
self.wu_h = gp.zeros(self.n_hidden)
def train(self, fulldata, num_epochs, eta=0.01, hidden=None, sample=False,
early_stop=True):
'''
Method to learn the weights of the RBM.
args:
array fulldata: the training data
int num_epochs: the number of times to run through the training data
float eta: the learning rate, default 0.01
array hidden: optional array specifying the hidden representation
to learn (for use in a translational-RBM)
bool sample: specifies whether training should use sampling,
default False
bool early_stop: whether to use early stopping, default True
'''
if hidden is not None:
# check that there is a hidden rep for each data row
assert hidden.shape[0] == data.shape[0]
# check that we have the right number of hidden units
assert hidden.shape[1] == self.n_hidden
# these parameters control momentum changes
initial_momentum = 0.5
final_momentum = 0.9
momentum_iter = 5
# when dealing with large arrays, we have to break the data into
# manageable chunks to avoid out of memory err
if fulldata.size < self.SIZE_LIMIT:
n_chunks = 1
chunk_size = fulldata.shape[0]
else:
n_chunks = int(np.ceil(fulldata.size/float(self.SIZE_LIMIT)))
chunk_size = fulldata.shape[0]/n_chunks
num_batches = chunk_size/self.batch_size
err_hist = [] # keep track of the errors for early stopping
for epoch in range(num_epochs):
if epoch <= momentum_iter:
momentum = initial_momentum
else:
momentum = final_momentum
err = []
print "Training epoch %d of %d," %(epoch+1, num_epochs),
for chunk in range(n_chunks):
num_batches = chunk_size/self.batch_size
data = gp.garray(fulldata[chunk*chunk_size:(chunk+1)*chunk_size])
if hidden is not None:
hid_chunk = gp.garray(hidden[chunk*chunk_size:(chunk+1)*chunk_size])
for batch in range(num_batches):
# positive phase
v1 = data[batch*self.batch_size:(batch+1)*self.batch_size]
if hidden is None:
h1 = self.prop_up(v1)
else:
h1 = hid_chunk[batch*self.batch_size:(batch+1)*self.batch_size]
# negative phase
if sample:
hSampled = h1.rand() < h1
v2 = self.prop_down(hSampled)
else:
v2 = self.prop_down(h1)
h2 = self.prop_up(v2)
# update weights
self.wu_vh = self.wu_vh * momentum + gp.dot(v1.T, h1) - gp.dot(v2.T, h2)
self.wu_v = self.wu_v * momentum + v1.sum(0) - v2.sum(0)
self.wu_h = self.wu_h * momentum + h1.sum(0) - h2.sum(0)
self.W += self.wu_vh * (eta/self.batch_size)
self.vbias += self.wu_v * (eta/self.batch_size)
self.hbias += self.wu_h * (eta/self.batch_size)
# calculate reconstruction error
err.append((v2-v1).euclid_norm()**2/(self.n_visible*self.batch_size))
err_hist.append(np.mean(err))
print "mean squared error: "+ str(np.mean(err))
# early stopping
if early_stop:
recent_err = np.mean(err_hist[epoch-50:epoch])
early_err = np.mean(err_hist[epoch-200:epoch-150])
if (epoch > 250) and ((recent_err * 1.2) > early_err):
break
def prop_up(self, data):
'''
Method to return the hidden representation given data on the visible layer.
args:
array data: the data on the visible layer
returns:
array hid: the probabilisitic activation of the hidden layer
'''
hid = gp.dot(data, self.W) + self.hbias
if self.hidtype == 'sigmoid':
return hid.logistic()
else:
return hid
def prop_down(self, data):
'''
Method to return the visible representation given the hidden
args:
array data: the hidden representation
returns:
array vis: the activation of the visible layer
'''
vis = gp.dot(data, self.W.T) + self.vbias
if self.vistype == 'sigmoid':
return vis.logistic()
else:
return vis
def hidden_state(self, data):
'''
Method to sample from the hidden representation given the visible
args:
array data: the data on the visible layer
returns:
array hSampled: the binary representation of the hidden layer activation
'''
hid = self.prop_up(data)
hSampled = hid.rand() < hid
return hSampled
class Holder(object):
'''
Objects of this class hold values of the RBMs in numpy arrays to free up space
on the GPU
'''
def __init__(self, rbm):
self.W = rbm.W.as_numpy_array()
self.hbias = rbm.hbias.as_numpy_array()
self.vbias = rbm.vbias.as_numpy_array()
self.n_hidden = rbm.n_hidden
self.n_visible = rbm.n_visible
self.hidtype = rbm.hidtype
self.vistype = rbm.vistype
def prop_up(self, data):
hid = np.dot(data, self.W) + self.hbias
if self.hidtype == 'sigmoid':
return 1./(1. + np.exp(-hid))
else:
return hid
class DeepNet(object):
'''
A class to implement a deep neural network
args:
list[int] layer_sizes: defines the number and size of layers
list[str] layer_types: defines layer types, 'sigmoid' or 'gaussian'
methods:
train
run_through_network
'''
def __init__(self, layer_sizes, layer_types):
assert len(layer_sizes) == len(layer_types)
self.layer_sizes = layer_sizes
self.layer_types = layer_types
def train(self, data, epochs, eta):
'''
Trains the deep net one RBM at a time
args:
array data: the training data (a gnumpy.array)
list[int] epochs: the number of training epochs for each RBM
float eta: the learning rate
'''
layers = []
vis = data
for i in range(len(self.layer_sizes)-1):
print "Pretraining RBM %d, vis=%d, hid=%d" % (i+1, self.layer_sizes[i],
self.layer_sizes[i+1])
g_rbm = RBM(self.layer_sizes[i], self.layer_sizes[i+1],
self.layer_types[i], self.layer_types[i+1])
g_rbm.train(vis, epochs[i], eta)
hid = self.get_activation(g_rbm, vis)
vis = hid
n_rbm = Holder(g_rbm)
layers.append(n_rbm)
gp.free_reuse_cache()
self.network = layers
def get_activation(self, rbm, data):
# trying to prop_up the whole data set causes out of memory err
hid = np.zeros((data.shape[0], rbm.n_hidden))
breaks = range(0, hid.shape[0], 128)
breaks.append(hid.shape[0])
for i in range(len(breaks)-1):
hid[breaks[i]:breaks[i+1]] = \
(rbm.prop_up(data[breaks[i]:breaks[i+1]])).as_numpy_array()
return hid
def run_through_network(self, data):
hid = data
for n_rbm in self.network:
vis = gp.garray(hid)
g_rbm = RBM(n_rbm.n_visible, n_rbm.n_hidden, n_rbm.vistype,
n_rbm.hidtype, n_rbm.W, n_rbm.hbias, n_rbm.vbias)
hid = self.get_activation(g_rbm, data)
gp.free_reuse_cache()
return hid
if __name__ == "__main__":
data = np.load('scaled_images.npy')
data = np.asarray(data, dtype='float32')
data /= 255.0
#m = data.mean(0)
#s = data.std(0)
#data = (data - m)/s
#data = gp.garray(data)
t = DeepNet([data.shape[1], data.shape[1], data.shape[1], data.shape[1]*2],
['sigmoid', 'sigmoid', 'sigmoid', 'sigmoid'])
t.train(data, [5, 5, 5], 0.0025)
out = t.run_through_network(data)
print out.shape
np.save('output.npy', out)