original_docstring: Compute the parameters gradients for the WGAN-GP loss
docstring: Compute the parameters gradients for the WGAN-GP loss
docstring_tokens: Compute the parameters gradients for the WGAN - GP loss
code: def gradient_penalty(self, real_image, fake_image):
          """Compute the parameters gradients for the WGAN-GP loss"""
          alpha = torch.rand(1, device=self.device)
          interpolation = alpha * real_image + (1 - alpha) * fake_image
          interpolation.requires_grad = True
  
          output = self(interpolation)
          gradient = torch.autograd.grad(output.sum(), interpolation,
              create_graph=True, retain_graph=True, only_inputs=True)[0]
  
          gradient_penalty = ((gradient.norm(2, dim=1) - 1) ** 2).mean()
          return gradient_penalty
code_tokens: def gradient_penalty ( self , real_image , fake_image ) : alpha = torch . rand ( 1 , device = self . device ) interpolation = alpha * real_image + ( 1 - alpha ) * fake_image interpolation . requires_grad = True output = self ( interpolation ) gradient = torch . autograd . grad ( output . sum ( ) , interpolation , create_graph = True , retain_graph = True , only_inputs = True ) [ 0 ] gradient_penalty = ( ( gradient . norm ( 2 , dim = 1 ) - 1 ) ** 2 ) . mean ( ) return gradient_penalty
short_docstring: Compute the parameters gradients for the WGAN-GP loss
short_docstring_tokens: Compute the parameters gradients for the WGAN - GP loss
comment: """Compute the parameters gradients for the WGAN-GP loss"""