code: def main(unused_argv):
      logging.set_verbosity(logging.ERROR)
  
      # Load training and test data.
      x_train, y_train, x_test, y_test = load_cifar10()
      # Sample weights are set to `None` by default, but can be changed here.
      sample_weight_train, sample_weight_test = None, None
  
      # Instantiate the tf.Estimator.
      classifier = tf_estimator.Estimator(
          model_fn=small_cnn_fn, model_dir=FLAGS.model_dir
      )
  
      # A function to construct input_fn given (data, label), to be used by the
      # membership inference training hook.
      def input_fn_constructor(x, y):
          return tf_compat_v1_estimator.inputs.numpy_input_fn(
              x={"x": x}, y=y, shuffle=False
          )
  
      # Get hook for membership inference attack.
      mia_hook = MembershipInferenceTrainingHook(
          classifier,
          (x_train, y_train),
          (x_test, y_test),
          input_fn_constructor,
          slicing_spec=SlicingSpec(entire_dataset=True, by_class=True),
          attack_types=[
              AttackType.THRESHOLD_ATTACK,
              AttackType.K_NEAREST_NEIGHBORS,
          ],
          tensorboard_dir=FLAGS.model_dir,
          tensorboard_merge_classifiers=FLAGS.tensorboard_merge_classifiers,
      )
  
      # Create tf.Estimator input functions for the training and test data.
      train_input_fn = tf_compat_v1_estimator.inputs.numpy_input_fn(
          x={"x": x_train},
          y=y_train,
          batch_size=FLAGS.batch_size,
          num_epochs=FLAGS.epochs,
          shuffle=True,
      )
      eval_input_fn = tf_compat_v1_estimator.inputs.numpy_input_fn(
          x={"x": x_test}, y=y_test, num_epochs=1, shuffle=False
      )
  
      # Training loop.
      steps_per_epoch = 50000 // FLAGS.batch_size
      for epoch in range(1, FLAGS.epochs + 1):
          # Train the model, with the membership inference hook.
          classifier.train(
              input_fn=train_input_fn,
              steps=steps_per_epoch,
              hooks=[mia_hook],
          )
  
          # Evaluate the model and print results
          eval_results = classifier.evaluate(input_fn=eval_input_fn)
          test_accuracy = eval_results["accuracy"]
          print(
              "Test accuracy after %d epochs is: %.3f"
              % (epoch, test_accuracy)
          )
  
      print("End of training attack")
      attack_results = run_attack_on_tf_estimator_model(
          classifier,
          (x_train, y_train, sample_weight_train),
          (x_test, y_test, sample_weight_test),
          input_fn_constructor,
          slicing_spec=SlicingSpec(entire_dataset=True, by_class=True),
          attack_types=[
              AttackType.THRESHOLD_ATTACK,
              AttackType.K_NEAREST_NEIGHBORS,
          ],
      )
      (
          att_types,
          att_slices,
          att_metrics,
          att_values,
      ) = get_flattened_attack_metrics(attack_results)
      print(
          "\n".join(
              [
                  "  %s: %.4f" % (", ".join([s, t, m]), v)
                  for t, s, m, v in zip(
                      att_types, att_slices, att_metrics, att_values
                  )
              ]
          )
      )
  
file_path: privacy/tensorflow_privacy/privacy/privacy_tests/membership_inference_attack/tf_estimator_evaluation_example.py
imports: from absl import app
  from absl import flags
  from absl import logging
  import numpy
  import tensorflow
  from tensorflow import estimator
  from tensorflow.compat.v1 import estimator
  from tensorflow_privacy.privacy.privacy_tests.membership_inference_attack.data_structures import AttackType
  from tensorflow_privacy.privacy.privacy_tests.membership_inference_attack.data_structures import get_flattened_attack_metrics
  from tensorflow_privacy.privacy.privacy_tests.membership_inference_attack.data_structures import SlicingSpec
  from tensorflow_privacy.privacy.privacy_tests.membership_inference_attack.tf_estimator_evaluation import MembershipInferenceTrainingHook
  from tensorflow_privacy.privacy.privacy_tests.membership_inference_attack.tf_estimator_evaluation import run_attack_on_tf_estimator_model
label_desc: Use image node_id 3 for calling a global function with example usage: main(unused_argv) and returns: tf_compat_v1_estimator