-
Notifications
You must be signed in to change notification settings - Fork 1
/
neuralnet.py
59 lines (51 loc) · 2.48 KB
/
neuralnet.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
# Create first network with Keras
from keras.models import Sequential,load_model
from keras.layers import Dense, Dropout, Activation, LSTM
from keras.utils import np_utils
from keras.wrappers.scikit_learn import KerasRegressor
from keras.callbacks import ModelCheckpoint
#sciKit for pipeline and CV
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
class network(object):
def __init__(self, X):
self.model = self.getModel(X)
def getModel(self,X):
# create model
model = Sequential()
model.add(LSTM(400, input_shape=(X.shape[1],X.shape[2]), return_sequences=True))
model.add(LSTM(200, input_shape=(X.shape[1],X.shape[2])))
model.add(Dense(128,activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
return model
def train(self,x,y,epochs=20,batch_size=5,filepath="recent_lstm_model_weights.h5"):
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
# fit the model
self.model.fit(x, y, epochs=epochs, batch_size=batch_size, callbacks=callbacks_list)
self.model = self.load_best_model(filepath)
return self.model
def load_best_model(self, filepath="recent_lstm_model_weights.h5"):
return load_model(filepath)
class timing_network(object):
def __init__(self, X):
self.model = self.getModel(X)
def getModel(self,X):
# create model
model = Sequential()
model.add(LSTM(400, input_shape=(X.shape[1],X.shape[2]), return_sequences=True))
model.add(LSTM(200, input_shape=(X.shape[1],X.shape[2])))
model.add(Dense(16,activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
return model
def train(self,x,y,epochs=20,batch_size=5,filepath="recent_lstm_timing_model_weights.h5"):
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
# fit the model
self.model.fit(x, y, epochs=epochs, batch_size=batch_size, callbacks=callbacks_list)
self.model = self.load_best_model(filepath)
return self.model
def load_best_model(self, filepath="recent_lstm_timing_model_weights.h5"):
return load_model(filepath)