class_docstr: <a id="MNISTConfigs"></a>
  
  ## Trainer configurations
class_docstr_tok: ["<","a","id=","''","MNISTConfigs","''",">","<","\/a",">","#","#","Trainer","configurations"]
docstr: ### Training or validation step
docstr_tok: ["#","#","#","Training","or","validation","step"]
code: def step(self, batch: any, batch_idx: BatchIndex):
      """
      ### Training or validation step
      """
  
      # Training/Evaluation mode
      self.model.train(self.mode.is_train)
  
      # Move data to the device
      data, target = batch[0].to(self.device), batch[1].to(self.device)
  
      # Update global step (number of samples processed) when in training mode
      if self.mode.is_train:
          tracker.add_global_step(len(data))
  
      # Whether to capture model outputs
      with self.mode.update(is_log_activations=batch_idx.is_last):
          # Get model outputs.
          output = self.model(data)
  
      # Calculate and log loss
      loss = self.loss_func(output, target)
      tracker.add("loss.", loss)
  
      # Calculate and log accuracy
      self.accuracy(output, target)
      self.accuracy.track()
  
      # Train the model
      if self.mode.is_train:
          # Calculate gradients
          loss.backward()
          # Take optimizer step
          self.optimizer.step()
          # Log the model parameters and gradients on last batch of every epoch
          if batch_idx.is_last:
              tracker.add("model", self.model)
          # Clear the gradients
          self.optimizer.zero_grad()
  
      # Save the tracked metrics
      tracker.save()
  
file_path: annotated_deep_learning_paper_implementations/labml_nn/experiments/mnist.py
imports: import torch.nn
  import torch.utils.data
  from labml_helpers.module import Module
  from labml import tracker
  from labml.configs import option
  from labml_helpers.datasets.mnist import MNISTConfigs
  from labml_helpers.device import DeviceConfigs
  from labml_helpers.metrics.accuracy import Accuracy
  from labml_helpers.train_valid import TrainValidConfigs, BatchIndex, hook_model_outputs
  from labml_nn.optimizers.configs import OptimizerConfigs
label_desc: Use image node_id 2 for calling the MNISTConfigs obj's underlying member method code with example usage: obj.step(batch, batch_idx) without return types