docstr: Trains the module parameters.
  
  Checkout `Module Tutorial <http://mxnet.io/tutorials/basic/module.html>`_ to see
  a end-to-end use-case.
  
  Parameters
  ----------
  train_data : DataIter
      Train DataIter.
  eval_data : DataIter
      If not ``None``, will be used as validation set and the performance
      after each epoch will be evaluated.
  eval_metric : str or EvalMetric
      Defaults to 'accuracy'. The performance measure used to display during training.
      Other possible predefined metrics are:
      'ce' (CrossEntropy), 'f1', 'mae', 'mse', 'rmse', 'top_k_accuracy'.
  epoch_end_callback : function or list of functions
      Each callback will be called with the current `epoch`, `symbol`, `arg_params`
      and `aux_params`.
  batch_end_callback : function or list of function
      Each callback will be called with a `BatchEndParam`.
  kvstore : str or KVStore
      Defaults to 'local'.
  optimizer : str or Optimizer
      Defaults to 'sgd'.
  optimizer_params : dict
      Defaults to ``(('learning_rate', 0.01),)``. The parameters for
      the optimizer constructor.
      The default value is not a dict, just to avoid pylint warning on dangerous
      default values.
  eval_end_callback : function or list of function
      These will be called at the end of each full evaluation, with the metrics over
      the entire evaluation set.
  eval_batch_end_callback : function or list of function
      These will be called at the end of each mini-batch during evaluation.
  initializer : Initializer
      The initializer is called to initialize the module parameters when they are
      not already initialized.
  arg_params : dict
      Defaults to ``None``, if not ``None``, should be existing parameters from a trained
      model or loaded from a checkpoint (previously saved model). In this case,
      the value here will be used to initialize the module parameters, unless they
      are already initialized by the user via a call to `init_params` or `fit`.
      `arg_params` has a higher priority than `initializer`.
  aux_params : dict
      Defaults to ``None``. Similar to `arg_params`, except for auxiliary states.
  allow_missing : bool
      Defaults to ``False``. Indicates whether to allow missing parameters when `arg_params`
      and `aux_params` are not ``None``. If this is ``True``, then the missing parameters
      will be initialized via the `initializer`.
  force_rebind : bool
      Defaults to ``False``. Whether to force rebinding the executors if already bound.
  force_init : bool
      Defaults to ``False``. Indicates whether to force initialization even if the
      parameters are already initialized.
  begin_epoch : int
      Defaults to 0. Indicates the starting epoch. Usually, if resumed from a
      checkpoint saved at a previous training phase at epoch N, then this value should be
      N+1.
  num_epoch : int
      Number of epochs for training.
  sparse_row_id_fn : A callback function
      The function  takes `data_batch` as an input and returns a dict of
      str -> NDArray. The resulting dict is used for pulling row_sparse
      parameters from the kvstore, where the str key is the name of the param,
      and the value is the row id of the param to pull.
  
  Examples
  --------
  >>> # An example of using fit for training.
  >>> # Assume training dataIter and validation dataIter are ready
  >>> # Assume loading a previously checkpointed model
  >>> sym, arg_params, aux_params = mx.model.load_checkpoint(model_prefix, 3)
  >>> mod.fit(train_data=train_dataiter, eval_data=val_dataiter, optimizer='sgd',
  ...     optimizer_params={'learning_rate':0.01, 'momentum': 0.9},
  ...     arg_params=arg_params, aux_params=aux_params,
  ...     eval_metric='acc', num_epoch=10, begin_epoch=3)
