How to Realize 'Gradient Reversal Layer' ?

8 ビュー (過去 30 日間)
Percy Hu
Percy Hu 2021 年 6 月 16 日
コメント済み: Percy Hu 2021 年 6 月 25 日
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.
  2 件のコメント
Percy Hu
Percy Hu 2021 年 6 月 16 日
編集済み: Percy Hu 2021 年 6 月 16 日
Percy Hu
Percy Hu 2021 年 6 月 16 日
編集済み: Percy Hu 2021 年 6 月 16 日
The gradient reversal layer has no parameters associated with it. During the forward propagation, the GRL acts as an identity transformation. During the backpropagation however, the GRL takes the gradient from the subsequent level and changes its sign, i.e., multiplies it by -1, before passing it to the preceding layer. Implementing such a layer using existing object-oriented packages for deep learning is simple, requiring only to dene procedures for the forward propagation (identity transformation), and backpropagation (multiplying by -1). The layer requires no parameter update.

サインインしてコメントする。

採用された回答

Philip Brown
Philip Brown 2021 年 6 月 21 日
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
methods
function Z = predict(layer, X)
Z = X; % Identity
end
function dLdX = backward(layer, X, dLdZ)
dLdX = -dLdZ; % Reverse gradient
end
end
end
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.
  1 件のコメント
Percy Hu
Percy Hu 2021 年 6 月 25 日
thank you very much for your help. It is really kind of you for your detailed explanation. and i hope your answer will help more people.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by