如何使用 Keras 创建自定义激活函数? [英] How do you create a custom activation function with Keras?

查看:54
本文介绍了如何使用 Keras 创建自定义激活函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时是默认的标准激活,例如 ReLU、tanh、softmax 和 高级激活 像 LeakyReLU 是不够的.它也可能不在 keras-contrib 中.

Sometimes the default standard activations like ReLU, tanh, softmax, ... and the advanced activations like LeakyReLU aren't enough. And it might also not be in keras-contrib.

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

How do you create your own activation function?

推荐答案

感谢 这个 Github 问题评论来自 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())

请记住,您在保存和恢复模型时必须导入此功能.请参阅keras-contrib 的注释.

Please keep in mind that you have to import this function when you save and restore the model. See the note of keras-contrib.

这篇关于如何使用 Keras 创建自定义激活函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