-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMONK.py
More file actions
57 lines (51 loc) · 1.57 KB
/
MONK.py
File metadata and controls
57 lines (51 loc) · 1.57 KB
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
from __future__ import print_function
import numpy as np
import theano
import theano.tensor as T
from ANNLIB.model import Model
from sklearn import preprocessing
import itertools
from sklearn.model_selection import KFold, cross_val_score
def data(filename):
with open(filename) as f:
data = f.readlines()
data=[e[:-1] for e in [e.split(' ') for e in map(lambda x: x[:-1],data)]]
translatio_2={'1':[1,0],'2':[0,1]}
translatio_3={'1':[1,0,0],'2':[0,1,0],'3':[0,0,1]}
translatio_4={'1':[1,0,0,0],'2':[0,1,0,0],'3':[0,0,1,0],'4':[0,0,0,1]}
encoding={'0':translatio_3,'1':translatio_3,'2':translatio_2,'3':translatio_3,'4':translatio_4,'5':translatio_2}
X=[ e[1:] for e in data]
newX=[]
for val in X:
temp=[]
for i in range(6):
temp.append(encoding[str(i)][str(val[i])])
temp=list(itertools.chain.from_iterable(temp))
newX.append(temp)
X=[ np.array(e, dtype=theano.config.floatX) for e in newX]
y=[e[0] for e in data]
X = np.array(X, dtype=theano.config.floatX)
y = [np.array(y, dtype=theano.config.floatX)]
#rng_state = np.random.get_state()
#np.random.shuffle(X)
#np.random.set_state(rng_state)
#np.random.shuffle(y)
X=X.transpose()
print(len(X[0]))
y=np.array(y)
return X,y
if __name__=='__main__':
X_train,y_train=data('dataset/monks-1.train')
X_test,y_test=data('dataset/monks-1.test')
assert X_train.shape[0]== X_test.shape[0]
m=Model(X_train,y_train,X_test,y_test,X_test,y_test)
m.ANNModel(hidden_unit=3,
outputsize=1,
learning_rate = 0.5,
momentum = 0.8,
lamb=0.0,
activations="sigmoid",
loss="MSE")
m.train(500)
#m.test()
m.plotLA()