gpt4 book ai didi

keras - 在keras中实现复杂的激活函数

转载 作者:行者123 更新时间:2023-12-02 06:33:40 25 4
gpt4 key购买 nike

我刚刚读了一篇有趣的论文:A continuum among logarithmic, linear, and exponential functions, and its potential to improve generalization in neural networks .

我想尝试在 Keras 中实现这个激活函数。我之前已经实现过自定义激活,例如正弦激活:

def sin(x):
return K.sin(x)
get_custom_objects().update({'sin': Activation(sin)})

然而,本文中的激活函数有3个独特的属性:

  1. 它将输入的大小加倍(输出是输入的 2 倍)
  2. 已参数化
  3. 它的参数应该被正则化

我想一旦我有了处理上述三个问题的骨架,我就可以自己计算出数学公式,但我会寻求任何我能得到的帮助!

最佳答案

在这里,我们需要以下两者之一:

  • Lambda 层 - 如果您的参数不可训练(您不希望它们随着反向传播而改变)
  • 自定义层 - 如果您需要自定义可训练参数。

Lambda 层:

如果您的参数不可训练,您可以为 lambda 层定义函数。该函数接受一个输入张量,它可以返回您想要的任何内容:

import keras.backend as K

def customFunction(x):

#x can be either a single tensor or a list of tensors
#if a list, use the elements x[0], x[1], etc.

#Perform your calculations here using the keras backend
#If you could share which formula exactly you're trying to implement,
#it's possible to make this answer better and more to the point

#dummy example
alphaReal = K.variable([someValue])
alphaImag = K.variable([anotherValue]) #or even an array of values

realPart = alphaReal * K.someFunction(x) + ...
imagPart = alphaImag * K.someFunction(x) + ....

#You can return them as two outputs in a list (requires the fuctional API model
#Or you can find backend functions that join them together, such as K.stack

return [realPart,imagPart]

#I think the separate approach will give you a better control of what to do next.

要了解您可以做什么,请探索 backend functions .

对于参数,您可以将它们定义为 keras 常量或变量(K.constantK.variable),可以在上面的函数内部或外部,甚至可以将它们转换为模型输入。 See details in this answer

在您的模型中,您只需添加一个使用该函数的 lambda 层。

  • 在顺序模型中:model.add(Lambda(customFunction, output_shape=someShape))
  • 在函数式 API 模型中:output = Lambda(customFunction, ...)(inputOrListOfInputs)

如果您要将更多输入传递给函数,则需要函数模型 API。
如果您使用 Tensorflow,output_shape 将自动计算,我相信只有 Theano 需要它。 (不确定 CNTK)。

自定义层:

自定义图层是您创建的新类。仅当您的函数中有可训练参数时,才需要这种方法。 (如:用反向传播优化alpha)

Keras teaches it here .

基本上,您有一个 __init__ 方法(用于传递常量参数)、一个 build 方法(用于创建可训练参数(权重))、一个 call 方法将进行计算(如果没有可训练参数,则 lambda 层中将执行计算),以及一个 compute_output_shape 方法,以便您可以告诉模型输出形状是什么.

class CustomLayer(Layer):

def __init__(self, alphaReal, alphaImag):

self.alphaReal = alphaReal
self.alphaImage = alphaImag

def build(self,input_shape):

#weights may or may not depend on the input shape
#you may use it or not...

#suppose we want just two trainable values:
weigthShape = (2,)

#create the weights:
self.kernel = self.add_weight(name='kernel',
shape=weightShape,
initializer='uniform',
trainable=True)

super(CustomLayer, self).build(input_shape) # Be sure to call this somewhere!

def call(self,x):

#all the calculations go here:

#dummy example using the constant inputs
realPart = self.alphaReal * K.someFunction(x) + ...
imagPart = self.alphaImag * K.someFunction(x) + ....

#dummy example taking elements of the trainable weights
realPart = self.kernel[0] * realPart
imagPart = self.kernel[1] * imagPart

#all the comments for the lambda layer above are valid here

#example returning a list
return [realPart,imagPart]

def compute_output_shape(self,input_shape):

#if you decide to return a list of tensors in the call method,
#return a list of shapes here, twice the input shape:
return [input_shape,input_shape]

#if you stacked your results somehow in a single tensor, compute a single tuple, maybe with an additional dimension equal to 2:
return input_shape + (2,)

关于keras - 在keras中实现复杂的激活函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47024918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com