Tensorflow:逻辑层(可训练的碰撞功能)

问题描述 投票:5回答:1

我正在尝试对bump-function的以下变体进行编码,逐个应用enter image description here

其中σ是可训练的;但它不起作用(错误报告如下)...


我的尝试:

这是我到目前为止编写的内容(如果有帮助的话:

假设我有两个功能(例如):

  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

错误报告:

    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

...

tensorflow:Gradients do not exist for variables 

此外,它似乎不是按组件应用的(除了不可训练的问题之外)。任何想法和事前感谢。

tensorflow keras customization layer tf.keras
1个回答
0
投票

[不幸的是,任何用于检查x是否在(-σ, σ)内的操作将无法区分,因此无法通过任何梯度下降方法来学习σ。错误消息正好表明了这一点-无法计算相对于self.threshold_level的梯度,因为tf.math.less相对于其输入是不可微分的。

关于元素方式的条件,您可以改为使用tf.where根据条件的元素方式布尔值从f_True(input)f_False(input)中选择元素。例如:

output_step_3 = tf.where(The_Logic, f_True(input), f_False(input))
© www.soinside.com 2019 - 2024. All rights reserved.