code: def train(
      save_dir,
      sess,
      cond_input_op,
      y,
      train_pl,
      loss,
      train_step,
      eg_loss,
      eg_train,
      train_eg,
      accuracy_op,
      accuracy_update_op,
      reset_metrics_op,
      training_iterator,
      validation_iterator,
      test_iterator,
      handle,
  ):
      init = tf.group(
          tf.global_variables_initializer(),
          tf.local_variables_initializer(),
      )
      sess.run(init)
  
      training_handle = sess.run(training_iterator.string_handle())
      validation_handle = sess.run(validation_iterator.string_handle())
      test_handle = sess.run(test_iterator.string_handle())
  
      max_acc = 0.0
  
      sess.run(training_iterator.initializer)
      global_step = tf.train.get_or_create_global_step()
      saver = tf.train.Saver()
  
      validation_accuracies = []
      validation_total_variances = []
      while True:
          try:
              _, _, i = sess.run(
                  [train_step, accuracy_update_op, global_step],
                  feed_dict={handle: training_handle, train_pl: True},
              )
              if FLAGS.lamb > 0.0:
                  _, train_var_loss = sess.run(
                      [eg_train, eg_loss],
                      feed_dict={
                          handle: training_handle,
                          train_eg: True,
                      },
                  )
          except tf.errors.OutOfRangeError:
              break
  
          if i % validation_step == 0:
              sess.run(reset_metrics_op)
              sess.run(validation_iterator.initializer)
              while True:
                  try:
                      sess.run(
                          accuracy_update_op,
                          feed_dict={handle: validation_handle},
                      )
                  except tf.errors.OutOfRangeError:
                      break
  
              sess.run(validation_iterator.initializer)
              vald_var_loss = sess.run(
                  eg_loss,
                  feed_dict={handle: validation_handle, train_eg: True},
              )
              validation_accuracy = sess.run(
                  accuracy_op, feed_dict={handle: validation_handle}
              )
  
              validation_total_variances.append(vald_var_loss)
              validation_accuracies.append(validation_accuracy)
  
              print(
                  "Iteration: {}, validation accuracy: {:.6f}, e-variation vald (batch): {:.6f}".format(
                      i, validation_accuracy, vald_var_loss
                  ),
                  end="\r",
              )
              sess.run(reset_metrics_op)
  
          if validation_accuracy > max_acc:
              max_acc = validation_accuracy
              save_path = saver.save(sess, save_dir)
  
      # Restore variables from disk
      saver.restore(sess, save_dir)
  
      # Loop over all batches
      sess.run(reset_metrics_op)
      sess.run(test_iterator.initializer)
      while True:
          try:
              sess.run(
                  accuracy_update_op,
                  feed_dict={handle: test_handle, train_pl: False},
              )
          except tf.errors.OutOfRangeError:
              break
      test_accuracy = sess.run(accuracy_op)
      print("Test accuracy: {:.6f}".format(test_accuracy))
      return (
          validation_total_variances,
          validation_accuracies,
          test_accuracy,
      )
  
file_path: attributionpriors/attributionpriors/mnist/mnist_train.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import numpy
  import tensorflow
  import numpy
  import tensorflow.contrib.slim
  import datetime
  import mnist_model
  import mnist_reader
  from attributionpriors.ops import AttributionPriorExplainer
label_desc: Use image node_id 6 for calling a global function with example usage: train(save_dir, sess, cond_input_op, y, train_pl, loss, train_step, eg_loss, eg_train, train_eg, accuracy_op, accuracy_update_op, reset_metrics_op, training_iterator, validation_iterator, test_iterator, handle) and returns: validation_total_variances,  validation_accuracies,  test_accuracy