Update save_model function
with statement instead of manually opening and closing file
@@ -98,7 +98,7 @@ def save_model(model, filepath, overwrite=True, include_optimizer=True):
if not proceed:
return
- f = h5py.File(filepath, 'w')
+ with h5py.File(filepath, mode='w') as f:
f.attrs['keras_version'] = str(keras_version).encode('utf8')
f.attrs['backend'] = K.backend().encode('utf8')
f.attrs['model_config'] = json.dumps({
@@ -122,7 +122,8 @@ def save_model(model, filepath, overwrite=True, include_optimizer=True):
'after instantiation. '
'As a result, we cannot save the optimizer '
'as part of the model save file.'
- 'You will have to compile your model again after loading it. '
+ 'You will have to compile your model again '
+ 'after loading it. '
'Prefer using a Keras optimizer instead '
'(see keras.io/optimizers).')
else:
@@ -143,8 +144,10 @@ def save_model(model, filepath, overwrite=True, include_optimizer=True):
optimizer_weights_group = f.create_group('optimizer_weights')
weight_values = K.batch_get_value(symbolic_weights)
weight_names = []
- for i, (w, val) in enumerate(zip(symbolic_weights, weight_values)):
- # Default values of symbolic_weights is /variable for theano and cntk
+ for i, (w, val) in enumerate(zip(symbolic_weights,
+ weight_values)):
+ # Default values of symbolic_weights is /variable
+ # for theano and cntk
if K.backend() == 'theano' or K.backend() == 'cntk':
if hasattr(w, 'name') and w.name != "/variable":
name = str(w.name)
@@ -168,7 +171,6 @@ def save_model(model, filepath, overwrite=True, include_optimizer=True):
else:
param_dset[:] = val
f.flush()
- f.close()
def load_model(filepath, custom_objects=None, compile=True):
