class_docstr: A trainer specifies how a network is going to be trained.
  
  A trainer is mainly defined by two sets of parameters. The first one
  determines the number of examples that the network will be trained on
  (`epochs`, `num_batches_per_epoch`), while the second one specifies how
  the gradient updates are performed (`learning_rate`, `clip_gradient` and
  `weight_decay`).
  
  Parameters
  ----------
  ctx
  epochs
      Number of epochs that the network will train (default: 100).
  num_batches_per_epoch
      Number of batches at each epoch (default: 50).
  learning_rate
      Initial learning rate (default: :math:`10^{-3}`).
  clip_gradient
      Maximum value of gradient. The gradient is clipped if it is too large
      (default: 10).
  weight_decay
      The weight decay (or L2 regularization) coefficient. Modifies objective
      by adding a penalty for having large weights (default :math:`10^{-8}`).
  init
      Initializer of the weights of the network (default: "xavier").
  hybridize
      If set to True the network will be hybridized before training
  callbacks
      A list of `gluonts.mx.trainer.callback.Callback` to control the
      training.
  add_default_callbacks
      bool, True by default. If `True`, LearningRateReduction and
      ModelAveragingCallbacks are used in addition to the callbacks specified
      in the callbacks argument. Make sure that you only set this to true if
      you don't specify one of the default callbacks yourself or there will
      be "duplicate callbacks". default callbacks:
      >>> callbacks = [
      ...     ModelAveraging(avg_strategy=SelectNBestMean(num_models=1)),
      ...     LearningRateReduction(
      ...         base_lr=1e-3, # learning_rate
      ...         decay_factor=0.5, # learning_rate_decay_factor
      ...         patience=10, # patience
      ...         min_lr=5e-5, # minimum_learning_rate
      ...         objective="min",
      ...     )
      ... ]
class_docstr_tok: ["A","trainer","specifies","how","a","network","is","going","to","be","trained",".","A","trainer","is","mainly","defined","by","two","sets","of","parameters",".","The","first","one","determines","the","number","of","examples","that","the","network","will","be","trained","on","(","`","epochs","`",",","`","num_batches_per_epoch","`",")",",","while","the","second","one","specifies","how","the","gradient","updates","are","performed","(","`","learning_rate","`",",","`","clip_gradient","`","and","`","weight_decay","`",")",".","Parameters","--","--","--","--","--","ctx","epochs","Number","of","epochs","that","the","network","will","train","(","default",":","100",")",".","num_batches_per_epoch","Number","of","batches","at","each","epoch","(","default",":","50",")",".","learning_rate","Initial","learning","rate","(","default",":",":","math",":","`","10^","{","-3","}","`",")",".","clip_gradient","Maximum","value","of","gradient",".","The","gradient","is","clipped","if","it","is","too","large","(","default",":","10",")",".","weight_decay","The","weight","decay","(","or","L2","regularization",")","coefficient",".","Modifies","objective","by","adding","a","penalty","for","having","large","weights","(","default",":","math",":","`","10^","{","-8","}","`",")",".","init","Initializer","of","the","weights","of","the","network","(","default",":","``","xavier","''",")",".","hybridize","If","set","to","True","the","network","will","be","hybridized","before","training","callbacks","A","list","of","`","gluonts.mx.trainer.callback.Callback","`","to","control","the","training",".","add_default_callbacks","bool",",","True","by","default",".","If","`","True","`",",","LearningRateReduction","and","ModelAveragingCallbacks","are","used","in","addition","to","the","callbacks","specified","in","the","callbacks","argument",".","Make","sure","that","you","only","set","this","to","true","if","you","do","n't","specify","one","of","the","default","callbacks","yourself","or","there","will","be","``","duplicate","callbacks","''",".","default","callbacks",":",">",">",">","callbacks","=","[","...","ModelAveraging","(","avg_strategy=SelectNBestMean","(","num_models=1",")",")",",","...","LearningRateReduction","(","...","base_lr=1e-3",",","#","learning_rate","...","decay_factor=0.5",",","#","learning_rate_decay_factor","...","patience=10",",","#","patience","...","min_lr=5e-5",",","#","minimum_learning_rate","...","objective=","''","min","''",",","...",")","...","]"]
code: def __init__(
      self,
      ctx: Optional[mx.Context] = None,
      epochs: int = 100,
      num_batches_per_epoch: int = 50,
      learning_rate: float = 1e-3,
      clip_gradient: float = 10.0,
      weight_decay: float = 1e-8,
      init: Union[str, mx.initializer.Initializer] = "xavier",
      hybridize: bool = True,
      callbacks: Optional[List[Callback]] = None,
      add_default_callbacks: bool = True,
  ) -> None:
      assert (
          0 <= epochs < float("inf")
      ), "The value of `epochs` should be >= 0"
      assert (
          0 < num_batches_per_epoch
      ), "The value of `num_batches_per_epoch` should be > 0"
      assert (
          0 < learning_rate < float("inf")
      ), "The value of `learning_rate` should be > 0"
  
      assert (
          0 < clip_gradient
      ), "The value of `clip_gradient` should be > 0"
      assert (
          0 <= weight_decay
      ), "The value of `weight_decay` should be => 0"
  
      self.epochs = epochs
      self.num_batches_per_epoch = num_batches_per_epoch
      self.learning_rate = learning_rate
      self.clip_gradient = clip_gradient
      self.weight_decay = weight_decay
      self.init = init
      self.hybridize = hybridize
      self.ctx = ctx if ctx is not None else get_mxnet_context()
      self.halt = False
  
      # Make sure callbacks is list -- they are assigned to `self.callbacks`
      # below
      callbacks = callbacks or []
  
      # TODO the following is done for backwards compatibility. For future
      # versions, add the default callbacks as default arg
      if add_default_callbacks:
          if not any(
              isinstance(callback, ModelAveraging)
              for callback in callbacks
          ):
              callbacks.append(
                  ModelAveraging(
                      avg_strategy=SelectNBestMean(num_models=1)
                  )
              )
  
          if not any(
              isinstance(callback, LearningRateReduction)
              for callback in callbacks
          ):
              callbacks.append(
                  LearningRateReduction(
                      base_lr=learning_rate,
                      patience=10,
                      objective="min",
                  )
              )
  
      self.callbacks = CallbackList(callbacks)
  
imports: import itertools
  import logging
  import os
  import tempfile
  import time
  import uuid
  import warnings
  from typing import cast, List, Optional, Union
  import mxnet
  import mxnet.autograd
  import mxnet.gluon.nn
  import numpy
  from mxnet.metric import ndarray
  from gluonts.core.component import validated
  from gluonts.dataset.loader import DataLoader
  from gluonts.exceptions import GluonTSDataError
  from gluonts.gluonts_tqdm import tqdm
  from gluonts.mx.context import get_mxnet_context
  from gluonts.mx.trainer.callback import Callback, CallbackList
  from gluonts.mx.util import HybridContext
  from .learning_rate_scheduler import LearningRateReduction
  from .model_averaging import SelectNBestMean, save_epoch_info, ModelAveraging
label_desc: Use image node_id 1 to create a new Trainer object  with example: obj = Trainer(ctx, epochs, num_batches_per_epoch, learning_rate, clip_gradient, weight_decay, init, hybridize, callbacks, add_default_callbacks)