(self, real_data, fake_data, device='cpu', pac=10, lambda_=10)
return gradient_penalty
Compute the gradient penalty.
Compute the gradient penalty.
Compute the gradient penalty .
def calc_gradient_penalty(self, real_data, fake_data, device='cpu', pac=10, lambda_=10):
        """Compute the gradient penalty."""
        alpha = torch.rand(real_data.size(0) // pac, 1, 1, device=device)
        alpha = alpha.repeat(1, pac, real_data.size(1))
        alpha = alpha.view(-1, real_data.size(1))

        interpolates = alpha * real_data + ((1 - alpha) * fake_data)

        disc_interpolates = self(interpolates)

        gradients = torch.autograd.grad(
            outputs=disc_interpolates, inputs=interpolates,
            grad_outputs=torch.ones(disc_interpolates.size(), device=device),
            create_graph=True, retain_graph=True, only_inputs=True
        )[0]

        gradients_view = gradients.view(-1, pac * real_data.size(1)).norm(2, dim=1) - 1
        gradient_penalty = ((gradients_view) ** 2).mean() * lambda_

        return gradient_penalty
def calc_gradient_penalty ( self , real_data , fake_data , device = 'cpu' , pac = 10 , lambda_ = 10 ) : alpha = torch . rand ( real_data . size ( 0 ) // pac , 1 , 1 , device = device ) alpha = alpha . repeat ( 1 , pac , real_data . size ( 1 ) ) alpha = alpha . view ( - 1 , real_data . size ( 1 ) ) interpolates = alpha * real_data + ( ( 1 - alpha ) * fake_data ) disc_interpolates = self ( interpolates ) gradients = torch . autograd . grad ( outputs = disc_interpolates , inputs = interpolates , grad_outputs = torch . ones ( disc_interpolates . size ( ) , device = device ) , create_graph = True , retain_graph = True , only_inputs = True ) [ 0 ] gradients_view = gradients . view ( - 1 , pac * real_data . size ( 1 ) ) . norm ( 2 , dim = 1 ) - 1 gradient_penalty = ( ( gradients_view ) ** 2 ) . mean ( ) * lambda_ return gradient_penalty