code: def train(
      nn_type,
      use_cuda,
      save_dirname=None,
      model_filename=None,
      params_filename=None,
  ):
      if use_cuda and not fluid.core.is_compiled_with_cuda():
          return
  
      startup_program = fluid.default_startup_program()
      main_program = fluid.default_main_program()
  
      if args.enable_ce:
          train_reader = paddle.batch(
              paddle.dataset.mnist.train(), batch_size=BATCH_SIZE
          )
          test_reader = paddle.batch(
              paddle.dataset.mnist.test(), batch_size=BATCH_SIZE
          )
          startup_program.random_seed = 90
          main_program.random_seed = 90
      else:
          train_reader = paddle.batch(
              paddle.reader.shuffle(
                  paddle.dataset.mnist.train(), buf_size=500
              ),
              batch_size=BATCH_SIZE,
          )
          test_reader = paddle.batch(
              paddle.dataset.mnist.test(), batch_size=BATCH_SIZE
          )
  
      img = fluid.layers.data(
          name="img", shape=[1, 28, 28], dtype="float32"
      )
      label = fluid.layers.data(name="label", shape=[1], dtype="int64")
  
      if nn_type == "softmax_regression":
          net_conf = softmax_regression
      elif nn_type == "multilayer_perceptron":
          net_conf = multilayer_perceptron
      else:
          net_conf = convolutional_neural_network
  
      prediction, avg_loss, acc = net_conf(img, label)
  
      test_program = main_program.clone(for_test=True)
      optimizer = fluid.optimizer.Adam(learning_rate=0.001)
      optimizer.minimize(avg_loss)
  
      def train_test(
          train_test_program, train_test_feed, train_test_reader
      ):
          acc_set = []
          avg_loss_set = []
          for test_data in train_test_reader():
              acc_np, avg_loss_np = exe.run(
                  program=train_test_program,
                  feed=train_test_feed.feed(test_data),
                  fetch_list=[acc, avg_loss],
              )
              acc_set.append(float(acc_np))
              avg_loss_set.append(float(avg_loss_np))
          # get test acc and loss
          acc_val_mean = numpy.array(acc_set).mean()
          avg_loss_val_mean = numpy.array(avg_loss_set).mean()
          return avg_loss_val_mean, acc_val_mean
  
      place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
  
      exe = fluid.Executor(place)
  
      feeder = fluid.DataFeeder(feed_list=[img, label], place=place)
      exe.run(startup_program)
      epochs = [epoch_id for epoch_id in range(PASS_NUM)]
  
      lists = []
      step = 0
      for epoch_id in epochs:
          for step_id, data in enumerate(train_reader()):
              metrics = exe.run(
                  main_program,
                  feed=feeder.feed(data),
                  fetch_list=[avg_loss, acc],
              )
              if step % 100 == 0:
                  print(
                      "Pass %d, Epoch %d, Cost %f"
                      % (step, epoch_id, metrics[0])
                  )
              step += 1
          # test for epoch
          avg_loss_val, acc_val = train_test(
              train_test_program=test_program,
              train_test_reader=test_reader,
              train_test_feed=feeder,
          )
  
          print(
              "Test with Epoch %d, avg_cost: %s, acc: %s"
              % (epoch_id, avg_loss_val, acc_val)
          )
          lists.append((epoch_id, avg_loss_val, acc_val))
          if save_dirname is not None:
              fluid.io.save_inference_model(
                  save_dirname,
                  ["img"],
                  [prediction],
                  exe,
                  model_filename=model_filename,
                  params_filename=params_filename,
              )
  
      if args.enable_ce:
          print("kpis\ttrain_cost\t%f" % metrics[0])
          print("kpis\ttest_cost\t%s" % avg_loss_val)
          print("kpis\ttest_acc\t%s" % acc_val)
  
      # find the best pass
      best = sorted(lists, key=lambda list: float(list[1]))[0]
      print(
          "Best pass is %s, testing Avgcost is %s" % (best[0], best[1])
      )
      print(
          "The classification accuracy is %.2f%%"
          % (float(best[2]) * 100)
      )
  
file_path: AdvBox/DataPoison/mnist_paddle.py
imports: from __future__ import print_function
  from builtins import range
  import os
  import argparse
  from PIL import Image
  import numpy
  import paddle
  import paddle.fluid
label_desc: Use image node_id 6 for calling a global function with example usage: train(nn_type, use_cuda, save_dirname, model_filename, params_filename) and returns: avg_loss_val_mean,  acc_val_mean