Skip to content

Commit

Permalink
Fix re: saving/loading models with frozen layers.
Browse files Browse the repository at this point in the history
  • Loading branch information
fchollet committed Dec 22, 2016
1 parent 1bc79f6 commit c4f3155
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
25 changes: 10 additions & 15 deletions keras/engine/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ def __init__(self, **kwargs):

# These properties will be set upon call of self.build(),
# which itself will be called upon self.add_inbound_node if necessary.
if not hasattr(self, 'trainable_weights'):
self.trainable_weights = []
if not hasattr(self, 'non_trainable_weights'):
self.non_trainable_weights = []
if not hasattr(self, '_trainable_weights'):
self._trainable_weights = []
if not hasattr(self, '_non_trainable_weights'):
self._non_trainable_weights = []
if not hasattr(self, 'losses'):
self.losses = []
if not hasattr(self, 'constraints'):
Expand Down Expand Up @@ -418,9 +418,9 @@ def add_weight(self, shape, initializer, name=None,
if constraint is not None:
self.constraints[weight] = constraint
if trainable:
self.trainable_weights.append(weight)
self._trainable_weights.append(weight)
else:
self.non_trainable_weights.append(weight)
self._non_trainable_weights.append(weight)
return weight

def assert_input_compatibility(self, input):
Expand Down Expand Up @@ -1053,16 +1053,11 @@ def __init__(self, input_shape=None, batch_input_shape=None,
self.uses_learning_phase = False
self.trainable = False
self.built = True
self.trainable_weights = []
self.non_trainable_weights = []

self._trainable_weights = []
self._non_trainable_weights = []
self.inbound_nodes = []
self.outbound_nodes = []

self.trainable_weights = []
self.non_trainable_weights = []
self.constraints = {}

self.sparse = sparse

if not name:
Expand Down Expand Up @@ -1269,8 +1264,8 @@ def __init__(self, layers=None, mode='sum', concat_axis=-1,
self.inbound_nodes = []
self.outbound_nodes = []
self.constraints = {}
self.trainable_weights = []
self.non_trainable_weights = []
self._trainable_weights = []
self._non_trainable_weights = []
self.supports_masking = True
self.uses_learning_phase = False
self.input_spec = None # Compatible with anything.
Expand Down
3 changes: 2 additions & 1 deletion keras/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ def deserialize(obj):
return model
training_config = json.loads(training_config.decode('utf-8'))
optimizer_config = training_config['optimizer_config']
optimizer = optimizer_from_config(optimizer_config, custom_objects=custom_objects)
optimizer = optimizer_from_config(optimizer_config,
custom_objects=custom_objects)

# recover loss functions and metrics
loss = deserialize(training_config['loss'])
Expand Down
3 changes: 2 additions & 1 deletion keras/utils/layer_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def layer_from_config(config, custom_objects=None):

arg_spec = inspect.getargspec(layer_class.from_config)
if 'custom_objects' in arg_spec.args:
return layer_class.from_config(config['config'], custom_objects=custom_objects)
return layer_class.from_config(config['config'],
custom_objects=custom_objects)
else:
return layer_class.from_config(config['config'])

Expand Down

0 comments on commit c4f3155

Please sign in to comment.