├── flip_gradient.py └── README.md /flip_gradient.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | def flip_gradient(x, l=1.0): 4 | positive_path = tf.stop_gradient(x * tf.cast(1 + l, tf.float32)) 5 | negative_path = -x * tf.cast(l, tf.float32) 6 | return positive_path + negative_path -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GradientReversal 2 | 3 | Implements the Gradient Reversal layer from Unsupervised Domain Adaptation by Backpropagation (https://arxiv.org/abs/1409.7495) and Domain-Adversarial Training of Neural Networks (https://arxiv.org/abs/1505.07818) in Tensorflow. 4 | 5 | The forward pass is the identify function, but the backward pass multiplies the gradients by -lambda. 6 | --------------------------------------------------------------------------------