class_docstr: Implements the gradient penalty loss term for WGANs.
  
  This class implements the gradient penalty loss term for WGANs as described in
  Gulrajani et al., "Improved Training of Wasserstein GANs" [1]_.  It is used
  internally by WGANModel
  
  Examples
  --------
  Importing necessary modules
  
  >>> import deepchem
  >>> from deepchem.models.torch_models.gan import WGANModel
  >>> from deepchem.models.torch_models import GradientPenaltyLayer
  >>> import torch
  >>> import torch.nn as nn
  >>> import torch.nn.functional as F
  
  Creating a Generator
  
  >>> class Generator(nn.Module):
  ...     def __init__(self, noise_input_shape, conditional_input_shape):
  ...         super(Generator, self).__init__()
  ...         self.noise_input_shape = noise_input_shape
  ...         self.conditional_input_shape = conditional_input_shape
  ...         self.noise_dim = noise_input_shape[1:]
  ...         self.conditional_dim = conditional_input_shape[1:]
  ...         input_dim = sum(self.noise_dim) + sum(self.conditional_dim)
  ...         self.output = nn.Linear(input_dim, 1)
  ...     def forward(self, input):
  ...         noise_input, conditional_input = input
  ...         inputs = torch.cat((noise_input, conditional_input), dim=1)
  ...         output = self.output(inputs)
  ...         return output
  
  Creating a Discriminator
  
  >>> class Discriminator(nn.Module):
  ...     def __init__(self, data_input_shape, conditional_input_shape):
  ...         super(Discriminator, self).__init__()
  ...         self.data_input_shape = data_input_shape
  ...         self.conditional_input_shape = conditional_input_shape
  ...         # Extracting the actual data dimension
  ...         data_dim = data_input_shape[1:]
  ...         # Extracting the actual conditional dimension
  ...         conditional_dim = conditional_input_shape[1:]
  ...         input_dim = sum(data_dim) + sum(conditional_dim)
  ...         # Define the dense layers
  ...         self.dense1 = nn.Linear(input_dim, 10)
  ...         self.dense2 = nn.Linear(10, 1)
  ...     def forward(self, input):
  ...         data_input, conditional_input = input
  ...         # Concatenate data_input and conditional_input along the second dimension
  ...         discrim_in = torch.cat((data_input, conditional_input), dim=1)
  ...         # Pass the concatenated input through the dense layers
  ...         x = F.relu(self.dense1(discrim_in))
  ...         output = torch.sigmoid(self.dense2(x))
  ...         return output
  
  Creating an Example WGANModel class
  
  >>> class ExampleWGAN(WGANModel):
  ...     def get_noise_input_shape(self):
  ...         return (100,2,)
  ...     def get_data_input_shapes(self):
  ...         return [(100,1,)]
  ...     def get_conditional_input_shapes(self):
  ...         return [(100,1,)]
  ...     def create_generator(self):
  ...         noise_dim = self.get_noise_input_shape()
  ...         conditional_dim = self.get_conditional_input_shapes()[0]
  ...         return nn.Sequential(Generator(noise_dim, conditional_dim))
  ...     def create_discriminator(self):
  ...         data_input_shape = self.get_data_input_shapes()[0]
  ...         conditional_input_shape = self.get_conditional_input_shapes()[0]
  ...         return nn.Sequential(
  ...             Discriminator(data_input_shape, conditional_input_shape))
  
  Defining an Example GradientPenaltyLayer
  
  >>> wgan = ExampleWGAN()
  >>> discriminator = wgan.discriminators[0]
  >>> gpl = GradientPenaltyLayer(wgan, discriminator)
  >>> inputs = [torch.randn(4, 1)]
  >>> conditional_inputs = [torch.randn(4, 1)]
  >>> output, penalty = gpl(inputs, conditional_inputs)
  
  
  References
  ----------
  .. [1]_ Gulrajani, Ishaan, et al. "Improved training of wasserstein gans."
      Advances in neural information processing systems 30 (2017).
      (https://arxiv.org/abs/1704.00028)
class_docstr_tok: ["Implements","the","gradient","penalty","loss","term","for","WGANs",".","This","class","implements","the","gradient","penalty","loss","term","for","WGANs","as","described","in","Gulrajani","et","al.",",","``","Improved","Training","of","Wasserstein","GANs","''","[","1","]","_",".","It","is","used","internally","by","WGANModel","Examples","--","--","--","--","Importing","necessary","modules",">",">",">","import","deepchem",">",">",">","from","deepchem.models.torch_models.gan","import","WGANModel",">",">",">","from","deepchem.models.torch_models","import","GradientPenaltyLayer",">",">",">","import","torch",">",">",">","import","torch.nn","as","nn",">",">",">","import","torch.nn.functional","as","F","Creating","a","Generator",">",">",">","class","Generator","(","nn.Module",")",":","...","def","__init__","(","self",",","noise_input_shape",",","conditional_input_shape",")",":","...","super","(","Generator",",","self",")",".__init__","(",")","...","self.noise_input_shape","=","noise_input_shape","...","self.conditional_input_shape","=","conditional_input_shape","...","self.noise_dim","=","noise_input_shape","[","1",":","]","...","self.conditional_dim","=","conditional_input_shape","[","1",":","]","...","input_dim","=","sum","(","self.noise_dim",")","+","sum","(","self.conditional_dim",")","...","self.output","=","nn.Linear","(","input_dim",",","1",")","...","def","forward","(","self",",","input",")",":","...","noise_input",",","conditional_input","=","input","...","inputs","=","torch.cat","(","(","noise_input",",","conditional_input",")",",","dim=1",")","...","output","=","self.output","(","inputs",")","...","return","output","Creating","a","Discriminator",">",">",">","class","Discriminator","(","nn.Module",")",":","...","def","__init__","(","self",",","data_input_shape",",","conditional_input_shape",")",":","...","super","(","Discriminator",",","self",")",".__init__","(",")","...","self.data_input_shape","=","data_input_shape","...","self.conditional_input_shape","=","conditional_input_shape","...","#","Extracting","the","actual","data","dimension","...","data_dim","=","data_input_shape","[","1",":","]","...","#","Extracting","the","actual","conditional","dimension","...","conditional_dim","=","conditional_input_shape","[","1",":","]","...","input_dim","=","sum","(","data_dim",")","+","sum","(","conditional_dim",")","...","#","Define","the","dense","layers","...","self.dense1","=","nn.Linear","(","input_dim",",","10",")","...","self.dense2","=","nn.Linear","(","10",",","1",")","...","def","forward","(","self",",","input",")",":","...","data_input",",","conditional_input","=","input","...","#","Concatenate","data_input","and","conditional_input","along","the","second","dimension","...","discrim_in","=","torch.cat","(","(","data_input",",","conditional_input",")",",","dim=1",")","...","#","Pass","the","concatenated","input","through","the","dense","layers","...","x","=","F.relu","(","self.dense1","(","discrim_in",")",")","...","output","=","torch.sigmoid","(","self.dense2","(","x",")",")","...","return","output","Creating","an","Example","WGANModel","class",">",">",">","class","ExampleWGAN","(","WGANModel",")",":","...","def","get_noise_input_shape","(","self",")",":","...","return","(","100,2",",",")","...","def","get_data_input_shapes","(","self",")",":","...","return","[","(","100,1",",",")","]","...","def","get_conditional_input_shapes","(","self",")",":","...","return","[","(","100,1",",",")","]","...","def","create_generator","(","self",")",":","...","noise_dim","=","self.get_noise_input_shape","(",")","...","conditional_dim","=","self.get_conditional_input_shapes","(",")","[","0","]","...","return","nn.Sequential","(","Generator","(","noise_dim",",","conditional_dim",")",")","...","def","create_discriminator","(","self",")",":","...","data_input_shape","=","self.get_data_input_shapes","(",")","[","0","]","...","conditional_input_shape","=","self.get_conditional_input_shapes","(",")","[","0","]","...","return","nn.Sequential","(","...","Discriminator","(","data_input_shape",",","conditional_input_shape",")",")","Defining","an","Example","GradientPenaltyLayer",">",">",">","wgan","=","ExampleWGAN","(",")",">",">",">","discriminator","=","wgan.discriminators","[","0","]",">",">",">","gpl","=","GradientPenaltyLayer","(","wgan",",","discriminator",")",">",">",">","inputs","=","[","torch.randn","(","4",",","1",")","]",">",">",">","conditional_inputs","=","[","torch.randn","(","4",",","1",")","]",">",">",">","output",",","penalty","=","gpl","(","inputs",",","conditional_inputs",")","References","--","--","--","--","--","..","[","1","]","_","Gulrajani",",","Ishaan",",","et","al",".","``","Improved","training","of","wasserstein","gans",".","''","Advances","in","neural","information","processing","systems","30","(","2017",")",".","(","https",":","\/\/arxiv.org\/abs\/1704.00028",")"]
docstr: Construct a GradientPenaltyLayer.
  
  Parameters
  ----------
  gan : WGANModel
      the WGANModel that this layer is part of
  discriminator : nn.Module
      the discriminator to compute the gradient penalty for
docstr_tok: ["Construct","a","GradientPenaltyLayer",".","Parameters","--","--","--","--","--","gan",":","WGANModel","the","WGANModel","that","this","layer","is","part","of","discriminator",":","nn.Module","the","discriminator","to","compute","the","gradient","penalty","for"]
code: def __init__(
      self, gan: WGANModel, discriminator: nn.Module, **kwargs
  ) -> None:
      """Construct a GradientPenaltyLayer.
  
      Parameters
      ----------
      gan : WGANModel
          the WGANModel that this layer is part of
      discriminator : nn.Module
          the discriminator to compute the gradient penalty for
      """
      super(GradientPenaltyLayer, self).__init__(**kwargs)
      self.gan = gan
      self.discriminator = discriminator
  
imports: import numpy
  import time
  from typing import Callable, Any, List, Tuple, Union
  from deepchem.models.torch_models.torch_model import TorchModel
label_desc: Use image node_id 1 to create a new GradientPenaltyLayer object from inherited base classes: nn with example: obj = GradientPenaltyLayer(gan, discriminator)