Skip to content

Commit

Permalink
Save (#967)
Browse files Browse the repository at this point in the history
* save

* save
  • Loading branch information
haifeng-jin committed Feb 14, 2020
1 parent 907b13f commit 6d61003
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 37 deletions.
6 changes: 6 additions & 0 deletions autokeras/engine/tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import tensorflow as tf
from tensorflow.python.util import nest

from autokeras import graph as graph_module
from autokeras import utils


class AutoTuner(kerastuner.engine.multi_execution_tuner.MultiExecutionTuner):
"""A Tuner class based on KerasTuner for AutoKeras.
Expand All @@ -28,6 +31,9 @@ class AutoTuner(kerastuner.engine.multi_execution_tuner.MultiExecutionTuner):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._finished = False
# Save or load the HyperModel.
utils.save_json(os.path.join(self.directory, 'graph'),
graph_module.serialize(self.hypermodel.hypermodel))

# Override the function to prevent building the model during initialization.
def _populate_initial_space(self):
Expand Down
4 changes: 2 additions & 2 deletions autokeras/hypermodels/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def __init__(self,
def get_config(self):
config = super().get_config()
config.update({
'max_len': self.max_len,
'max_tokens': self.vocab_size,
'output_sequence_length': self.output_sequence_length,
'max_tokens': self.max_tokens,
})
return config

Expand Down
24 changes: 13 additions & 11 deletions autokeras/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pickle
import json
import re

import numpy as np
Expand Down Expand Up @@ -99,16 +99,6 @@ def is_label(y):
return len(y.flatten()) == len(y)


def pickle_from_file(path):
"""Load the pickle file from the provided path and returns the object."""
return pickle.load(open(path, 'rb'))


def pickle_to_file(obj, path):
"""Save the pickle file to the specified path."""
pickle.dump(obj, open(path, 'wb'))


def to_snake_case(name):
intermediate = re.sub('(.)([A-Z][a-z0-9]+)', r'\1_\2', name)
insecure = re.sub('([a-z])([A-Z])', r'\1_\2', intermediate).lower()
Expand Down Expand Up @@ -142,3 +132,15 @@ def check_tf_version():
'You can use `pip freeze` to check afterwards that everything is '
'ok.'.format(version=tf.__version__)
)


def save_json(path, obj):
obj = json.dumps(obj)
with tf.io.gfile.GFile(path, 'w') as f:
f.write(obj)


def load_json(path):
with tf.io.gfile.GFile(path, 'r') as f:
obj = f.read()
return json.loads(obj)
35 changes: 11 additions & 24 deletions tests/autokeras/engine/tuner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
import tensorflow as tf

from autokeras.engine import tuner as tuner_module
from autokeras.tuners import greedy
from tests import utils


@mock.patch('kerastuner.engine.base_tuner.BaseTuner.__init__')
@mock.patch('kerastuner.engine.base_tuner.BaseTuner.search')
@mock.patch('tensorflow.keras.Model.fit')
def test_add_early_stopping(fit_fn, base_tuner_search, init, tmp_path):
def test_add_early_stopping(fit_fn, base_tuner_search, tmp_path):
graph = utils.build_graph()
tuner = tuner_module.AutoTuner(oracle=mock.Mock(), hypermodel=graph)
tuner.hypermodel = graph
tuner.oracle = mock.Mock()
tuner.directory = tmp_path
tuner.project_name = ''
tuner = tuner_module.AutoTuner(
oracle=greedy.GreedyOracle(graph, objective='val_loss'),
hypermodel=graph,
directory=tmp_path)
hp = kerastuner.HyperParameters()
trial = mock.Mock()
trial.hyperparameters = hp
Expand All @@ -31,26 +30,14 @@ def test_add_early_stopping(fit_fn, base_tuner_search, init, tmp_path):
for callback in callbacks])


@mock.patch('kerastuner.engine.base_tuner.BaseTuner.__init__')
def test_overwrite_init(base_tuner_init, tmp_path):
tuner_module.AutoTuner(
oracle=mock.Mock(),
hypermodel=lambda hp: None,
directory=tmp_path)

assert not base_tuner_init.call_args_list[0][1]['overwrite']


@mock.patch('kerastuner.engine.base_tuner.BaseTuner.__init__')
@mock.patch('kerastuner.engine.base_tuner.BaseTuner.search')
@mock.patch('tensorflow.keras.Model.fit')
def test_overwrite_search(fit_fn, base_tuner_search, init, tmp_path):
def test_overwrite_search(fit_fn, base_tuner_search, tmp_path):
graph = utils.build_graph()
tuner = tuner_module.AutoTuner(oracle=mock.Mock(), hypermodel=graph)
tuner.hypermodel = graph
tuner.oracle = mock.Mock()
tuner.directory = tmp_path
tuner.project_name = ''
tuner = tuner_module.AutoTuner(
oracle=greedy.GreedyOracle(graph, objective='val_loss'),
hypermodel=graph,
directory=tmp_path)
hp = kerastuner.HyperParameters()
trial = mock.Mock()
trial.hyperparameters = hp
Expand Down

0 comments on commit 6d61003

Please sign in to comment.