예제 #1
0
    def _python_properties_internal(self):
        """Returns dictionary of all python properties."""
        # TODO(kathywu): Add support for metrics serialization.
        # TODO(kathywu): Synchronize with the keras spec (go/keras-json-spec) once
        # the python config serialization has caught up.
        metadata = dict(
            class_name=type(self.obj).__name__,
            name=self.obj.name,
            trainable=self.obj.trainable,
            expects_training_arg=self.obj._expects_training_arg,  # pylint: disable=protected-access
            dtype=policy.serialize(self.obj._dtype_policy),  # pylint: disable=protected-access
            batch_input_shape=getattr(self.obj, '_batch_input_shape', None))

        with generic_utils.skip_failed_serialization():
            # Store the config dictionary, which may be used when reviving the object.
            # When loading, the program will attempt to revive the object from config,
            # and if that fails, the object will be revived from the SavedModel.
            config = generic_utils.serialize_keras_object(self.obj)['config']
            if config is not None:
                metadata['config'] = config
        if self.obj.input_spec is not None:
            # Layer's input_spec has already been type-checked in the property setter.
            metadata['input_spec'] = nest.map_structure(
                lambda x: generic_utils.serialize_keras_object(x)
                if x else None, self.obj.input_spec)
        if (self.obj.activity_regularizer is not None
                and hasattr(self.obj.activity_regularizer, 'get_config')):
            metadata[
                'activity_regularizer'] = generic_utils.serialize_keras_object(
                    self.obj.activity_regularizer)
        return metadata
예제 #2
0
def get_config(obj):
    with generic_utils.skip_failed_serialization():
        # Store the config dictionary, which may be used when reviving the object.
        # When loading, the program will attempt to revive the object from config,
        # and if that fails, the object will be revived from the SavedModel.
        config = generic_utils.serialize_keras_object(obj)['config']

    if config is not None:
        return {'config': config}
    return {}