使用Keras的'selu'激活功能时出错

AR_*_*AR_ 1 python neural-network keras tensorflow

我正在使用带有Tensorflow后端的Keras.当我尝试使用'selu'激活功能时:

model.add(Dense(32, input_shape=(input_length - 1,)))
model.add(Activation('selu'))
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

ValueError: Unknown activation function:selu
Run Code Online (Sandbox Code Playgroud)

这有什么解决方案吗?

Wil*_*ren 8

Selu不在您activations.py的keras(很可能因为它是在2017年6月14日,仅在22天前添加).您只需在文件中添加缺少的代码,activations.pyselu在脚本中创建自己的激活.

示例代码

from keras.activations import elu

def selu(x):
    """Scaled Exponential Linear Unit. (Klambauer et al., 2017)
    # Arguments
        x: A tensor or variable to compute the activation function for.
    # References
        - [Self-Normalizing Neural Networks](https://arxiv.org/abs/1706.02515)
    """
    alpha = 1.6732632423543772848170429916717
    scale = 1.0507009873554804934193349852946
    return scale * elu(x, alpha)

model.add(Dense(32, input_shape=(input_length - 1,)), activation=selu)
Run Code Online (Sandbox Code Playgroud)