How to Realize 'Gradient Reversal Layer' ?
4 views (last 30 days)
How can i complete a 'Gradient Reversal Layer' in matlab like in pytorch or tensorflow?
It is normally used in transfer learning network when a GAN-like loss is adopted.
Could i realize it by define a custom layer?
It is very grateful if you can offer an example of some detailed advice. Thank you for your help.
Philip Brown on 21 Jun 2021
It looks like you should be able to do this by writing your own custom layer. See the "Intermediate Layer Template" for some code to get started.
There's a custom layer used in a visualization example which does something a little bit similar (to modify the behavior of a ReLU gradient), here.
I think the custom layer code you need looks something like this:
classdef GradientReversalLayer < nnet.layer.Layer
function Z = predict(layer, X)
Z = X; % Identity
function dLdX = backward(layer, X, dLdZ)
dLdX = -dLdZ; % Reverse gradient
If you want to define the constant you multiple the gradient by, you could make it a property of the custom layer and include that in your backward function.