code: def train(opt, device):
      if isinstance(device, mx.Device):
          device = [device]
  
      train_data, val_data = get_data_iters(dataset, batch_size, opt)
      for p in net.collect_params().values():
          p.reset_device(device)
      trainer = gluon.Trainer(
          net.collect_params(),
          "sgd",
          optimizer_params={
              "learning_rate": opt.lr,
              "wd": opt.wd,
              "momentum": opt.momentum,
              "multi_precision": True,
          },
          kvstore=kv,
      )
      loss = gluon.loss.SoftmaxCrossEntropyLoss()
  
      total_time = 0
      num_epochs = 0
      best_acc = [0]
      for epoch in range(opt.start_epoch, opt.epochs):
          trainer = update_learning_rate(
              opt.lr, trainer, epoch, opt.lr_factor, lr_steps
          )
          tic = time.time()
          train_data.reset()
          metric.reset()
          btic = time.time()
          for i, batch in enumerate(train_data):
              data = gluon.utils.split_and_load(
                  batch.data[0].astype(opt.dtype),
                  device_list=device,
                  batch_axis=0,
              )
              label = gluon.utils.split_and_load(
                  batch.label[0].astype(opt.dtype),
                  device_list=device,
                  batch_axis=0,
              )
              outputs = []
              Ls = []
              with ag.record():
                  for x, y in zip(data, label):
                      z = net(x)
                      L = loss(z, y)
                      # store the loss and do backward after we have done forward
                      # on all GPUs for better speed on multiple GPUs.
                      Ls.append(L)
                      outputs.append(z)
                  ag.backward(Ls)
              trainer.step(batch.data[0].shape[0])
              metric.update(label, outputs)
              if opt.log_interval and not (i + 1) % opt.log_interval:
                  name, acc = metric.get()
                  logger.info(
                      "Epoch[%d] Batch [%d]\tSpeed: %f samples/sec\t%s=%f, %s=%f"
                      % (
                          epoch,
                          i,
                          batch_size / (time.time() - btic),
                          name[0],
                          acc[0],
                          name[1],
                          acc[1],
                      )
                  )
              btic = time.time()
  
          epoch_time = time.time() - tic
  
          # First epoch will usually be much slower than the subsequent epics,
          # so don't factor into the average
          if num_epochs > 0:
              total_time = total_time + epoch_time
          num_epochs = num_epochs + 1
  
          name, acc = metric.get()
          logger.info(
              "[Epoch %d] training: %s=%f, %s=%f"
              % (epoch, name[0], acc[0], name[1], acc[1])
          )
          logger.info("[Epoch %d] time cost: %f" % (epoch, epoch_time))
          name, val_acc = test(device, val_data)
          logger.info(
              "[Epoch %d] validation: %s=%f, %s=%f"
              % (epoch, name[0], val_acc[0], name[1], val_acc[1])
          )
  
          # save model if meet requirements
          save_checkpoint(epoch, val_acc[0], best_acc)
      if num_epochs > 1:
          print(
              "Average epoch time: {}".format(
                  float(total_time) / (num_epochs - 1)
              )
          )
  
file_path: mxnet/example/gluon/image_classification.py
imports: from __future__ import division
  import argparse, time, os
  import logging
  import mxnet
  from mxnet import gluon
  from mxnet import profiler
  from mxnet.gluon import nn
  from mxnet.gluon.model_zoo import vision
  from mxnet import autograd
  from mxnet.test_utils import get_mnist_iterator
  from mxnet.gluon.metric import Accuracy, TopKAccuracy, CompositeEvalMetric
  import numpy
  from data import get_cifar10_iterator, get_imagenet_iterator, get_caltech101_iterator, dummy_iterator
label_desc: Use image node_id 6 for calling a global function with example usage: train(opt, device) without return types