Skip to content

Commit

Permalink
added an option to save the best model among the all epochs
Browse files Browse the repository at this point in the history
  • Loading branch information
hezal-s committed Sep 29, 2017
1 parent e5d6fee commit 147d823
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
7 changes: 7 additions & 0 deletions autonomio/_utils/callbacks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from keras.callbacks import EarlyStopping
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import LearningRateScheduler
from keras.callbacks import ModelCheckpoint

from autonomio._utils.get_method import get_method

Expand Down Expand Up @@ -45,4 +46,10 @@ def step_decay(epoch):

l.append(LearningRateScheduler(step_decay))

if para['save_best'] is True:

path = './' + para['save_model'] + '.h5'
l.append(ModelCheckpoint(path, save_best_only=True,
save_weights_only=True))

return l
9 changes: 7 additions & 2 deletions autonomio/commands/_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def train(X=None, Y=None, data=None,
activation_out='sigmoid',
metrics=['accuracy'],
save_model=False,
save_best=False,
neuron_max='auto',
batch_size=10,
verbose=0,
Expand Down Expand Up @@ -134,13 +135,16 @@ def train(X=None, Y=None, data=None,
https://keras.io/metrics/
save_model = An option to save the model configuration, weights
and parameters.
and parameters from last epoch.
OPTIONS: default is 'False', if 'True' model
will be saved with default name ('model')
and if string, then the model name
will be the string value e.g. 'titanic'.
save_best = When True saves the best model. Works only when 'save_model'
is activated.
neuron_max = The maximum number of neurons on any layer.
neuron_last = How many neurons there are in the last layer.
Expand Down Expand Up @@ -286,7 +290,8 @@ def train(X=None, Y=None, data=None,
'lr_scheduler': lr_scheduler,
'initial_lr': initial_lr,
'drop': drop,
'drop_each': drop_each
'drop_each': drop_each,
'save_best': save_best
}

if model is 'lstm':
Expand Down
2 changes: 1 addition & 1 deletion test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
tr = train(1, 'quality_score', temp, flatten=.5, learning_rate=0.1)
tr = train(1, 'quality_score', temp, flatten='mean', metrics='accuracy')

tr = train('text', 'neg', temp, save_model='test_model')
tr = train('text', 'neg', temp, save_model='test_model', save_best=True)
te = predictor(temp, 'test_model')

tr = train(1, 'neg', temp, layers=1, validation=True)
Expand Down

0 comments on commit 147d823

Please sign in to comment.