class_docstr: Manages a MXNet model for training.
class_docstr_tok: ["Manages","a","MXNet","model","for","training","."]
docstr: Train the model and update the model parameters.
docstr_tok: ["Train","the","model","and","update","the","model","parameters","."]
code: def train(
      self,
      train_data,
      epochs=1,
      batch_size=32,
      validation_data=None,
      train_resize_batch_num=None,
  ):
      """Train the model and update the model parameters."""
      stats = dict()
      if self.is_worker:
          config = copy.copy(self.config)
          if "batch_size" not in config:
              config["batch_size"] = batch_size
  
          if train_resize_batch_num is not None:
              config["train_resize_batch_num"] = train_resize_batch_num
          train_data_iter = train_data(config, self.kv)
          val_data_iter = (
              validation_data(config, self.kv)
              if validation_data
              else None
          )
  
          start_time = time.time()
          if self.trainer:  # Imperative API
  
              def cpu_context(target_data):
                  if isinstance(target_data, list):
                      return [cpu_context(d) for d in target_data]
                  else:
                      return target_data.as_in_context(mx.cpu())
  
              for epoch in range(epochs):
                  # DataLoader doesn't need to be reset.
                  if isinstance(train_data_iter, mx.io.DataIter):
                      train_data_iter.reset()
                  if self.eval_metrics:
                      self.eval_metrics.reset()  # metrics will accumulate for one batch.
                  batch_start_time = time.time()
                  epoch_start_time = time.time()
                  for i, batch in enumerate(train_data_iter):
                      data = cpu_context(batch.data)
                      label = cpu_context(batch.label)
                      if not isinstance(data, list):
                          data = [data]
                      if not isinstance(label, list):
                          label = [label]
                      from mxnet import autograd as ag
  
                      with ag.record():
                          output = self.model(*data)  # forward
                          if not isinstance(output, list):
                              output = [output]
                          Ls = self.loss(*output, *label)
                          ag.backward(Ls)
                      self.trainer.step(batch_size)
                      if self.eval_metrics:
                          self.eval_metrics.update(label, output)
                      if not (i + 1) % self.config["log_interval"]:
                          # This would be logged on driver for each worker process.
                          iteration_log = (
                              "Epoch[%d] Batch[%d]  Speed: %f samples/sec  %s=%f"
                              % (
                                  epoch,
                                  i,
                                  batch_size
                                  / (time.time() - batch_start_time),
                                  "loss",
                                  Ls.asnumpy().mean(),
                              )
                          )
                          if self.eval_metrics:
                              names, accs = self.eval_metrics.get()
                              names, accs = to_list(names), to_list(
                                  accs
                              )
                              for name, acc in zip(names, accs):
                                  iteration_log += "  %s=%f" % (
                                      name,
                                      acc,
                                  )
                          self.logger.info(iteration_log)
                      batch_start_time = time.time()
                  # Epoch time log.
                  self.logger.info(
                      "[Epoch %d] time cost: %f"
                      % (epoch, time.time() - epoch_start_time)
                  )
                  # Epoch metrics log on train data.
                  if self.eval_metrics:
                      epoch_train_log = "[Epoch %d] training: " % epoch
                      names, accs = self.eval_metrics.get()
                      names, accs = to_list(names), to_list(accs)
                      for name, acc in zip(names, accs):
                          epoch_train_log += "%s=%f  " % (name, acc)
                      self.logger.info(epoch_train_log)
                  # Epoch metrics log on validation data if any.
                  if val_data_iter:
                      if isinstance(val_data_iter, mx.io.DataIter):
                          val_data_iter.reset()
                      self.val_metrics.reset()
                      for batch in val_data_iter:
                          data = cpu_context(batch.data)
                          label = cpu_context(batch.label)
                          if not isinstance(data, list):
                              data = [data]
                          if not isinstance(label, list):
                              label = [label]
                          output = self.model(*data)
                          if not isinstance(output, list):
                              output = [output]
                          self.val_metrics.update(label, output)
                      epoch_val_log = "[Epoch %d] validation: " % epoch
                      names, accs = self.val_metrics.get()
                      names, accs = to_list(names), to_list(accs)
                      for name, acc in zip(names, accs):
                          epoch_val_log += "%s=%f  " % (name, acc)
                      self.logger.info(epoch_val_log)
                  # TODO: save checkpoints
              if self.eval_metrics:
                  names, accs = self.eval_metrics.get()
                  names, accs = to_list(names), to_list(accs)
                  for name, acc in zip(names, accs):
                      stats[name] = acc
          else:  # Symbolic API
              # TODO: seems no history (i.e. validation accuracy) returned by fit?
              if "init" not in self.config:
                  from mxnet.initializer import Uniform
  
                  self.config["init"] = Uniform(
                      0.01
                  )  # This is the default value for MXNet.
              if self.eval_metrics is None:
                  self.eval_metrics = (
                      "acc"  # This is the default value for MXNet.
                  )
              self.model.fit(
                  train_data=train_data_iter,
                  num_epoch=epochs,
                  initializer=self.config["init"],
                  kvstore=self.kv,
                  optimizer=self.config["optimizer"],
                  optimizer_params=self.config["optimizer_params"],
                  eval_data=val_data_iter,
                  eval_metric=self.eval_metrics,
                  validation_metric=self.val_metrics,
                  batch_end_callback=mx.callback.Speedometer(
                      batch_size, self.config["log_interval"]
                  ),
                  epoch_end_callback=None
                  if "model" not in self.config
                  else mx.callback.do_checkpoint(self.config["model"]),
              )
          epoch_time = time.time() - start_time
          stats["epoch_time"] = epoch_time
      return [stats]
  
file_path: BigDL/python/orca/src/bigdl/orca/learn/mxnet/mxnet_runner.py
imports: import os
  import time
  import logging
  import subprocess
  import ray._private.services
  import mxnet
  from mxnet import gluon
  import copy
  from bigdl.orca.ray.utils import to_list
  from bigdl.dllib.utils.log4Error import *
label_desc: Use image node_id 2 for calling the MXNetRunner obj's underlying member method code with example usage: obj.train(train_data, epochs, batch_size, validation_data, train_resize_batch_num) and returns: list, unknown, target_data