Python tensorflow.python.keras.layers.Layer() Examples

The following are 4 code examples of tensorflow.python.keras.layers.Layer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow.python.keras.layers , or try the search function .
Example #1
Source Project: FATE   Author: FederatedAI   File: backend.py    License: Apache License 2.0 6 votes vote down vote up
def restore_model(cls, model_bytes):  # todo: restore optimizer to support incremental learning
        with tempfile.TemporaryDirectory() as tmp_path:
            with io.BytesIO(model_bytes) as bytes_io:
                with zipfile.ZipFile(bytes_io, 'r', zipfile.ZIP_DEFLATED) as f:
                    f.extractall(tmp_path)

            # Comment this block because tf 1.15 is not supporting Keras Customized Layer
            # try:
            #     keras_model = tf.keras.models.load_model(filepath=tmp_path,
            #                                              custom_objects={'ConstantLayer': ConstantLayer})
            # except IOError:
            #     import warnings
            #     warnings.warn('loading the model as SavedModel is still in experimental stages. '
            #                   'trying tf.keras.experimental.load_from_saved_model...')
            keras_model = \
                    tf.keras.experimental.load_from_saved_model(saved_model_path=tmp_path,
                                                                custom_objects={'ConstantLayer': ConstantLayer})
        model = cls()
        model._set_model(keras_model)
        return model 
Example #2
Source Project: icme2019   Author: ShenDezhou   File: activation.py    License: MIT License 5 votes vote down vote up
def activation_fun(activation, fc):

    if isinstance(activation, str):
        fc = tf.keras.layers.Activation(activation)(fc)
    elif issubclass(activation, Layer):
        fc = activation()(fc)
    else:
        raise ValueError(
            "Invalid activation,found %s.You should use a str or a Activation Layer Class." % (activation))
    return fc 
Example #3
Source Project: FATE   Author: FederatedAI   File: backend.py    License: Apache License 2.0 5 votes vote down vote up
def export_model(self):
        with tempfile.TemporaryDirectory() as tmp_path:
            # Comment this block because tf 1.15 is not supporting Keras Customized Layer
            # try:
            #     # LOGGER.info("Model saved with model.save method.")
            #     tf.keras.models.save_model(self._model, filepath=tmp_path, save_format="tf")
            # except NotImplementedError:
            #     import warnings
            #     warnings.warn('Saving the model as SavedModel is still in experimental stages. '
            #                   'trying tf.keras.experimental.export_saved_model...')
            tf.keras.experimental.export_saved_model(self._model, saved_model_path=tmp_path)

            model_bytes = zip_dir_as_bytes(tmp_path)

        return model_bytes 
Example #4
Source Project: DeepCTR   Author: shenweichen   File: activation.py    License: Apache License 2.0 5 votes vote down vote up
def activation_layer(activation):
    if activation == "dice" or activation == "Dice":
        act_layer = Dice()
    elif (isinstance(activation, str)) or (sys.version_info.major == 2 and isinstance(activation, (str, unicode))):
        act_layer = tf.keras.layers.Activation(activation)
    elif issubclass(activation, Layer):
        act_layer = activation()
    else:
        raise ValueError(
            "Invalid activation,found %s.You should use a str or a Activation Layer Class." % (activation))
    return act_layer