Python tensorflow.keras.activations() Examples

The following are 5 code examples of tensorflow.keras.activations(). 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.keras , or try the search function .
Example #1
Source File: layers.py    From astroNN with MIT License 6 votes vote down vote up
def __init__(self,
                 deg=1,
                 output_units=1,
                 use_xbias=True,
                 init_w=None,
                 name=None,
                 activation=None,
                 kernel_regularizer=None,
                 kernel_constraint=None):
        super().__init__(name=name)
        self.input_spec = InputSpec(min_ndim=2)
        self.deg = deg
        self.output_units = output_units
        self.use_bias = use_xbias
        self.activation = activations.get(activation)
        self.kernel_regularizer = tfk.regularizers.get(kernel_regularizer)
        self.kernel_constraint = tfk.constraints.get(kernel_constraint)
        self.init_w = init_w

        if self.init_w is not None and len(self.init_w) != self.deg + 1:
            raise ValueError(f"If you specify initial weight for {self.deg}-deg polynomial, "
                             f"you must provide {self.deg + 1} weights") 
Example #2
Source File: layers.py    From astroNN with MIT License 5 votes vote down vote up
def get_config(self):
        """
        :return: Dictionary of configuration
        :rtype: dict
        """
        config = {'degree': self.deg,
                  'use_bias': self.use_bias,
                  'activation': activations.serialize(self.activation),
                  'initial_weights': self.init_w,
                  'kernel_regularizer': tfk.regularizers.serialize(self.kernel_regularizer),
                  'kernel_constraint': tfk.constraints.serialize(self.kernel_constraint)}
        base_config = super().get_config()
        return {**dict(base_config.items()), **config} 
Example #3
Source File: unet.py    From ashpy with Apache License 2.0 5 votes vote down vote up
def __init__(
        self,
        input_res,
        min_res,
        kernel_size,
        initial_filters,
        filters_cap,
        channels,  # number of classes
        use_dropout_encoder=True,
        use_dropout_decoder=True,
        dropout_prob=0.3,
        encoder_non_linearity=keras.layers.LeakyReLU,
        decoder_non_linearity=keras.layers.ReLU,
        use_attention=False,
    ):
        """Build the Semantic UNet model."""
        super().__init__(
            input_res,
            min_res,
            kernel_size,
            initial_filters,
            filters_cap,
            channels,
            use_dropout_encoder,
            use_dropout_decoder,
            dropout_prob,
            encoder_non_linearity,
            decoder_non_linearity,
            last_activation=keras.activations.softmax,
            use_attention=use_attention,
        ) 
Example #4
Source File: utils.py    From MultiPlanarUNet with MIT License 5 votes vote down vote up
def init_activation(activation_string, logger=None, **kwargs):
    """
    Same as 'init_losses', but for optimizers.
    Please refer to the 'init_losses' docstring.
    """
    activation = _init(
        activation_string,
        tf_funcs=[activations, addon_activations],
        custom_funcs=None,
        logger=logger
    )[0]
    return activation 
Example #5
Source File: tf_objects_factory.py    From nucleus7 with Mozilla Public License 2.0 4 votes vote down vote up
def activation_factory(activation_or_name_with_params: Union[dict, str, type]
                       ) -> Union[Callable[[tf.Tensor], tf.Tensor], partial]:
    """
    Factory to get the activation function

    Parameters
    ----------
    activation_or_name_with_params
        either activation fn itself, then will be returned as is, or only the
        name of activation, which will be get from tf.nn or from
        keras.activations modules or a dict with name and kwargs to pass
        to the activation_fn

    Returns
    -------
    activation_fn
        activation function

    """
    if callable(activation_or_name_with_params):
        return activation_or_name_with_params

    if activation_or_name_with_params is None:
        return tf.identity

    name_with_params = activation_or_name_with_params
    name, params = _get_name_and_params(name_with_params)

    if _check_should_import_function(name):
        activation_function = _import_function(name, params)
        _check_signature(activation_function, [], 1)
        return activation_function

    assert hasattr(tf.nn, name) or hasattr(keras.activations, name), (
        "Use activation name from tf.nn or keras.activations "
        "(got {})".format(name))

    if hasattr(tf.nn, name):
        activation = getattr(tf.nn, name)
    else:
        activation = getattr(keras.activations, name)

    if not params:
        return activation
    return partial(activation, **params)