code: def train(epochs, ctx):
      # Collect all parameters from net and its children, then initialize them.
      net.initialize(mx.init.Xavier(magnitude=2.24), ctx=ctx)
      # Trainer is for updating parameters with gradient.
      trainer = gluon.Trainer(
          net.collect_params(),
          "sgd",
          {"learning_rate": opt.lr, "momentum": opt.momentum},
      )
      metric = mx.gluon.metric.Accuracy()
      loss = gluon.loss.SoftmaxCrossEntropyLoss()
  
      for epoch in range(epochs):
          # reset data iterator and metric at begining of epoch.
          metric.reset()
          for i, (data, label) in enumerate(train_data):
              # Copy data to ctx if necessary
              data = data.to_device(ctx)
              label = label.to_device(ctx)
              # Start recording computation graph with record() section.
              # Recorded graphs can then be differentiated with backward.
              with autograd.record():
                  output = net(data)
                  L = loss(output, label)
                  L.backward()
              # take a gradient step with batch_size equal to data.shape[0]
              trainer.step(data.shape[0])
              # update metric at last.
              metric.update([label], [output])
  
              if i % opt.log_interval == 0 and i > 0:
                  name, acc = metric.get()
                  print(
                      f"[Epoch {epoch} Batch {i}] Training: {name}={acc}"
                  )
  
          name, acc = metric.get()
          print(f"[Epoch {epoch}] Training: {name}={acc}")
  
          name, val_acc = test(ctx)
          print(f"[Epoch {epoch}] Validation: {name}={val_acc}")
  
      net.save_parameters("mnist.params")
  
file_path: mxnet/example/gluon/mnist/mnist.py
imports: from __future__ import print_function
  import argparse
  import logging
  import numpy
  import mxnet
  from mxnet import gluon, autograd
  from mxnet.gluon import nn
label_desc: Use image node_id 3 for calling a global function with example usage: train(epochs, ctx) without return types