-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn.py
44 lines (39 loc) · 1.34 KB
/
learn.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
import numpy as np
import keras.utils
from sklearn.metrics import accuracy_score,classification_report
from keras.models import Model
import deep,files
def simple_exp(in_path,out_path=None,n_epochs=1000):
seq_dict=files.get_seqs(in_path)
train,test=files.split(seq_dict)
X,y,names,params=prepare_data(train)
model=deep.make_conv(params)
model.fit(X,y,epochs = n_epochs)
if(out_path):
extrac_feats(model,seq_dict,out_path)
else:
test_model(model,test)
def prepare_data(seq_dict):
names=seq_dict.keys()
data=list(seq_dict.values())
X=np.array(data)
X=np.expand_dims(X,axis=-1)
y=[ int(name_i.split("_")[0])-1 for name_i in names]
params={'ts_len':X.shape[1], 'n_feats': X.shape[2],
'n_cats':max(y)+1}
y=keras.utils.to_categorical(y)
return X,y,names,params
def test_model(model,test):
X,y_true,names,params=prepare_data(test)
y_pred=model.predict(X)
y_pred = np.argmax(y_pred, axis=-1)
y_true = np.argmax(y_true, axis=-1)
report=classification_report(y_pred,y_true,digits=4)
print(report)
def extrac_feats(model,seq_dict,out_path):
extractor=Model(inputs=model.input,
outputs=model.get_layer("hidden").output)
X,y,names,params=prepare_data(seq_dict)
X=extractor.predict(X)
files.save_feats(X,names,out_path)
#simple_exp("agum")