From b96ecad510e47b4a0a5ad10c857f93a18ece2024 Mon Sep 17 00:00:00 2001 From: Fariz Rahman Date: Wed, 3 Oct 2018 23:56:21 +0530 Subject: [PATCH 1/4] use mode arg --- keras/utils/io_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keras/utils/io_utils.py b/keras/utils/io_utils.py index 8f5f521c6f9..03cc34771e8 100644 --- a/keras/utils/io_utils.py +++ b/keras/utils/io_utils.py @@ -183,7 +183,7 @@ def __init__(self, path, mode='a'): self.data = path self._is_file = False elif isinstance(path, str): - self.data = h5py.File(path,) + self.data = h5py.File(path, mode=mode) self._is_file = True elif isinstance(path, dict): self.data = path From 9f533d8399ef6300065f9afa3f84514531bb36cc Mon Sep 17 00:00:00 2001 From: Fariz Rahman Date: Thu, 4 Oct 2018 00:05:08 +0530 Subject: [PATCH 2/4] add loop test --- tests/test_model_saving.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_model_saving.py b/tests/test_model_saving.py index dbaf71da847..9ff75b2d732 100644 --- a/tests/test_model_saving.py +++ b/tests/test_model_saving.py @@ -632,6 +632,30 @@ def test_saving_recurrent_layer_without_bias(): os.remove(fname) +def test_loop_model_saving(): + model = Sequential() + model.add(Dense(2, input_shape=(3,))) + model.compile(loss=losses.MSE, + optimizer=optimizers.RMSprop(lr=0.0001), + metrics=[metrics.categorical_accuracy], + sample_weight_mode='temporal') + + x = np.random.random((1, 3)) + + _, fname = tempfile.mkstemp('.h5') + + for _ in range(3): + model.train_on_batch(x, y) + save_model(model, fname, overwrite=True) + out = model.predict(x) + + new_model = load_model(fname) + os.remove(fname) + + out2 = new_model.predict(x) + assert_allclose(out, out2, atol=1e-05) + + def test_saving_constant_initializer_with_numpy(): """Test saving and loading model of constant initializer with numpy inputs. """ From 0c5c308031ab13c6488ae3eca41744cd48b45e0e Mon Sep 17 00:00:00 2001 From: Fariz Rahman Date: Thu, 4 Oct 2018 00:32:16 +0530 Subject: [PATCH 3/4] add stricter condition --- keras/utils/io_utils.py | 2 +- tests/test_model_saving.py | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/keras/utils/io_utils.py b/keras/utils/io_utils.py index 03cc34771e8..ef7436cf91a 100644 --- a/keras/utils/io_utils.py +++ b/keras/utils/io_utils.py @@ -209,7 +209,7 @@ def __setitem__(self, attr, val): else: self.data[attr] = val return - if attr in self: + if isinstance(self.data, h5py.Group) and attr in self.data: raise KeyError('Cannot set attribute. ' 'Group with name "{}" exists.'.format(attr)) if is_np: diff --git a/tests/test_model_saving.py b/tests/test_model_saving.py index 9ff75b2d732..6f99c47e7da 100644 --- a/tests/test_model_saving.py +++ b/tests/test_model_saving.py @@ -637,11 +637,10 @@ def test_loop_model_saving(): model.add(Dense(2, input_shape=(3,))) model.compile(loss=losses.MSE, optimizer=optimizers.RMSprop(lr=0.0001), - metrics=[metrics.categorical_accuracy], - sample_weight_mode='temporal') + metrics=[metrics.categorical_accuracy]) x = np.random.random((1, 3)) - + y = np.random.random((1, 2)) _, fname = tempfile.mkstemp('.h5') for _ in range(3): From e42d096909cb9bff5efdfb7e7bb5ac4d8c576b99 Mon Sep 17 00:00:00 2001 From: Fariz Rahman Date: Thu, 4 Oct 2018 00:35:40 +0530 Subject: [PATCH 4/4] make dict behaviour similar --- keras/utils/io_utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/keras/utils/io_utils.py b/keras/utils/io_utils.py index ef7436cf91a..d7b175a8f75 100644 --- a/keras/utils/io_utils.py +++ b/keras/utils/io_utils.py @@ -188,6 +188,8 @@ def __init__(self, path, mode='a'): elif isinstance(path, dict): self.data = path self._is_file = False + if mode == 'w': + self.data.clear() # Flag to check if a dict is user defined data or a sub group: self.data['_is_group'] = True else: