class_docstr: A trainer for training models. 
class_docstr_tok: ["A","trainer","for","training","models","."]
code: def update(self, batch, eval=False):
      device = next(self.model.parameters()).device
      (
          inputs,
          orig_idx,
          word_orig_idx,
          sentlens,
          wordlens,
          text,
      ) = unpack_batch(batch, device)
      (
          word,
          word_mask,
          wordchars,
          wordchars_mask,
          upos,
          xpos,
          ufeats,
          pretrained,
          lemma,
          head,
          deprel,
      ) = inputs
  
      if eval:
          self.model.eval()
      else:
          self.model.train()
          self.optimizer.zero_grad()
      loss, _ = self.model(
          word,
          word_mask,
          wordchars,
          wordchars_mask,
          upos,
          xpos,
          ufeats,
          pretrained,
          lemma,
          head,
          deprel,
          word_orig_idx,
          sentlens,
          wordlens,
          text,
      )
      loss_val = loss.data.item()
      if eval:
          return loss_val
  
      loss.backward()
      torch.nn.utils.clip_grad_norm_(
          self.model.parameters(), self.args["max_grad_norm"]
      )
      self.optimizer.step()
      return loss_val
  
file_path: stanza/stanza/models/depparse/trainer.py
imports: import sys
  import logging
  import torch
  from torch import nn
  from stanza.models.common.trainer import Trainer
  from stanza.models.common import utils, loss
  from stanza.models.common.foundation_cache import NoTransformerFoundationCache
  from stanza.models.common.chuliu_edmonds import chuliu_edmonds_one_root
  from stanza.models.depparse.model import Parser
  from stanza.models.pos.vocab import MultiVocab
label_desc: Use image node_id 2 for calling the Trainer obj's underlying member method code with example usage: obj.update(batch, eval) and returns: loss_val, loss_val