class_docstr: Classifier based on GPT2 modules. Please see
  :class:`~texar.tf.modules.PretrainedGPT2Mixin` for a brief description
  of GPT2.
  
  This is a combination of the
  :class:`~texar.tf.modules.GPT2Encoder` with a classification
  layer. Both step-wise classification and sequence-level classification
  are supported, specified in :attr:`hparams`.
  
  Arguments are the same as in
  :class:`~texar.tf.modules.GPT2Encoder`.
  
  Args:
      pretrained_model_name (optional): a `str`, the name
          of pre-trained model (e.g., ``gpt2-small``). Please refer to
          :class:`~texar.tf.modules.PretrainedGPT2Mixin` for
          all supported models.
          If `None`, the model name in :attr:`hparams` is used.
      cache_dir (optional): the path to a folder in which the
          pre-trained models will be cached. If `None` (default),
          a default directory (``texar_data`` folder under user's home
          directory) will be used.
      hparams (dict or HParams, optional): Hyperparameters. Missing
          hyperparameter will be set to default values. See
          :meth:`default_hparams` for the hyperparameter structure
          and default values.
  
  .. document private functions
  .. automethod:: _build
class_docstr_tok: ["Classifier","based","on","GPT2","modules",".","Please","see",":","class",":","`","~texar.tf.modules.PretrainedGPT2Mixin","`","for","a","brief","description","of","GPT2",".","This","is","a","combination","of","the",":","class",":","`","~texar.tf.modules.GPT2Encoder","`","with","a","classification","layer",".","Both","step-wise","classification","and","sequence-level","classification","are","supported",",","specified","in",":","attr",":","`","hparams","`",".","Arguments","are","the","same","as","in",":","class",":","`","~texar.tf.modules.GPT2Encoder","`",".","Args",":","pretrained_model_name","(","optional",")",":","a","`","str","`",",","the","name","of","pre-trained","model","(","e.g.",",","``","gpt2-small","``",")",".","Please","refer","to",":","class",":","`","~texar.tf.modules.PretrainedGPT2Mixin","`","for","all","supported","models",".","If","`","None","`",",","the","model","name","in",":","attr",":","`","hparams","`","is","used",".","cache_dir","(","optional",")",":","the","path","to","a","folder","in","which","the","pre-trained","models","will","be","cached",".","If","`","None","`","(","default",")",",","a","default","directory","(","``","texar_data","``","folder","under","user","'s","home","directory",")","will","be","used",".","hparams","(","dict","or","HParams",",","optional",")",":","Hyperparameters",".","Missing","hyperparameter","will","be","set","to","default","values",".","See",":","meth",":","`","default_hparams","`","for","the","hyperparameter","structure","and","default","values",".","..","document","private","functions","..","automethod",":",":","_build"]
code: def __init__(
      self, pretrained_model_name=None, cache_dir=None, hparams=None
  ):
      super().__init__(hparams=hparams)
  
      with tf.variable_scope(self.variable_scope):
          encoder_hparams = dict_fetch(
              hparams, GPT2Encoder.default_hparams()
          )
          if encoder_hparams is not None:
              encoder_hparams["name"] = None
  
          self._encoder = GPT2Encoder(
              pretrained_model_name=pretrained_model_name,
              cache_dir=cache_dir,
              hparams=encoder_hparams,
          )
  
          # Creates an dropout layer
          drop_kwargs = {"rate": self._hparams.dropout}
          layer_hparams = {"type": "Dropout", "kwargs": drop_kwargs}
          self._dropout_layer = get_layer(hparams=layer_hparams)
  
          # Creates an additional classification layer if needed
          self._num_classes = self._hparams.num_classes
          if self._num_classes <= 0:
              self._logit_layer = None
          else:
              logit_kwargs = self._hparams.logit_layer_kwargs
              if logit_kwargs is None:
                  logit_kwargs = {}
              elif not isinstance(logit_kwargs, HParams):
                  raise ValueError(
                      "hparams['logit_layer_kwargs'] must be a dict."
                  )
              else:
                  logit_kwargs = logit_kwargs.todict()
              logit_kwargs.update({"units": self._num_classes})
              if "name" not in logit_kwargs:
                  logit_kwargs["name"] = "logit_layer"
  
              layer_hparams = {"type": "Dense", "kwargs": logit_kwargs}
              self._logit_layer = get_layer(hparams=layer_hparams)
  
file_path: texar/texar/tf/modules/classifiers/gpt2_classifier.py
imports: import tensorflow
  from texar.tf.core.layers import get_layer
  from texar.tf.modules.classifiers.classifier_base import ClassifierBase
  from texar.tf.modules.encoders.gpt2_encoder import GPT2Encoder
  from texar.tf.hyperparams import HParams
  from texar.tf.modules.pretrained.gpt2 import PretrainedGPT2Mixin
  from texar.tf.utils.utils import dict_fetch
label_desc: Use image node_id 1 to create a new GPT2Classifier object from inherited base classes: ClassifierBase, PretrainedGPT2Mixin with example: obj = GPT2Classifier(pretrained_model_name, cache_dir, hparams)