code: def _train_iter_generator(
      initial_iter=0,
      final_iter=0,
      validation_every_n=0,
      validation_max_iter=0,
  ):
      """
      This generator yields a sequence of interleaved training and validation
      iterations.
  
      :param initial_iter: starting iteration of the training sequence
      :param final_iter: ending iteration of the training sequence
      :param validation_every_n: validation at every n training
      :param validation_max_iter: number of validation iterations
      :return: iteration message instances
      """
      train_iterations = _iter_msg_generator(
          range(initial_iter + 1, final_iter + 1), TRAIN
      )
      for train_iter_msg in train_iterations:
          yield train_iter_msg
          current_iter = train_iter_msg.current_iter
          if (
              current_iter > 0
              and validation_every_n > 0
              and current_iter % validation_every_n == 0
          ):
              # generating validation iterations without changing the current
              # iteration number.
              valid_iterations = _iter_msg_generator(
                  [current_iter] * validation_max_iter, VALID
              )
              for valid_iter_msg in valid_iterations:
                  yield valid_iter_msg
  
imports: import itertools
  import time
  from niftynet.engine.application_variables import CONSOLE, TF_SUMMARIES
  from niftynet.engine.signal import TRAIN, VALID, INFER
  from niftynet.utilities.util_common import look_up_operations
label_desc: Use image node_id 2 for calling a global function with example usage: _train_iter_generator(initial_iter, final_iter, validation_every_n, validation_max_iter) without return types