且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Keras中使用自定义步骤激活功能会导致“操作具有“无"梯度.错误.如何解决呢?

更新时间:2023-02-25 21:46:33

如上所述

As mentioned here, you could use tf.custom_gradient to define a "back-propagatable" gradient for your activation function.

也许是这样的:

@tf.custom_gradient
def binary_activation(x):

    ones = tf.ones(tf.shape(x), dtype=x.dtype.base_dtype)
    zeros = tf.zeros(tf.shape(x), dtype=x.dtype.base_dtype)

    def grad(dy):
        return ...  # TODO define gradient
  return keras.backend.switch(x > 0.5, ones, zeros), grad