(
        dis: Discriminator,
        real_samples: Tensor,
        fake_samples: Tensor,
        depth: int,
        alpha: float,
        reg_lambda: float = 10,
        labels: Optional[Tensor] = None,
    )
return penalty
private helper for calculating the gradient penalty
        Args:
            dis: the discriminator used for computing the penalty
            real_samples: real samples
            fake_samples: fake samples
            depth: current depth in the optimization
            alpha: current alpha for fade-in
            reg_lambda: regularisation lambda

        Returns: computed gradient penalty
private helper for calculating the gradient penalty
        Args:
            dis: the discriminator used for computing the penalty
            real_samples: real samples
            fake_samples: fake samples
            depth: current depth in the optimization
            alpha: current alpha for fade-in
            reg_lambda: regularisation lambda
private helper for calculating the gradient penalty Args : dis : the discriminator used for computing the penalty real_samples : real samples fake_samples : fake samples depth : current depth in the optimization alpha : current alpha for fade - in reg_lambda : regularisation lambda
def _gradient_penalty(
        dis: Discriminator,
        real_samples: Tensor,
        fake_samples: Tensor,
        depth: int,
        alpha: float,
        reg_lambda: float = 10,
        labels: Optional[Tensor] = None,
    ) -> Tensor:
        """
        private helper for calculating the gradient penalty
        Args:
            dis: the discriminator used for computing the penalty
            real_samples: real samples
            fake_samples: fake samples
            depth: current depth in the optimization
            alpha: current alpha for fade-in
            reg_lambda: regularisation lambda

        Returns: computed gradient penalty
        """
        batch_size = real_samples.shape[0]

        # generate random epsilon
        epsilon = torch.rand((batch_size, 1, 1, 1)).to(real_samples.device)

        # create the merge of both real and fake samples
        merged = epsilon * real_samples + ((1 - epsilon) * fake_samples)
        merged.requires_grad_(True)

        # forward pass
        if labels is not None:
            assert dis.conditional, "labels passed to an unconditional discriminator"
            op = dis(merged, depth, alpha, labels)
        else:
            op = dis(merged, depth, alpha)

        # perform backward pass from op to merged for obtaining the gradients
        gradient = torch.autograd.grad(
            outputs=op,
            inputs=merged,
            grad_outputs=torch.ones_like(op),
            create_graph=True,
            retain_graph=True,
            only_inputs=True,
        )[0]

        # calculate the penalty using these gradients
        gradient = gradient.view(gradient.shape[0], -1)
        penalty = reg_lambda * ((gradient.norm(p=2, dim=1) - 1) ** 2).mean()

        # return the calculated penalty:
        return penalty
def _gradient_penalty ( dis : Discriminator , real_samples : Tensor , fake_samples : Tensor , depth : int , alpha : float , reg_lambda : float = 10 , labels : Optional [ Tensor ] = None , ) -> Tensor : batch_size = real_samples . shape [ 0 ] # generate random epsilon epsilon = torch . rand ( ( batch_size , 1 , 1 , 1 ) ) . to ( real_samples . device ) # create the merge of both real and fake samples merged = epsilon * real_samples + ( ( 1 - epsilon ) * fake_samples ) merged . requires_grad_ ( True ) # forward pass if labels is not None : assert dis . conditional , "labels passed to an unconditional discriminator" op = dis ( merged , depth , alpha , labels ) else : op = dis ( merged , depth , alpha ) # perform backward pass from op to merged for obtaining the gradients gradient = torch . autograd . grad ( outputs = op , inputs = merged , grad_outputs = torch . ones_like ( op ) , create_graph = True , retain_graph = True , only_inputs = True , ) [ 0 ] # calculate the penalty using these gradients gradient = gradient . view ( gradient . shape [ 0 ] , - 1 ) penalty = reg_lambda * ( ( gradient . norm ( p = 2 , dim = 1 ) - 1 ) ** 2 ) . mean ( ) # return the calculated penalty: return penalty