class_docstr: Simple implementation of the starspace algorithm: https://arxiv.org/abs/1709.03856.
docstr: Set up model if shared params not set, otherwise no work to do.
docstr_tok: ["Set","up","model","if","shared","params","not","set",",","otherwise","no","work","to","do","."]
code: def __init__(self, opt, shared=None):
      """
      Set up model if shared params not set, otherwise no work to do.
      """
      super().__init__(opt, shared)
      opt = self.opt
      self.reset_metrics()
      self.id = "Starspace"
      self.NULL_IDX = 0
      self.cands = torch.LongTensor(1, 1, 1)
      self.ys_cache = []
      self.ys_cache_sz = opt["cache_size"]
      self.truncate = opt["truncate"] if opt["truncate"] > 0 else None
      self.history = {}
      if shared:
          # set up shared properties
          self.dict = shared["dict"]
          self.model = shared["model"]
      else:
          logging.info("creating StarspaceAgent")
          # this is not a shared instance of this class, so do full init
          if opt.get("model_file") and (
              PathManager.exists(opt.get("model_file") + ".dict")
              or (opt["dict_file"] is None)
          ):
              # set default dict-file if not set
              opt["dict_file"] = opt["model_file"] + ".dict"
          # load dictionary and basic tokens & vectors
          self.dict = DictionaryAgent(opt)
  
          self.model = Starspace(opt, len(self.dict), self.dict)
          if opt.get("model_file") and PathManager.exists(
              opt["model_file"]
          ):
              self.load(opt["model_file"])
          else:
              self._init_embeddings()
          self.model.share_memory()
  
      # set up modules
      self.criterion = torch.nn.CosineEmbeddingLoss(
          margin=opt["margin"], size_average=False
      )
      self.reset()
      self.fixedCands = False
      self.fixedX = None
      if self.opt.get("fixed_candidates_file"):
          self.fixedCands_txt = load_cands(
              self.opt.get("fixed_candidates_file")
          )
          fcs = []
          for c in self.fixedCands_txt:
              fcs.append(torch.LongTensor(self.parse(c)).unsqueeze(0))
          self.fixedCands = fcs
          logging.info("loaded candidates")
  
imports: from typing import Optional
  from parlai.core.params import ParlaiParser
  from parlai.core.opt import Opt
  from parlai.core.agents import Agent
  from parlai.core.dict import DictionaryAgent
  from parlai.utils.misc import maintain_dialog_history, load_cands
  from parlai.core.torch_agent import TorchAgent
  from parlai.utils.io import PathManager
  import parlai.utils.torch
  import parlai.utils.logging
  from .modules import Starspace
  import torch
  from torch import optim
  import torch.nn
  from collections import deque
  import copy
  import random
  import json
label_desc: Use image node_id 3 to create a new StarspaceAgent object from inherited base classes: Agent with example: obj = StarspaceAgent(opt, shared)