Skip to content

Commit

Permalink
Added new LSTM architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
Zvezdin committed Feb 25, 2018
1 parent 7b1734e commit e6c37a9
Showing 1 changed file with 40 additions and 18 deletions.
58 changes: 40 additions & 18 deletions neural/basic_lstm_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,38 @@ def build(self, inputShape, numTargets, args = {}):
for arr in [args['LSTM'], args['dense']]:
while 0 in arr: arr.remove(0)

self.model = Sequential()

for i, layer in enumerate(args['LSTM']):
ret_seq=(i< (len(args['LSTM'])-1))
name = str(layer)+ '_' + str(i+1) + ('_ret_seq' if ret_seq else '')
if i==0:
self.model.add(LSTM(layer, return_sequences=ret_seq, stateful=args['stateful'], batch_input_shape=(args['batch'], time_steps, features), name=name ) )
self.model.add(Dropout(0.1, name='0.1'))
else:
self.model.add(LSTM(layer, return_sequences=ret_seq, stateful=args['stateful'], name=name) )
#model.add(Activation('relu'))
print("Adding LSTM Layer of size %d." % layer)

for dense in args['dense']:
self.model.add(Dense(dense))
#model.add(Activation('relu'))
oldModel = True

if oldModel:
self.model = Sequential()
for i, layer in enumerate(args['LSTM']):
ret_seq=(i< (len(args['LSTM'])-1))
name = str(layer)+ '_' + str(i+1) + ('_ret_seq' if ret_seq else '')
if i==0:
self.model.add(LSTM(layer, return_sequences=ret_seq, stateful=args['stateful'], batch_input_shape=(args['batch'], time_steps, features), name=name ) )
self.model.add(Dropout(0.1, name='0.1'))
else:
self.model.add(LSTM(layer, return_sequences=ret_seq, stateful=args['stateful'], name=name) )
#model.add(Activation('relu'))
print("Adding LSTM Layer of size %d." % layer)
for dense in args['dense']:
self.model.add(Dense(dense))
#model.add(Activation('relu'))
else:
dropout_value = 0.2

self.model = Sequential()

#First recurrent layer with dropout
self.model.add(Bidirectional(LSTM(time_steps, return_sequences=True), input_shape=(time_steps, features),))
self.model.add(Dropout(dropout_value))

#Second recurrent layer with dropout
self.model.add(Bidirectional(LSTM((time_steps*2), return_sequences=True)))
self.model.add(Dropout(dropout_value))

#Third recurrent layer
self.model.add(Bidirectional(LSTM(time_steps, return_sequences=False)))

self.model.add(Dense(numTargets, name=str(numTargets)))
self.model.add(Activation('linear', name='linear'))
Expand Down Expand Up @@ -146,8 +162,14 @@ def train(self, givenDataset, givenLabels, args = {}, targetNormalization = None
def predict(self, dataset):
dataset, _ = self.reformat(dataset, None, cut_timesteps=self.cut_timesteps)

return self.model.predict(dataset, batch_size=self.batch_size)
return self.model.predict(dataset, batch_size=self.getBatchSize())

def evaluate(self, dataset, labels):
_, labels = self.reformat(dataset, labels, cut_timesteps=self.cut_timesteps) #don't reformat the dataset, predict() will reformat it
return self.scorePrediction(self.predict(dataset), labels)
return self.scorePrediction(self.predict(dataset), labels)

def getBatchSize(self):
if self.batch_size is None:
self.batch_size = self.model.input_shape[0]

return self.batch_size

0 comments on commit e6c37a9

Please sign in to comment.