class_docstr: An updater for a pytorch LM.
class_docstr_tok: ["An","updater","for","a","pytorch","LM","."]
docstr: Update the model.
docstr_tok: ["Update","the","model","."]
code: def update_core(self):
      """Update the model."""
      # When we pass one iterator and optimizer to StandardUpdater.__init__,
      # they are automatically named 'main'.
      train_iter = self.get_iterator("main")
      optimizer = self.get_optimizer("main")
      # Progress the dataset iterator for sentences at each iteration.
      self.model.zero_grad()  # Clear the parameter gradients
      accum = {"loss": 0.0, "nll": 0.0, "count": 0}
      for _ in range(self.accum_grad):
          batch = train_iter.__next__()
          # Concatenate the token IDs to matrices and send them to the device
          # self.converter does this job
          # (it is chainer.dataset.concat_examples by default)
          x, t = concat_examples(
              batch, device=self.device[0], padding=(0, -100)
          )
          if self.device[0] == -1:
              loss, nll, count = self.model(x, t)
          else:
              # apex does not support torch.nn.DataParallel
              loss, nll, count = data_parallel(
                  self.model, (x, t), self.device
              )
  
          # backward
          loss = loss.mean() / self.accum_grad
          if self.use_apex:
              from apex import amp
  
              with amp.scale_loss(loss, optimizer) as scaled_loss:
                  scaled_loss.backward()
          else:
              loss.backward()  # Backprop
          # accumulate stats
          accum["loss"] += float(loss)
          accum["nll"] += float(nll.sum())
          accum["count"] += int(count.sum())
  
      for k, v in accum.items():
          reporter.report({k: v}, optimizer.target)
      if self.gradclip is not None:
          nn.utils.clip_grad_norm_(
              self.model.parameters(), self.gradclip
          )
      optimizer.step()  # Update the parameters
      self.scheduler.step(n_iter=self.iteration)
  
imports: import copy
  import json
  import logging
  import numpy
  import torch
  import torch.nn
  from chainer import Chain, reporter, training
  from chainer.dataset import convert
  from chainer.training import extensions
  from torch.nn.parallel import data_parallel
  from espnet.asr.asr_utils import snapshot_object, torch_load, torch_resume, torch_snapshot
  from espnet.lm.lm_utils import MakeSymlinkToBestModel, ParallelSentenceIterator, count_tokens, load_dataset, read_tokens
  from espnet.nets.lm_interface import LMInterface, dynamic_import_lm
  from espnet.optimizer.factory import dynamic_import_optimizer
  from espnet.scheduler.pytorch import PyTorchScheduler
  from espnet.scheduler.scheduler import dynamic_import_scheduler
  from espnet.utils.deterministic_utils import set_deterministic_pytorch
  from espnet.utils.training.evaluator import BaseEvaluator
  from espnet.utils.training.iterators import ShufflingEnabler
  from espnet.utils.training.tensorboard_logger import TensorboardLogger
  from espnet.utils.training.train_utils import check_early_stop, set_early_stop
label_desc: Use image node_id 2 for calling the BPTTUpdater obj's underlying member method code with example usage: obj.update_core() without return types