Different Forward and Backward Propagation in TensroFlow2 & Keras

Viewed 602

I am training a neural network when in the forward pass, randomly half of the time a non-differentiable activation is used which rounds activation to either 0 or 1 (binary), and the other half it uses a differentiable function similar to Sigmoid (saturated-Sigmoid to be exact) In the backward pass however, we use the gradient with regard to the differentiable function, even when we have used the non-differentiable discrete one for forward pass. The code I have so far is:

diff_active = tf.math.maximum(sat_sigmoid_term1(feature), sat_sigmoid_term2(feature))
binary_masks = diff_active 
rand_cond = tf.random.uniform([1,])
cond = tf.constant(rand_cond, shape=[1,])
if cond <0.5:
        with tf.GradientTape() as tape:
            non_diff_active = tf.grad_pass_through(tf.keras.layers.Lambda(lambda x: tf.where(tf.math.greater(x,0), x, tf.zeros_like(x))))(feature)
            grads = tape.gradient(non_diff_active , feature)
            binary_masks = non_diff_active  
tf.math.multiply(binary_masks, feature)  

My intuition is that this way, the differentiable activation is always applied (and hopefully gradient of it is always included in bacl-prop) and with tf.grad_pass_through() I can apply the non-differentiable activation while replacing it's back-propagation with identity matrix. However, I am not sure if my use of tf.grad_pass_through() or the way I condition of my random variable is correct aand if the behaviour is as intended?

1 Answers

You can use tf.custom_gradient for that:

import tensorflow as tf

@tf.function
def sigmoid_grad(x):
    return tf.gradients(tf.math.sigmoid(x), x)[0]

@tf.custom_gradient
def sigmoid_or_bin(x, rand):
    rand = tf.convert_to_tensor(rand)
    out = tf.cond(rand > 0.5,
                  lambda: tf.math.sigmoid(x),
                  lambda: tf.dtypes.cast(x > 0, x.dtype))
    return out, lambda y: (y * sigmoid_grad(x), None)

# Test
tf.random.set_seed(0)
x = tf.random.uniform([4], -1, 1)
tf.print(x)
# [-0.416049719 -0.586867094 0.0707814693 0.122514963]
with tf.GradientTape() as t:
    t.watch(x)
    y = tf.math.sigmoid(x)
tf.print(y)
# [0.397462428 0.357354015 0.517688 0.530590475]
tf.print(t.gradient(y, x))
# [0.239486054 0.229652107 0.249687135 0.249064222]
with tf.GradientTape() as t:
    t.watch(x)
    y = sigmoid_or_bin(x, 0.2)
tf.print(y)
# [0 0 1 1]
tf.print(t.gradient(y, x))
# [0.239486054 0.229652107 0.249687135 0.249064222]
with tf.GradientTape() as t:
    t.watch(x)
    y = sigmoid_or_bin(x, 0.8)
tf.print(y)
# [0.397462428 0.357354015 0.517688 0.530590475]
tf.print(t.gradient(y, x))
# [0.239486054 0.229652107 0.249687135 0.249064222]
Related