Keras implementation of a gradient inversion layer for the Tensorflow backend, following the paper Domain-Adversarial Training of Neural Networks. Modified the Theano version by Pumpikano found here, expanding on the work done by VanushVaswani found here.
The layer can be placed in a Functional model such as:
Flip = flipGradientTF.GradientReversal(hp_lambda)
dann_in = Flip(previous_layer_output)
dann_out = Dense(2)(dann_in)
where hp_lambda
is the constant which multiplies the flipped gradient.
An example of a model where this is implemented (with full code) can be found here: https://github.com/michetonu/DA-RNN_manoeuver_anticipation
Works only with Tensorflow < 2.0.0! See here for alternative solutions with TF2 https://stackoverflow.com/questions/56841166/how-to-implement-gradient-reversal-layer-in-tf-2-0