Hi all.
I want to check the gradient is reversed well or not.
Then how to check the gradients ?
class GradReverse(Function):
    def __init__(self, lambd):
        self.lambd = lambd

    def forward(self, x):
        return x.view_as(x)

    def backward(self, grad_output):
        return grad_output * -self.lambd


def grad_reverse(x, lambd=1.0):
    return GradReverse(lambd)(x)

class Discriminator(nn.Module):
    def __init__(self, channel, pool, classes=10):
        super(Discriminator, self).__init__()
        self.cls_fn = nn.Linear(channel, classes)

    def forward(self, x, reverse=False, eta=0.1):
        if reverse:
            x = grad_reverse(x, eta)
        x = self.cls_fn(x)
        return x

def train():
    discriminator = Discriminator()

    out = discriminator(input, reverse=True)
    loss = CE_criterion(out, label)

    out2 = discriminator(input, reverse=False)
    loss2 = CE_criterion(out2, label)

    loss.backward(retain_graph=True)
    loss2.backward()