在 Keras/Tensorflow 中实现可训练的广义 Bump 函数层

BLB*_*LBA 7 python machine-learning keras tensorflow tf.keras

我正在尝试编写按组件应用的Bump 函数的以下变体:

广义凹凸函数方程,

在哪里 ?可训练;但它不起作用(下面报告了错误)。


我的尝试:

这是我到目前为止编码的内容(如果有帮助的话)。假设我有两个函数(例如):

  def f_True(x):
    # Compute Bump Function
    bump_value = 1-tf.math.pow(x,2)
    bump_value = -tf.math.pow(bump_value,-1)
    bump_value = tf.math.exp(bump_value)
    return(bump_value)

  def f_False(x):
    # Compute Bump Function
    x_out = 0*x
    return(x_out)

class trainable_bump_layer(tf.keras.layers.Layer):

    def __init__(self, *args, **kwargs):
        super(trainable_bump_layer, self).__init__(*args, **kwargs)

    def build(self, input_shape):
        self.threshold_level = self.add_weight(name='threshlevel',
                                    shape=[1],
                                    initializer='GlorotUniform',
                                    trainable=True)

    def call(self, input):
        # Determine Thresholding Logic
        The_Logic = tf.math.less(input,self.threshold_level)
        # Apply Logic
        output_step_3 = tf.cond(The_Logic, 
                                lambda: f_True(input),
                                lambda: f_False(input))
        return output_step_3
Run Code Online (Sandbox Code Playgroud)

错误报告:

    Train on 100 samples
Epoch 1/10
WARNING:tensorflow:Gradients do not exist for variables ['reconfiguration_unit_steps_3_3/threshlevel:0'] when minimizing the loss.
WARNING:tensorflow:Gradients do not exist for variables ['reconfiguration_unit_steps_3_3/threshlevel:0'] when minimizing the loss.
 32/100 [========>.....................] - ETA: 3s
Run Code Online (Sandbox Code Playgroud)

...

tensorflow:Gradients do not exist for variables 
Run Code Online (Sandbox Code Playgroud)

此外,它似乎没有应用于组件方面(除了不可训练的问题)。可能是什么问题呢?