class_docstr: Agent that uses a generator model with a classifier head.
  
  Useful for performing classification with a pretrained generator model. The
  generator encoder/decoder weights can be frozen during classifier training.
class_docstr_tok: ["Agent","that","uses","a","generator","model","with","a","classifier","head",".","Useful","for","performing","classification","with","a","pretrained","generator","model",".","The","generator","encoder\/decoder","weights","can","be","frozen","during","classifier","training","."]
docstr: Set up model.
docstr_tok: ["Set","up","model","."]
code: def __init__(self, opt, shared=None):
      """
      Set up model.
      """
  
      # set up classes
      if (
          opt.get("classes") is None
          and opt.get("classes_from_file") is None
      ):
          raise RuntimeError(
              "Must specify --classes or --classes-from-file argument."
          )
      if not shared:
          if (
              opt["classes_from_file"]
              == "image_chat_personalities_file"
          ):
              # Download the file of Image-Chat personalities and use it as the list
              # of classes
              opt["classes_from_file"] = get_personality_list_path(opt)
          if opt["classes_from_file"] is not None:
              with open(opt["classes_from_file"]) as f:
                  self.class_list = f.read().splitlines()
          else:
              self.class_list = opt["classes"]
          self.class_dict = {
              val: i for i, val in enumerate(self.class_list)
          }
          if opt.get("class_weights", None) is not None:
              self.class_weights = opt["class_weights"]
          else:
              self.class_weights = [1.0 for _ in self.class_list]
      else:
          self.class_list = shared["class_list"]
          self.class_dict = shared["class_dict"]
          self.class_weights = shared["class_weights"]
  
      super().__init__(opt, shared)
  
      # Override the criterion
      if not shared:
          self.criterion = self.build_criterion()
          if self.use_cuda:
              self.criterion.cuda()
  
      # Freeze generator encoder/decoder weights
      if opt["freeze_enc_dec_weights"]:
          for param in chain(
              self.model.encoder.parameters(),
              self.model.decoder.parameters(),
          ):
              param.requires_grad = False
  
imports: from typing import Optional
  from parlai.core.params import ParlaiParser
  from parlai.core.opt import Opt
  from itertools import chain
  import torch
  from torch.nn import functional
  from parlai.agents.transformer.transformer import TransformerClassifierAgent, TransformerGeneratorAgent
  from parlai.core.metrics import AverageMetric
  from parlai.core.torch_agent import Output
  from parlai.tasks.style_gen.agents import get_personality_list_path
  from parlai.utils.fp16 import FP16SafeCrossEntropy
  from parlai.utils.misc import warn_once, round_sigfigs
  from projects.style_gen.modules import ClassifierOnGeneratorModel, ClassificationMixin
label_desc: Use image node_id 2 to create a new ClassifierAgent object from inherited base classes: ClassificationMixin, TransformerGeneratorAgent with example: obj = ClassifierAgent(opt, shared)