keras - 针对不同神经元的自定义 keras 激活函数
问题描述
我有一个自定义的 keras 层,我必须创建我的自定义激活函数。是否可以将不同神经元的固定激活放在同一层中?例如,假设我有一个具有 3 个单元的密集层,我希望第一个单元的激活是 relu,第二个是 tanh,第三个是 sigmoid;独立于 x 的值,所以这是不行的:
def myactivation(x):
if x something:
return relu(x)
elif something else :
return another_activation(x)
我想要做的是在特定神经元上应用激活作为
def myactivation(x):
if x == neuron0:
return relu(x)
elif x == neuron1:
return tanh(x)
else:
return sigmoid(x)
这可能吗?或者还有另一种方法来实现这样的事情?
解决方案
import keras.backend as K
def myactivation(x):
#x is the layer's output, shaped as (batch_size, units)
#each element in the last dimension is a neuron
n0 = x[:,0:1]
n1 = x[:,1:2]
n2 = x[:,2:3] #each N is shaped as (batch_size, 1)
#apply the activation to each neuron
x0 = K.relu(n0)
x1 = K.tanh(n1)
x2 = K.sigmoid(n2)
return K.concatenate([x0,x1,x2], axis=-1) #return to the original shape
推荐阅读
- .net-core - 从 HttpContext 会话数据添加属性时,ITelemetryProcessor 不起作用
- html - HTML 中的下拉菜单和 textarea 元素
- python - 如何在python中与条件进行合并
- sql - 分配数量的 Oracle SQL 查询
- php - Docker php:apache HTML/PHP 代码运行不正确。stack main() 在多个区域抛出
- python - 请求在 coinbase api 上传输/发送时出错
- python-3.x - 为什么我的 ttk.Checkbuttons 默认显示被屏蔽?
- arduino - sendPronto 数据类型?
- c - gettid() 为两个不同的线程返回相同的值?
- javascript - 内部/模块/cjs/loader.js:896 ?? 错误:'MODULE_NOT_FOUND',