code: def membership_inference(args, load_data_module, download_mnist):
      # load clusters:
      clusters = None
      if args.number_arms is not None:
          clusters_file = "clusters_K=%d_pca=%d.torch" % (
              args.number_arms,
              args.pca,
          )
          clusters_file = os.path.join(
              load_data_module.MEMOIZE_FOLDER, clusters_file
          )
          logging.info("Loading clusters from file...")
          clusters = torch.load(clusters_file)
  
      # load dataset:
      train_data, _ = load_data_module.load_data(
          split="train", download_mnist_func=download_mnist
      )
      components = examples.util.pca(train_data, args.pca)
      positive_set = load_data_module.load_data(
          split="train",
          pca=components,
          clusters=clusters,
          bandwidth=args.bandwidth,
          download_mnist_func=download_mnist,
      )
      negative_set = load_data_module.load_data(
          split="test",
          pca=components,
          clusters=clusters,
          bandwidth=args.bandwidth,
          download_mnist_func=download_mnist,
      )
  
      # get list of checkpoints:
      model_files = [
          os.path.join(args.checkpoint_folder, filename)
          for filename in os.listdir(args.checkpoint_folder)
          if filename.endswith(".torch")
      ]
      model_files = sorted(model_files)
      iterations = [
          int(os.path.splitext(f)[0].split("_")[-1])
          for f in model_files
      ]
  
      # load permutation used in training:
      perm = load_data_module.load_data_sampler(
          permfile=args.permfile, download_mnist_func=download_mnist
      )
  
      def subset(dataset, iteration):
          ids = perm[:iteration]
          return tuple(d[ids, :] for d in dataset)
  
      # measure accuracies of membership inference attacs:
      advantage = [
          membership_accuracy(
              torch.load(model_file),
              subset(positive_set, iteration),
              negative_set,
              epsilon=args.epsilon,
          )
          for model_file, iteration in zip(model_files, iterations)
      ]
  
      # save advantages to file:
      if args.savefile is not None:
          with open(args.savefile, "wb") as fid:
              pickle.dump(advantage, fid)
  
      # plot advantages:
      if args.visualize:
          opts = {
              "xlabel": "Number of iterations",
              "ylabel": "Accuracy of inference attack",
          }
          visdom.line(iterations, advantage, opts=opts)
  
file_path: CrypTen/examples/bandits/membership_inference.py
imports: import argparse
  import logging
  import os
  import pickle
  import examples.util
  import torch
  import visdom
  from examples.multiprocess_launcher import MultiProcessLauncher
label_desc: Use image node_id 4 for calling a global function with example usage: membership_inference(args, load_data_module, download_mnist) and returns: tuple