如何使用 Keras 创建自定义激活函数?
How do you create a custom activation function with Keras?
有时默认standard activations like ReLU, tanh, softmax, ... and the advanced activations like LeakyReLU aren't enough. And it might also not be in keras-contrib。
如何创建自己的激活函数?
感谢 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。
假设您想将 swish
or gelu
添加到 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'))
比稍微简单:您只需创建一个自定义的元素后端函数并将其用作参数即可。您仍然需要在加载模型之前导入此函数。
from keras import backend as K
def custom_activation(x):
return (K.sigmoid(x) * 5) - 1
model.add(Dense(32 , activation=custom_activation))
您可以使用 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)>
有时默认standard activations like ReLU, tanh, softmax, ... and the advanced activations like LeakyReLU aren't enough. And it might also not be in keras-contrib。
如何创建自己的激活函数?
感谢 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。
假设您想将 swish
or gelu
添加到 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'))
比
from keras import backend as K
def custom_activation(x):
return (K.sigmoid(x) * 5) - 1
model.add(Dense(32 , activation=custom_activation))
您可以使用 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)>