简体   繁体   English

在Keras中使用自定义步骤激活功能会导致“操作没有梯度梯度的操作”错误。 如何解决呢?

[英]Using a custom step activation function in Keras results in “An operation has `None` for gradient.” error. How to resolve this?

I am building auto-encoder and I want to encode my values into a logical matrix. 我正在构建自动编码器,我想将我的值编码为逻辑矩阵。 However, when I'm using my custom step activation function in one of the intermediate layers (all other layers are using 'relu'), keras raises this error: 但是,当我在一个中间层(所有其他层都使用“ relu”)中使用我的自定义步骤激活功能时,keras会引发以下错误:

An operation has `None` for gradient.

I've tried using hard-sigmoid function, but it doesn't fit my problem, because it still produces intermediate values, when I only need binary. 我尝试使用硬S型函数,但是它不适合我的问题,因为当我只需要二进制时,它仍然会产生中间值。 I am aware, that at most points my function has no gradient, but is it possible to use some other function for gradient calculation and still use step function for accuracy and loss calculations? 我知道,我的函数在大多数时候都没有梯度,但是是否可以使用其他函数进行梯度计算,而仍使用步进函数进行精度和损耗计算?

My activation function: 我的激活功能:

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)
    return keras.backend.switch(x > 0.5, ones, zeros)

I expect to be able to use binary step activation function to train the network and then use it as a typical auto-encoder. 我希望能够使用二进制步进激活功能来训练网络,然后将其用作典型的自动编码器。 Something simmilar to binary feature map used in this paper . 本文使用的类似二元特征映射的东西。

As mentioned here , you could use tf.custom_gradient to define a "back-propagatable" gradient for your activation function. 如前所述在这里 ,你可以使用tf.custom_gradient定义“后繁殖的”梯度为您激活功能。

Perhaps something like: 也许像这样:

@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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Keras 中使用自定义步骤激活 function 会导致“'tuple' object has no attribute '_keras_shape'”错误。 如何解决这个问题? - Using a custom step activation function in Keras results in “'tuple' object has no attribute '_keras_shape'” error. How to resolve this? Keras 自定义层 ValueError:操作对渐变具有“无”。 - Keras Custom Layer ValueError: An operation has `None` for gradient. Keras 丢失 function 值错误:ValueError:操作对梯度有“无”。 在 LSTM 网络上 - Keras loss function value error: ValueError: An operation has `None` for gradient. on LSTM network 有扩展现有激活功能的简单方法吗? 我的自定义softmax函数返回:一个操作的梯度没有“无” - Is there a simple way to extend an existing activation function? My custom softmax function returns: An operation has `None` for gradient ValueError:一个操作对渐变有“无” - 不使用自定义 - ValueError: An operation has `None` for gradient - Not using Custom 自定义 keras 层引发:ValueError:渐变操作具有“无” - Custom keras layer raises : ValueError: An operation has `None` for gradient Keras自定义图层:ValueError:某个操作的梯度没有“无” - Keras Custom Layer: ValueError: An operation has `None` for gradient Keras 自定义层 ValueError:一个操作对梯度有“None” - Keras Custom Layer ValueError: An operation has `None` for gradient Keras自定义丢失实现:ValueError:对于渐变,操作具有“无” - Keras custom loss implementation : ValueError: An operation has `None` for gradient 张量的 ValueError(“变量 {} 有 `None` 的梯度。”) - ValueError(“Variable {} has `None` for gradient. ”) for Tensor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM