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

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

问题描述

有时默认的标准激活,例如ReLU,tanh,softmax,...和高级激活是不够的.而且可能也不在 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?

推荐答案

Nitch的这个Github问题评论.

# 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天全站免登陆