不同神经元的自定义keras激活函数
我有一个自定义的keras层,我必须创建我的自定义激活功能。是否有可能在同一层对不同的神经元进行固定的激活? 例如,假设我有一个有3个单位的致密层,我希望第一个单位的激活是relu,第二个单位是tanh,第三个单位是s形;独立于x的值,因此这不正常:不同神经元的自定义keras激活函数,keras,activation-function,Keras,Activation Function,我有一个自定义的keras层,我必须创建我的自定义激活功能。是否有可能在同一层对不同的神经元进行固定的激活? 例如,假设我有一个有3个单位的致密层,我希望第一个单位的激活是relu,第二个单位是tanh,第三个单位是s形;独立于x的值,因此这不正常: def myactivation(x): if x something: return relu(x) elif something else : return another_activation(
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