code: def run_train_loop(
      optimizer, train_iter, test_iter, test_count, epoch, device
  ):
      model = optimizer.target
  
      train_count = 0
      sum_accuracy = 0
      sum_loss = 0
      while train_iter.epoch < epoch:
          batch = train_iter.next()
          x_array, t_array = convert.concat_examples(batch, device)
          x = chainer.Variable(x_array)
          t = chainer.Variable(t_array, requires_grad=False)
          optimizer.update(model, x, t)
          train_count += len(t)
          sum_loss += float(model.loss.array) * len(t)
          sum_accuracy += float(model.accuracy.array) * len(t)
  
          if train_iter.is_new_epoch:
              print("epoch: ", train_iter.epoch)
              print(
                  "train mean loss: {}, accuracy: {}".format(
                      sum_loss / train_count, sum_accuracy / train_count
                  )
              )
              # evaluation
              train_count = 0
              sum_accuracy = 0
              sum_loss = 0
              # It is good practice to turn off train mode during evaluation.
              with configuration.using_config("train", False):
                  for batch in test_iter:
                      x_array, t_array = convert.concat_examples(
                          batch, device
                      )
                      x = chainer.Variable(x_array)
                      t = chainer.Variable(t_array, requires_grad=False)
                      loss = model(x, t)
                      sum_loss += float(loss.array) * len(t)
                      sum_accuracy += float(model.accuracy.array) * len(
                          t
                      )
  
              test_iter.reset()
              print(
                  "test mean  loss: {}, accuracy: {}".format(
                      sum_loss / test_count, sum_accuracy / test_count
                  )
              )
              sum_accuracy = 0
              sum_loss = 0
  
file_path: chainer/examples/static_graph_optimizations/mnist/train_mnist_custom_loop.py
imports: from __future__ import print_function
  import argparse
  import warnings
  import numpy
  import chainer
  from chainer import configuration
  from chainer.dataset import convert
  import chainer.links
  from chainer import serializers
  import chainerx
  import train_mnist
label_desc: Use image node_id 1 for calling a global function with example usage: run_train_loop(optimizer, train_iter, test_iter, test_count, epoch, device) without return types