code: def testPlateauOpHook(self):
      global_step = tf.train.create_global_step()
      counter = tf.get_variable("count", initializer=0, dtype=tf.int32)
      indicator = tf.get_variable(
          "indicator", initializer=0, dtype=tf.int32
      )
      tf.summary.scalar("count", counter)
      incr_global_step = tf.assign_add(global_step, 1)
      incr_counter = tf.assign_add(counter, 1)
      incr_indicator = tf.assign_add(indicator, 1)
  
      # Stop if the global step has not gone up by more than 1 in 20 steps.
  
      ckpt_dir = self.ckpt_dir("plateauop")
      stop_hook = metrics_hook.PlateauOpHook(
          ckpt_dir,
          "count_1",
          incr_indicator,
          num_plateau_steps=20,
          plateau_delta=1.0,
          plateau_decrease=False,
          every_n_steps=10,
      )
      with self.sess(stop_hook, ckpt_dir) as sess:
          for _ in range(20):
              sess.run((incr_global_step, incr_counter))
  
          # Summary files should now have 2 values in them
          self.flush()
  
          # Run for more steps so that the hook gets triggered and we verify that we
          # don't stop.
          for _ in range(30):
              sess.run((incr_global_step, incr_counter))
  
          self.flush()
  
          # Run without incrementing the counter
          for _ in range(30):
              sess.run(incr_global_step)
          self.flush()
  
          self.assertTrue(sess.run(indicator) < 1)
  
          # Metrics should be written such that now the counter has gone >20 steps
          # without being incremented.
          # Check that we run the incr_indicator op several times
          for _ in range(3):
              for _ in range(10):
                  sess.run(incr_global_step)
              self.flush()
  
          self.assertTrue(sess.run(indicator) > 1)
  
file_path: tensor2tensor/tensor2tensor/utils/metrics_hook_test.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import contextlib
  import os
  import shutil
  from tensor2tensor.utils import metrics_hook
  import tensorflow.compat.v1
label_desc: Use image node_id 7 for calling the MetricsHookTest obj's underlying member method code with example usage: obj.testPlateauOpHook() without return types