-
Notifications
You must be signed in to change notification settings - Fork 5
/
posing.py
442 lines (358 loc) · 15 KB
/
posing.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# Demonstration of how to pose the problem and how different formulations
# lead to different results!
#
# The MIT License (MIT)
#
# Copyright (c) 2016 Abram Hindle <[email protected]>, Leif Johnson <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# first off we load up some modules we want to use
import keras
import scipy
import math
import numpy as np
import numpy.random as rnd
import logging
import sys
from numpy.random import power, normal, lognormal, uniform
from keras.models import Sequential
from keras.layers.core import Dense
from keras.optimizers import SGD
from keras.optimizers import Adam
from sklearn.preprocessing import OneHotEncoder
import theautil
import collections
# What are we going to do?
# - we're going to generate data derived from 4 different distributions
# - we're going to scale that data
# - we're going to create a RBM (1 hidden layer neural network)
# - we're going to train it to classify data as belonging to one of these distributions
# maximum number of iterations before we bail
mupdates = 1000
# setup logging
logging.basicConfig(stream = sys.stderr, level=logging.INFO)
# how we pose our problem to the deep belief network matters.
# lets make the task easier by scaling all values between 0 and 1
def min_max_scale(data):
'''scales data by minimum and maximum values between 0 and 1'''
dmin = np.min(data)
return (data - dmin)/(np.max(data) - dmin)
# how many samples per each distribution
bsize = 100
# poor man's enum
LOGNORMAL=0
POWER=1
NORM=2
UNIFORM=3
print('''
########################################################################
# Experiment 1: can we classify single samples?
#
#
#########################################################################
''')
def make_dataset1():
'''Make a dataset of single samples with labels from which distribution they come from'''
# now lets make some samples
lns = min_max_scale(lognormal(size=bsize)) #log normal
powers = min_max_scale(power(0.1,size=bsize)) #power law
norms = min_max_scale(normal(size=bsize)) #normal
uniforms = min_max_scale(uniform(size=bsize)) #uniform
# add our data together
data = np.concatenate((lns,powers,norms,uniforms))
# concatenate our labels
labels = np.concatenate((
(np.repeat(LOGNORMAL,bsize)),
(np.repeat(POWER,bsize)),
(np.repeat(NORM,bsize)),
(np.repeat(UNIFORM,bsize))))
tsize = len(labels)
# make sure dimensionality and types are right
data = data.reshape((len(data),1))
data = data.astype(np.float32)
labels = labels.astype(np.int32)
labels = labels.reshape((len(data),))
return data, labels, tsize
# this will be the training data and validation data
data, labels, tsize = make_dataset1()
# this is the test data, this is kept separate to prove we can
# actually work on the data we claim we can.
#
# Without test data, you might just have great performance on the
# train set.
test_data, test_labels, _ = make_dataset1()
# now lets shuffle
# If we're going to select a validation set we probably want to shuffle
def joint_shuffle(arr1,arr2):
assert len(arr1) == len(arr2)
indices = np.arange(len(arr1))
np.random.shuffle(indices)
arr1[0:len(arr1)] = arr1[indices]
arr2[0:len(arr2)] = arr2[indices]
# our data and labels are shuffled together
joint_shuffle(data,labels)
def split_validation(percent, data, labels):
'''
split_validation splits a dataset of data and labels into
2 partitions at the percent mark
percent should be an int between 1 and 99
'''
s = int(percent * len(data) / 100)
tdata = data[0:s]
vdata = data[s:]
tlabels = labels[0:s]
vlabels = labels[s:]
return ((tdata,tlabels),(vdata,vlabels))
# make a validation set from the train set
train1, valid1 = split_validation(90, data, labels)
print(train1[0].shape)
print(train1[1].shape)
enc1 = OneHotEncoder(handle_unknown='ignore',sparse=False)
enc1.fit(train1[1].reshape(len(train1[1]),1))
train1_y = enc1.transform(train1[1].reshape(len(train1[1]),1))
print(train1_y.shape)
valid1_y = enc1.transform(valid1[1].reshape(len(valid1[1]),1))
print(valid1_y.shape)
test1_y = enc1.transform(test_labels.reshape(len(test_labels),1))
print(test1_y.shape)
# build our classifier
print("We're building a MLP of 1 input layer node, 4 hidden layer nodes, and an output layer of 4 nodes. The output layer has 4 nodes because we have 4 classes that the neural network will output.")
cnet = Sequential()
cnet.add(Dense(4,input_shape=(1,),activation="sigmoid"))
cnet.add(Dense(4,activation="softmax"))
copt = SGD(lr=0.1)
# opt = Adam(lr=0.1)
cnet.compile(loss="categorical_crossentropy", optimizer=copt, metrics=["accuracy"])
history = cnet.fit(train1[0], train1_y, validation_data=(valid1[0], valid1_y),
epochs=100, batch_size=16)
#score = cnet.evaluate(test_data, test_labels)
#print("Scores: %s" % score)
classify = cnet.predict_classes(test_data)
print(theautil.classifications(classify,test_labels))
score = cnet.evaluate(test_data, test1_y)
print("Scores: %s" % score)
# now that's kind of interesting, an accuracy of .3 to .5 max
# still pretty inaccurate, but 1 sample might never be enough.
print("We could train longer and we might get better results, but there's ambiguity in each. As a human we might have a hard time determining them.")
print('''
########################################################################
# Experiment 2: can we classify a sample of data?
#
#
#########################################################################
''')
print("In this example we're going to input 40 values from a single distribution, and we'll see if we can classify the distribution.")
width=40
def make_widedataset(width=width):
# we're going to make rows of 40 features unsorted
wlns = min_max_scale(lognormal(size=(bsize,width))) #log normal
wpowers = min_max_scale(power(0.1,size=(bsize,width))) #power law
wnorms = min_max_scale(normal(size=(bsize,width))) #normal
wuniforms = min_max_scale(uniform(size=(bsize,width))) #uniform
wdata = np.concatenate((wlns,wpowers,wnorms,wuniforms))
# concatenate our labels
wlabels = np.concatenate((
(np.repeat(LOGNORMAL,bsize)),
(np.repeat(POWER,bsize)),
(np.repeat(NORM,bsize)),
(np.repeat(UNIFORM,bsize))))
joint_shuffle(wdata,wlabels)
wdata = wdata.astype(np.float32)
wlabels = wlabels.astype(np.int32)
wlabels = wlabels.reshape((len(data),))
return wdata, wlabels
# make our train sets
wdata, wlabels = make_widedataset()
# make our test sets
test_wdata, test_wlabels = make_widedataset()
# split out our validation set
wtrain, wvalid = split_validation(90, wdata, wlabels)
print("At this point we have a weird decision to make, how many neurons in the hidden layer?")
encwc = OneHotEncoder(handle_unknown='ignore',sparse=True)
encwc.fit(wtrain[1].reshape(len(wtrain[1]),1))
wtrain_y = encwc.transform(wtrain[1].reshape(len(wtrain[1]),1))
wvalid_y = encwc.transform(wvalid[1].reshape(len(wvalid[1]),1))
wtest_y = encwc.transform(test_wlabels.reshape(len(test_wlabels),1))
# wcnet = theanets.Classifier([width,width/4,4]) #267
wcnet = Sequential()
wcnet.add(Dense(width,input_shape=(width,),activation="sigmoid"))
wcnet.add(Dense(int(width/4),activation="sigmoid"))
wcnet.add(Dense(4,activation="softmax"))
wcnet.compile(loss="categorical_crossentropy", optimizer=SGD(lr=0.1), metrics=["accuracy"])
history = wcnet.fit(wtrain[0], wtrain_y, validation_data=(wvalid[0], wvalid_y),
epochs=100, batch_size=16)
classify = wcnet.predict_classes(test_wdata)
print(theautil.classifications(classify,test_wlabels))
score = wcnet.evaluate(test_wdata, wtest_y)
print("Scores: %s" % score)
# # You could try some of these alternative setups
#
# [width,4]) #248
# [width,width/2,4]) #271
# [width,width,4]) #289
# [width,width*2,4]) #292
# [width,width/2,width/4,4]) #270
# [width,width/2,width/4,width/8,width/16,4]) #232
# [width,width*8,4]) #304
print("Ok that was neat, it definitely worked better, it had more data though.")
print("But what if we help it out, and we sort the values so that the first and last bins are always the min and max values?")
# now lets input sorted values
print('''
########################################################################
# Experiment 3: can we classify a SORTED sample of data?
#
#
#########################################################################
''')
print("Sorting the data")
wdata.sort(axis=1)
test_wdata.sort(axis=1)
swcnet = Sequential()
swcnet.add(Dense(width,input_shape=(width,),activation="sigmoid"))
swcnet.add(Dense(int(width/4),activation="sigmoid"))
swcnet.add(Dense(4,activation="softmax"))
swcnet.compile(loss="categorical_crossentropy", optimizer=SGD(lr=0.1), metrics=["accuracy"])
history = swcnet.fit(wtrain[0], wtrain_y, validation_data=(wvalid[0], wvalid_y),
epochs=100, batch_size=16)
classify = swcnet.predict_classes(test_wdata)
print(theautil.classifications(classify,test_wlabels))
score = swcnet.evaluate(test_wdata, wtest_y)
print("Scores: %s" % score)
#
# swcnet = theanets.Classifier([width,width/2,4])
# res = swcnet.train(wtrain,wvalid, algo='layerwise', patience=1, max_updates=mupdates)
# print(res)
# res = swcnet.train(wtrain,wvalid, algo='rprop', patience=1, max_updates=mupdates)
# print(res)
# print("%s / %s " % (sum(swcnet.classify(wdata) == wlabels),tsize))
# print("%s / %s " % (sum(swcnet.classify(test_wdata) == test_wlabels),tsize))
# print(collections.Counter(swcnet.classify(test_wdata)))
#
print("That was an improvement!")
print("What if we add binning, where by we classify the histogram?")
# a little better
print('''
########################################################################
# Experiment 4: can we classify a discretized histogram of sample data?
#
#
#########################################################################
'''
)
# let's try actual binning
def bin(row):
return np.histogram(row,bins=len(row),range=(0.0,1.0))[0]/float(len(row))
print("Apply the histogram to all the data rows")
bdata = np.apply_along_axis(bin,1,wdata).astype(np.float32)
blabels = wlabels
# ensure we have our test data
test_bdata = np.apply_along_axis(bin,1,test_wdata).astype(np.float32)
test_blabels = test_wlabels
# helper data
enum_funcs = [
(LOGNORMAL,"log normal",lambda size: lognormal(size=size)),
(POWER,"power",lambda size: power(0.1,size=size)),
(NORM,"normal",lambda size: normal(size=size)),
(UNIFORM,"uniforms",lambda size: uniform(size=size)),
]
# uses enum_funcs to evaluate PER CLASS how well our classify operates
def classify_test(bnet,ntests=1000):
for tup in enum_funcs:
enum, name, func = tup
lns = min_max_scale(func(size=(ntests,width))) #log normal
blns = np.apply_along_axis(bin,1,lns).astype(np.float32)
blns_labels = np.repeat(enum,ntests)
blns_labels.astype(np.int32)
classification = bnet.predict_classes(blns)
classified = theautil.classifications(classification,blns_labels)
print("Name:%s Tests:[%s] Count:%s -- Res:%s" % (name,ntests, collections.Counter(classification),classified ))
# train & valid
btrain, bvalid = split_validation(90, bdata, blabels)
encb = OneHotEncoder(handle_unknown='ignore',space=False)
encb.fit(btrain[1].reshape(len(btrain[1]),1))
btrain_y = encb.transform(btrain[1].reshape(len(btrain[1]),1))
bvalid_y = encb.transform(bvalid[1].reshape(len(bvalid[1]),1))
btest_y = encb.transform(test_blabels.reshape(len(test_blabels),1))
# similar network structure
# bnet = theanets.Classifier([width,width/2,4])
bnet = Sequential()
bnet.add(Dense(width,input_shape=(width,),activation="sigmoid"))
bnet.add(Dense(int(width/4),activation="sigmoid"))
bnet.add(Dense(4,activation="softmax"))
bnet.compile(loss="categorical_crossentropy", optimizer=SGD(lr=0.1), metrics=["accuracy"])
history = bnet.fit(btrain[0], btrain_y, validation_data=(bvalid[0], bvalid_y),
epochs=100, batch_size=16)
classify = bnet.predict_classes(test_bdata)
print(theautil.classifications(classify,test_blabels))
score = bnet.evaluate(test_bdata, btest_y)
print("Scores: %s" % score)
classify_test(bnet)
# sometimes lognormal doesn't show up so well -- it can look like a powerlaw
# so after binning I have to say it is far more robust than before
# let's tune
print('''
########################################################################
# Experiment 5: Can we tune the binned data?
#
#
#########################################################################
'''
)
import Search
# 1 repetition
state = {"reps":1}
params = {"batch_size":[1,4,8,16,32,64],
"lr":[1.0,0.1,0.01,0.001,0.0001],
"activation":["sigmoid","tanh","relu"],
"optimizer":["SGD","Adam"],
"epochs":[25],
"arch":[
[width],
[width,width],
[width,int(width/4)],
[2*width,width],
[int(width/4),int(width/8)],
[int(width/4)]]
}
def get_optimizer(x):
if x == "Adam":
return Adam
return SGD
def f(state,params):
bnet = Sequential()
arch = params["arch"]
bnet.add(Dense(arch[0],input_shape=(width,),activation=params["activation"]))
for layer in arch[1:]:
bnet.add(Dense(int(layer),activation=params["activation"]))
bnet.add(Dense(4,activation="softmax"))
optimizer = get_optimizer(params["optimizer"])
bnet.compile(loss="categorical_crossentropy",
optimizer=optimizer(lr=params["lr"]), metrics=["accuracy"])
history = bnet.fit(btrain[0], btrain_y,
validation_data=(bvalid[0], bvalid_y),
epochs=params["epochs"], batch_size=params["batch_size"])
classify = bnet.predict_classes(test_bdata)
print(theautil.classifications(classify,test_blabels))
score = bnet.evaluate(test_bdata, btest_y)
print("Scores: %s" % score)
return score[1]
state["f"] = f
random_results = Search.random_search(state,params,Search.heuristic_function,time=60)
random_results = sorted(random_results, key=lambda x: x['Score'])
print(random_results[-1])