【问题标题】:How do you create a custom activation function with Keras?如何使用 Keras 创建自定义激活函数?
【发布时间】:2017-10-10 11:02:46
【问题描述】:

有时默认的standard activations(如 ReLU、tanh、softmax,...)和 advanced activations(如 LeakyReLU)是不够的。它也可能不在keras-contrib

如何创建自己的激活函数?

【问题讨论】:

    标签: python keras keras-layer


    【解决方案1】:

    感谢this Github issue comment by Ritchie Ng

    # Creating a model
    from keras.models import Sequential
    from keras.layers import Dense
    
    # Custom activation function
    from keras.layers import Activation
    from keras import backend as K
    from keras.utils.generic_utils import get_custom_objects
    
    
    def custom_activation(x):
        return (K.sigmoid(x) * 5) - 1
    
    get_custom_objects().update({'custom_activation': Activation(custom_activation)})
    
    # Usage
    model = Sequential()
    model.add(Dense(32, input_dim=784))
    model.add(Activation(custom_activation, name='SpecialActivation'))
    print(model.summary())
    

    请记住,您必须在保存和恢复模型时导入此功能。见the note of keras-contrib

    【讨论】:

    • 也可以作为参数传递给层,如model.add(Dense(10,activation=custom_activation))
    • 如果它是一个自定义激活函数,keras 是否知道如何通过微分反向传播它。 (如 Theano)
    • 是的,Keras 会自动区分
    • 我能否以某种方式为激活函数提供自己的导数,以防需要对数值稳定性进行一些修改?
    【解决方案2】:

    Martin Thoma's answer 稍微简单一点:您可以只创建一个自定义元素后端函数并将其用作参数。您仍然需要在加载模型之前导入此函数。

    from keras import backend as K
    
    def custom_activation(x):
        return (K.sigmoid(x) * 5) - 1
    
    model.add(Dense(32 , activation=custom_activation))
    

    【讨论】:

      【解决方案3】:

      假设您想将swishgelu 添加到keras,以前的方法是很好的内联插入。但是您也可以将它们插入到 keras 激活函数集中,这样您就可以调用自定义函数,就像调用 ReLU 一样。我用 keras 2.2.2 测试了这个(任何 v2 都可以)。将自定义函数的定义附加到此文件$HOME/anaconda2/lib/python2.7/site-packages/keras/activations.py(python 和 anaconda 版本可能不同)。

      在keras内部:

      $HOME/anaconda2/lib/python2.7/site-packages/keras/activations.py
      
      def swish(x):
          return (K.sigmoid(beta * x) * alpha *x)
      

      然后在你的python文件中:

      $HOME/Documents/neural_nets.py
      
      model = Sequential()
      model.add(Activation('swish'))
      

      【讨论】:

        【解决方案4】:

        您可以使用lambda 关键字或Lambda 层。假设你的神经网络没有激活给出了一堆5

        import tensorflow as tf
        import numpy as np
        
        x = np.ones((5, 5))
        
        model = tf.keras.Sequential([
            tf.keras.layers.Dense(1, kernel_initializer=tf.initializers.Ones)
        ])
        
        model.build(input_shape=x.shape)
        
        model(x)
        
        <tf.Tensor: shape=(5, 1), dtype=float32, numpy=
        array([[5.],
               [5.],
               [5.],
               [5.],
               [5.]], dtype=float32)>
        

        而你想让激活函数除以5。你可以添加Lambda层:

        model = tf.keras.Sequential([
            tf.keras.layers.Dense(1, kernel_initializer=tf.initializers.Ones),
            tf.keras.layers.Lambda(lambda x: x/5)
        ])
        
        <tf.Tensor: shape=(5, 1), dtype=float32, numpy=
        array([[1.],
               [1.],
               [1.],
               [1.],
               [1.]], dtype=float32)>
        

        或者在 activation 参数中使用 lambda 关键字:

        model = tf.keras.Sequential([
            tf.keras.layers.Dense(1, 
                                  kernel_initializer=tf.initializers.Ones, 
                                  activation=lambda x: x/5)
        ])
        
        <tf.Tensor: shape=(5, 1), dtype=float32, numpy=
        array([[1.],
               [1.],
               [1.],
               [1.],
               [1.]], dtype=float32)>
        

        【讨论】:

          猜你喜欢
          • 2020-05-29
          • 2019-01-16
          • 1970-01-01
          • 1970-01-01
          • 2019-12-19
          • 2018-08-30
          • 2021-09-02
          • 1970-01-01
          • 2022-01-08
          相关资源
          最近更新 更多