<filename>torchvision/models/gradient_reversal.py
import torch.nn as nn
from torch.autograd import Function #it represents a functions that works on tensors, i have to specify what is does forward and backward

''' 
Very easy template to start for developing your AlexNet with DANN 
Has not been tested, might contain incompatibilities with most recent versions of PyTorch (you should address this)
However, the logic is consistent
'''


class ReverseLayerF(Function):  #GradientReversalFn
    # Forwards identity
    # Sends backward reversed gradients
    @staticmethod
    def forward(ctx, x, alpha):
        ctx.alpha = alpha   #this stores the parameter of the network and it is what i have to multiply the gradient by

        return x.view_as(x)

    @staticmethod
    def backward(ctx, grad_output):
        output = grad_output.neg() * ctx.alpha

        return output, None


class RandomNetworkWithReverseGrad(nn.Module):
    def __init__(self, **kwargs):
        super(RandomNetworkWithReverseGrad, self).__init__()
        self.features = nn.Sequential(...)
        self.classifier = nn.Sequential(...)
        self.dann_classifier = nn.Sequential(...)

    def forward(self, x, alpha=None):
        features = self.features
        # Flatten the features:
        features = features.view(features.size(0), -1)
        # If we pass alpha, we can assume we are training the discriminator
        if alpha is not None:
            # gradient reversal layer (backward gradients will be reversed)
            reverse_feature = ReverseLayerF.apply(features, alpha)
            discriminator_output = ...
            return discriminator_output
        # If we don't pass alpha, we assume we are training with supervision
        else:
            # do something else
            class_outputs = ...
            return class_outputs

<filename>torchvision/models/gradient_reversal.py
import torch.nn as nn
from torch.autograd import Function #it represents a functions that works on tensors, i have to specify what is does forward and backward

''' 
Very easy template to start for developing your AlexNet with DANN 
Has not been tested, might contain incompatibilities with most recent versions of PyTorch (you should address this)
However, the logic is consistent
'''


class ReverseLayerF(Function):  #GradientReversalFn
    # Forwards identity
    # Sends backward reversed gradients
    @staticmethod
    def forward(ctx, x, alpha):
        ctx.alpha = alpha   #this stores the parameter of the network and it is what i have to multiply the gradient by

        return x.view_as(x)

    @staticmethod
    def backward(ctx, grad_output):
        output = grad_output.neg() * ctx.alpha

        return output, None


class RandomNetworkWithReverseGrad(nn.Module):
    def __init__(self, **kwargs):
        super(RandomNetworkWithReverseGrad, self).__init__()
        self.features = nn.Sequential(...)
        self.classifier = nn.Sequential(...)
        self.dann_classifier = nn.Sequential(...)

    def forward(self, x, alpha=None):
        features = self.features
        # Flatten the features:
        features = features.view(features.size(0), -1)
        # If we pass alpha, we can assume we are training the discriminator
        if alpha is not None:
            # gradient reversal layer (backward gradients will be reversed)
            reverse_feature = ReverseLayerF.apply(features, alpha)
            discriminator_output = ...
            return discriminator_output
        # If we don't pass alpha, we assume we are training with supervision
        else:
            # do something else
            class_outputs = ...
            return class_outputs

#it represents a functions that works on tensors, i have to specify what is does forward and backward Very easy template to start for developing your AlexNet with DANN  Has not been tested, might contain incompatibilities with most recent versions of PyTorch (you should address this) However, the logic is consistent #GradientReversalFn # Forwards identity # Sends backward reversed gradients #this stores the parameter of the network and it is what i have to multiply the gradient by # Flatten the features: # If we pass alpha, we can assume we are training the discriminator # gradient reversal layer (backward gradients will be reversed) # If we don't pass alpha, we assume we are training with supervision # do something else