【问题标题】:Custom keras activation function for different neurons针对不同神经元的自定义 keras 激活函数
【发布时间】:2020-02-15 08:02:22
【问题描述】:

我有一个自定义的 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)

这可能吗?或者还有其他方法可以实现这样的功能?

【问题讨论】:

    标签: keras activation-function


    【解决方案1】:
    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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      • 2017-12-08
      • 2013-08-20
      • 2020-05-29
      • 2018-08-30
      • 2018-05-28
      相关资源
      最近更新 更多