import tensorflow as tf

        red_ind = list(range(1, len(x.get_shape())))
        avoid_zero_div = 1e-8
        square = tf.maximum(avoid_zero_div,
                            reduce_sum(tf.square(grad),
                                       reduction_indices=red_ind,
                                       keepdims=True))
        normalized_grad = old_div(grad, tf.sqrt(square))
    else:
        normalized_grad = tf.sign(grad)
        normalized_grad = tf.stop_gradient(normalized_grad)

    scaled_grad = eps * normalized_grad
