更新时间: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