docstr_tok: ["Trains","the","module","parameters",".","Checkout","`","Module","Tutorial","<","http",":","\/\/mxnet.io\/tutorials\/basic\/module.html",">","`","_","to","see","a","end-to-end","use-case",".","Parameters","--","--","--","--","--","train_data",":","DataIter","Train","DataIter",".","eval_data",":","DataIter","If","not","``","None","``",",","will","be","used","as","validation","set","and","the","performance","after","each","epoch","will","be","evaluated",".","eval_metric",":","str","or","EvalMetric","Defaults","to","'accuracy","'",".","The","performance","measure","used","to","display","during","training",".","Other","possible","predefined","metrics","are",":","'ce","'","(","CrossEntropy",")",",","'f1","'",",","'mae","'",",","'mse","'",",","'rmse","'",",","'top_k_accuracy","'",".","epoch_end_callback",":","function","or","list","of","functions","Each","callback","will","be","called","with","the","current","`","epoch","`",",","`","symbol","`",",","`","arg_params","`","and","`","aux_params","`",".","batch_end_callback",":","function","or","list","of","function","Each","callback","will","be","called","with","a","`","BatchEndParam","`",".","kvstore",":","str","or","KVStore","Defaults","to","'local","'",".","optimizer",":","str","or","Optimizer","Defaults","to","'sgd","'",".","optimizer_params",":","dict","Defaults","to","``","(","(","'learning_rate","'",",","0.01",")",",",")","``",".","The","parameters","for","the","optimizer","constructor",".","The","default","value","is","not","a","dict",",","just","to","avoid","pylint","warning","on","dangerous","default","values",".","eval_end_callback",":","function","or","list","of","function","These","will","be","called","at","the","end","of","each","full","evaluation",",","with","the","metrics","over","the","entire","evaluation","set",".","eval_batch_end_callback",":","function","or","list","of","function","These","will","be","called","at","the","end","of","each","mini-batch","during","evaluation",".","initializer",":","Initializer","The","initializer","is","called","to","initialize","the","module","parameters","when","they","are","not","already","initialized",".","arg_params",":","dict","Defaults","to","``","None","``",",","if","not","``","None","``",",","should","be","existing","parameters","from","a","trained","model","or","loaded","from","a","checkpoint","(","previously","saved","model",")",".","In","this","case",",","the","value","here","will","be","used","to","initialize","the","module","parameters",",","unless","they","are","already","initialized","by","the","user","via","a","call","to","`","init_params","`","or","`","fit","`",".","`","arg_params","`","has","a","higher","priority","than","`","initializer","`",".","aux_params",":","dict","Defaults","to","``","None","``",".","Similar","to","`","arg_params","`",",","except","for","auxiliary","states",".","allow_missing",":","bool","Defaults","to","``","False","``",".","Indicates","whether","to","allow","missing","parameters","when","`","arg_params","`","and","`","aux_params","`","are","not","``","None","``",".","If","this","is","``","True","``",",","then","the","missing","parameters","will","be","initialized","via","the","`","initializer","`",".","force_rebind",":","bool","Defaults","to","``","False","``",".","Whether","to","force","rebinding","the","executors","if","already","bound",".","force_init",":","bool","Defaults","to","``","False","``",".","Indicates","whether","to","force","initialization","even","if","the","parameters","are","already","initialized",".","begin_epoch",":","int","Defaults","to","0",".","Indicates","the","starting","epoch",".","Usually",",","if","resumed","from","a","checkpoint","saved","at","a","previous","training","phase","at","epoch","N",",","then","this","value","should","be","N+1",".","num_epoch",":","int","Number","of","epochs","for","training",".","sparse_row_id_fn",":","A","callback","function","The","function","takes","`","data_batch","`","as","an","input","and","returns","a","dict","of","str","-",">","NDArray",".","The","resulting","dict","is","used","for","pulling","row_sparse","parameters","from","the","kvstore",",","where","the","str","key","is","the","name","of","the","param",",","and","the","value","is","the","row","id","of","the","param","to","pull",".","Examples","--","--","--","--",">",">",">","#","An","example","of","using","fit","for","training",".",">",">",">","#","Assume","training","dataIter","and","validation","dataIter","are","ready",">",">",">","#","Assume","loading","a","previously","checkpointed","model",">",">",">","sym",",","arg_params",",","aux_params","=","mx.model.load_checkpoint","(","model_prefix",",","3",")",">",">",">","mod.fit","(","train_data=train_dataiter",",","eval_data=val_dataiter",",","optimizer='sgd","'",",","...","optimizer_params=","{","'learning_rate':0.01",",","'momentum","'",":","0.9","}",",","...","arg_params=arg_params",",","aux_params=aux_params",",","...","eval_metric='acc","'",",","num_epoch=10",",","begin_epoch=3",")"]
code: def fit(
      self,
      train_data,
      eval_data=None,
      eval_metric="acc",
      epoch_end_callback=None,
      batch_end_callback=None,
      kvstore="local",
      optimizer="sgd",
      optimizer_params=(("learning_rate", 0.01),),
      eval_end_callback=None,
      eval_batch_end_callback=None,
      initializer=Uniform(0.01),
      arg_params=None,
      aux_params=None,
      allow_missing=False,
      force_rebind=False,
      force_init=False,
      begin_epoch=0,
      num_epoch=None,
      validation_metric=None,
      monitor=None,
      sparse_row_id_fn=None,
  ):
      """Trains the module parameters.
  
      Checkout `Module Tutorial <http://mxnet.io/tutorials/basic/module.html>`_ to see
      a end-to-end use-case.
  
      Parameters
      ----------
      train_data : DataIter
          Train DataIter.
      eval_data : DataIter
          If not ``None``, will be used as validation set and the performance
          after each epoch will be evaluated.
      eval_metric : str or EvalMetric
          Defaults to 'accuracy'. The performance measure used to display during training.
          Other possible predefined metrics are:
          'ce' (CrossEntropy), 'f1', 'mae', 'mse', 'rmse', 'top_k_accuracy'.
      epoch_end_callback : function or list of functions
          Each callback will be called with the current `epoch`, `symbol`, `arg_params`
          and `aux_params`.
      batch_end_callback : function or list of function
          Each callback will be called with a `BatchEndParam`.
      kvstore : str or KVStore
          Defaults to 'local'.
      optimizer : str or Optimizer
          Defaults to 'sgd'.
      optimizer_params : dict
          Defaults to ``(('learning_rate', 0.01),)``. The parameters for
          the optimizer constructor.
          The default value is not a dict, just to avoid pylint warning on dangerous
          default values.
      eval_end_callback : function or list of function
          These will be called at the end of each full evaluation, with the metrics over
          the entire evaluation set.
      eval_batch_end_callback : function or list of function
          These will be called at the end of each mini-batch during evaluation.
      initializer : Initializer
          The initializer is called to initialize the module parameters when they are
          not already initialized.
      arg_params : dict
          Defaults to ``None``, if not ``None``, should be existing parameters from a trained
          model or loaded from a checkpoint (previously saved model). In this case,
          the value here will be used to initialize the module parameters, unless they
          are already initialized by the user via a call to `init_params` or `fit`.
          `arg_params` has a higher priority than `initializer`.
      aux_params : dict
          Defaults to ``None``. Similar to `arg_params`, except for auxiliary states.
      allow_missing : bool
          Defaults to ``False``. Indicates whether to allow missing parameters when `arg_params`
          and `aux_params` are not ``None``. If this is ``True``, then the missing parameters
          will be initialized via the `initializer`.
      force_rebind : bool
          Defaults to ``False``. Whether to force rebinding the executors if already bound.
      force_init : bool
          Defaults to ``False``. Indicates whether to force initialization even if the
          parameters are already initialized.
      begin_epoch : int
          Defaults to 0. Indicates the starting epoch. Usually, if resumed from a
          checkpoint saved at a previous training phase at epoch N, then this value should be
          N+1.
      num_epoch : int
          Number of epochs for training.
      sparse_row_id_fn : A callback function
          The function  takes `data_batch` as an input and returns a dict of
          str -> NDArray. The resulting dict is used for pulling row_sparse
          parameters from the kvstore, where the str key is the name of the param,
          and the value is the row id of the param to pull.
  
      Examples
      --------
      >>> # An example of using fit for training.
      >>> # Assume training dataIter and validation dataIter are ready
      >>> # Assume loading a previously checkpointed model
      >>> sym, arg_params, aux_params = mx.model.load_checkpoint(model_prefix, 3)
      >>> mod.fit(train_data=train_dataiter, eval_data=val_dataiter, optimizer='sgd',
      ...     optimizer_params={'learning_rate':0.01, 'momentum': 0.9},
      ...     arg_params=arg_params, aux_params=aux_params,
      ...     eval_metric='acc', num_epoch=10, begin_epoch=3)
      """
      assert num_epoch is not None, "please specify number of epochs"
      assert arg_params is None and aux_params is None
  
      self.bind(
          data_shapes=train_data.provide_data,
          label_shapes=train_data.provide_label,
          for_training=True,
          force_rebind=force_rebind,
      )
      if monitor is not None:
          self.install_monitor(monitor)
      self.init_params(
          initializer=initializer,
          arg_params=arg_params,
          aux_params=aux_params,
          allow_missing=allow_missing,
          force_init=force_init,
      )
      self.init_optimizer(
          kvstore=kvstore,
          optimizer=optimizer,
          optimizer_params=optimizer_params,
      )
  
      if validation_metric is None:
          validation_metric = eval_metric
      if not isinstance(eval_metric, metric.EvalMetric):
          eval_metric = metric.create(eval_metric)
      epoch_eval_metric = copy.deepcopy(eval_metric)
  
      ################################################################################
      # training loop
      ################################################################################
      for epoch in range(begin_epoch, num_epoch):
          tic = time.time()
          eval_metric.reset()
          epoch_eval_metric.reset()
          nbatch = 0
          data_iter = iter(train_data)
          end_of_batch = False
          next_data_batch = next(data_iter)
          while not end_of_batch:
              data_batch = next_data_batch
              if monitor is not None:
                  monitor.tic()
              self.forward_backward(data_batch)
              self.update()
              assert not isinstance(data_batch, list)
  
              # if isinstance(data_batch, list):
              #    #print('XXX')
              #    self.update_metric(eval_metric,
              #                       [db.label for db in data_batch],
              #                       pre_sliced=True)
              #    self.update_metric(epoch_eval_metric,
              #                       [db.label for db in data_batch],
              #                       pre_sliced=True)
              # else:
              #    #print('before update metric')
              #    self.update_metric(eval_metric, data_batch.label)
              #    self.update_metric(epoch_eval_metric, data_batch.label)
              # labels = data_batch.label
              # labels = [self.global_label]
              # self.update_metric(eval_metric, labels)
              # self.update_metric(epoch_eval_metric, labels)
  
              try:
                  # pre fetch next batch
                  next_data_batch = next(data_iter)
                  self.prepare(
                      next_data_batch, sparse_row_id_fn=sparse_row_id_fn
                  )
              except StopIteration:
                  end_of_batch = True
  
              if monitor is not None:
                  monitor.toc_print()
  
              # if end_of_batch:
              #    eval_name_vals = epoch_eval_metric.get_name_value()
  
              if batch_end_callback is not None:
                  batch_end_params = BatchEndParam(
                      epoch=epoch,
                      nbatch=nbatch,
                      eval_metric=None,
                      locals=locals(),
                  )
                  batch_end_callback(batch_end_params)
                  # for callback in _as_list(batch_end_callback):
                  #    callback(batch_end_params)
              nbatch += 1
  
          # one epoch of training is finished
          # for name, val in eval_name_vals:
          #    self.logger.info('Epoch[%d] Train-%s=%f', epoch, name, val)
          toc = time.time()
          self.logger.info(
              "Epoch[%d] Time cost=%.3f", epoch, (toc - tic)
          )
  
          # sync aux params across devices
          arg_params, aux_params = self.get_params()
          self.set_params(arg_params, aux_params)
  
          # end of 1 epoch, reset the data-iter for another epoch
          train_data.reset()
  
imports: import logging
  import copy
  import time
  import os
  import mxnet
  import numpy
  from mxnet import context
  from mxnet.initializer import Uniform
  from mxnet.module.base_module import BaseModule
  from mxnet.module.module import Module
  from mxnet import metric
  from mxnet.model import BatchEndParam
  from mxnet import io
  import mxnet.ndarray
  from config import config
label_desc: Use image node_id 25 for calling the ParallModule obj's underlying member method code with example usage: obj.fit(train_data, eval_data, eval_metric, epoch_end_callback, batch_end_callback, kvstore, optimizer, optimizer_params, eval_end_callback, eval_batch_end_callback, initializer, arg_params, aux_params, allow_missing, force_rebind, force_init, begin_epoch, num_epoch, validation_metric, monitor, sparse_row_id_fn) without return types