class_docstr: Manages a PyTorch model for training.
class_docstr_tok: ["Manages","a","PyTorch","model","for","training","."]
code: def train_epochs(
      self,
      data_creator,
      epochs=1,
      batch_size=32,
      profile=False,
      wrap_dataloader=None,
      callbacks=None,
      validation_data_creator=None,
  ):
      config = copy.copy(self.config)
      if OrcaContext.serialize_data_creator:
          with FileLock(
              os.path.join(tempfile.gettempdir(), ".orcadata.lock")
          ):
              self.train_loader = data_creator(config, batch_size)
      else:
          self.train_loader = data_creator(config, batch_size)
  
      if wrap_dataloader is None:
          if TorchRunner.should_wrap_dataloader(self.train_loader):
              self.train_loader = self.with_sampler(self.train_loader)
      elif wrap_dataloader is True:
          self.train_loader = self.with_sampler(self.train_loader)
  
      if validation_data_creator:
          if OrcaContext.serialize_data_creator:
              with FileLock(
                  os.path.join(
                      tempfile.gettempdir(), ".orca_val_data.lock"
                  )
              ):
                  val_loader = validation_data_creator(
                      config, batch_size
                  )
          else:
              val_loader = validation_data_creator(config, batch_size)
  
          wrapped = False
          if wrap_dataloader is None:
              if TorchRunner.should_wrap_dataloader(val_loader):
                  val_loader = self.with_sampler(val_loader)
                  wrapped = True
          elif wrap_dataloader is True:
              val_loader = self.with_sampler(val_loader)
              wrapped = True
  
          if not wrapped:
              # Truncate validation by the min step for all workers (data may distribute unevenly)
              # Or it results in error in next epoch of training (op.preamble.length <= op.nbytes)
              validation_tensor = torch.tensor(len(val_loader))
              invalidInputError(
                  self.backend != "horovod", "Sanity check failed!"
              )
              self.dist_backend.all_reduce_min(validation_tensor)
              val_steps = validation_tensor.item()
          else:
              val_steps = None
  
      else:
          val_loader = None
          val_steps = None
  
      self.val_loader = val_loader
      self.num_epochs = epochs
      self.call_hook(callbacks=callbacks, fn_name="before_run")
  
      stats_list = list()
      for i in range(self.num_epochs):
          del self.epoch_stats
          self.call_hook(
              callbacks=callbacks, fn_name="before_train_epoch"
          )
          stats = self.train_epoch(
              self.train_loader,
              profile=profile,
              callbacks=callbacks,
              val_loader=val_loader,
              val_steps=val_steps,
          )
          self.epoch_stats = stats
          self.call_hook(
              callbacks=callbacks, fn_name="after_train_epoch"
          )
  
          if self.rank == 0:
              if self.sync_stats:
                  self.logger.info(
                      f"Finished training epoch {i + 1}, "
                      + f"stats averaged over workers: {stats}"
                  )
              else:
                  self.logger.info(
                      f"Finished training epoch {i + 1}, "
                      + f"stats on rank 0: {stats}"
                  )
          stats_list.append(stats)
  
      self.call_hook(callbacks=callbacks, fn_name="after_run")
  
      return stats_list
  
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 7 for calling the TorchRunner obj's underlying member method code with example usage: obj.train_epochs(data_creator, epochs, batch_size, profile, wrap_dataloader, callbacks, validation_data_creator) and returns: stats_list