class_docstr: Manages a PyTorch model for training.
class_docstr_tok: ["Manages","a","PyTorch","model","for","training","."]
docstr: Runs one standard training pass over the training dataloader.
  
  By default, this method will iterate over the given iterator and
  call ``self.train_batch`` over each batch.
  
  You do not need to call ``train_batch`` in this method if you plan
  to implement a custom optimization/training routine here.
  
  You may find ``ray.util.sgd.utils.AverageMeterCollection`` useful
  when overriding this method. See example below:
  
  .. code-block:: python
  
      def train_epoch(self, ...):
          meter_collection = AverageMeterCollection()
          self.model.train()
          for batch in iterator:
              # do some processing
              metrics = {"metric_1": 1, "metric_2": 3} # dict of metrics
  
              # This keeps track of all metrics across multiple batches
              meter_collection.update(metrics, n=len(batch))
  
          # Returns stats of the meters.
          stats = meter_collection.summary()
          return stats
  
  
  Args:
      iterator (iter): Iterator over the training data for the entire
          epoch. This iterator is expected to be entirely consumed.
  
  Returns:
      A dict of metrics from training.
docstr_tok: ["Runs","one","standard","training","pass","over","the","training","dataloader",".","By","default",",","this","method","will","iterate","over","the","given","iterator","and","call","``","self.train_batch","``","over","each","batch",".","You","do","not","need","to","call","``","train_batch","``","in","this","method","if","you","plan","to","implement","a","custom","optimization\/training","routine","here",".","You","may","find","``","ray.util.sgd.utils.AverageMeterCollection","``","useful","when","overriding","this","method",".","See","example","below",":","..","code-block",":",":","python","def","train_epoch","(","self",",","...",")",":","meter_collection","=","AverageMeterCollection","(",")","self.model.train","(",")","for","batch","in","iterator",":","#","do","some","processing","metrics","=","{","``","metric_1","''",":","1",",","``","metric_2","''",":","3","}","#","dict","of","metrics","#","This","keeps","track","of","all","metrics","across","multiple","batches","meter_collection.update","(","metrics",",","n=len","(","batch",")",")","#","Returns","stats","of","the","meters",".","stats","=","meter_collection.summary","(",")","return","stats","Args",":","iterator","(","iter",")",":","Iterator","over","the","training","data","for","the","entire","epoch",".","This","iterator","is","expected","to","be","entirely","consumed",".","Returns",":","A","dict","of","metrics","from","training","."]
code: def _train_epoch(self, iterator, callbacks=None):
      """Runs one standard training pass over the training dataloader.
  
      By default, this method will iterate over the given iterator and
      call ``self.train_batch`` over each batch.
  
      You do not need to call ``train_batch`` in this method if you plan
      to implement a custom optimization/training routine here.
  
      You may find ``ray.util.sgd.utils.AverageMeterCollection`` useful
      when overriding this method. See example below:
  
      .. code-block:: python
  
          def train_epoch(self, ...):
              meter_collection = AverageMeterCollection()
              self.model.train()
              for batch in iterator:
                  # do some processing
                  metrics = {"metric_1": 1, "metric_2": 3} # dict of metrics
  
                  # This keeps track of all metrics across multiple batches
                  meter_collection.update(metrics, n=len(batch))
  
              # Returns stats of the meters.
              stats = meter_collection.summary()
              return stats
  
  
      Args:
          iterator (iter): Iterator over the training data for the entire
              epoch. This iterator is expected to be entirely consumed.
  
      Returns:
          A dict of metrics from training.
      """
      self._mode = "train"
  
      metric_meters = AverageMeterCollection()
  
      # TODO: Discuss the situation when there are multiple components,
      #       It is best for the user to write this part of the logic in a hook func.
      self.model.train()
      # self.training_models may not be DDP if horovod.
      from torch.nn.parallel import DistributedDataParallel as DDP
  
      if isinstance(self.model, DDP):
          with self.model.join():
              self._train_loop(iterator, metric_meters, callbacks)
      else:
          self._train_loop(iterator, metric_meters, callbacks)
  
      self.call_hook(callbacks=callbacks, fn_name="on_lr_adjust")
  
      return metric_meters.summary(
          sync_stats=self.sync_stats, dist_backend=self.dist_backend
      )
  
imports: from filelock import FileLock
  import logging
  import os
  import copy
  import tempfile
  import math
  import torch
  import torch.nn
  import numpy
  from torch.utils.data import DataLoader, IterableDataset
  from bigdl.orca.learn.metrics import Metric
  from bigdl.orca import OrcaContext
  from bigdl.orca.learn.pytorch import utils
  from bigdl.orca.learn.pytorch.utils import AverageMeterCollection, NUM_SAMPLES, get_batchsize, index_concatenate, split_predict_cols
  from bigdl.orca.learn.pytorch.core import BaseRunner
  from bigdl.dllib.utils.log4Error import invalidInputError
label_desc: Use image node_id 9 for calling the TorchRunner obj's underlying member method code with example usage: obj._train_epoch(iterator, callbacks) and returns: metric_meters