code: def train(args):
      """
      Train the knowledge graph embedding model.
      :param args: all args.
      :return: None
      """
      kgreader = KGLoader(
          batch_size=args.batch_size,
          data_dir=args.data_dir,
          neg_mode=args.neg_mode,
          neg_times=args.neg_times,
      )
      if args.model in model_dict:
          Model = model_dict[args.model]
      else:
          raise ValueError("No model for name {}".format(args.model))
      model = Model(
          data_reader=kgreader,
          hidden_size=args.hidden_size,
          margin=args.margin,
          learning_rate=args.learning_rate,
          args=args,
          optimizer=args.optimizer,
      )
  
      def iter_map_wrapper(data_batch, repeat=1):
          """
          wrapper for multiprocess reader
          :param data_batch: the source data iter.
          :param repeat: repeat data for multi epoch
          :return: iterator of feed data
          """
  
          def data_repeat():
              """repeat data for multi epoch"""
              for i in range(repeat):
                  for d in data_batch():
                      yield d
  
          reader = mp_reader_mapper(
              data_repeat,
              func=kgreader.training_data_no_filter
              if args.nofilter
              else kgreader.training_data_map,
              num_works=args.sample_workers,
          )
  
          return reader
  
      def iter_wrapper(data_batch, feed_list):
          """
          Decorator of make up the feed dict
          :param data_batch: the source data iter.
          :param feed_list: the feed list (names of variables).
          :return: iterator of feed data.
          """
  
          def work():
              """work"""
              for batch in data_batch():
                  feed_dict = {}
                  for k, v in zip(feed_list, batch):
                      feed_dict[k] = v
                  yield feed_dict
  
          return work
  
      loader = fluid.io.DataLoader.from_generator(
          feed_list=model.train_feed_vars, capacity=20, iterable=True
      )
  
      places = (
          fluid.cuda_places() if args.use_cuda else fluid.cpu_places()
      )
      exe = fluid.Executor(places[0])
      exe.run(model.startup_program)
      exe.run(fluid.default_startup_program())
      if args.pretrain and model.model_name in ["TransR", "transr"]:
          pretrain_ent = os.path.join(
              args.checkpoint,
              model.ent_name.replace("TransR", "TransE"),
          )
          pretrain_rel = os.path.join(
              args.checkpoint,
              model.rel_name.replace("TransR", "TransE"),
          )
          if os.path.exists(pretrain_ent):
              print("loading pretrain!")
              # var = fluid.global_scope().find_var(model.ent_name)
              load_var(
                  exe, model.train_program, model.ent_name, pretrain_ent
              )
              # var = fluid.global_scope().find_var(model.rel_name)
              load_var(
                  exe, model.train_program, model.rel_name, pretrain_rel
              )
          else:
              raise ValueError(
                  "pretrain file {} not exists!".format(pretrain_ent)
              )
  
      prog = fluid.CompiledProgram(
          model.train_program
      ).with_data_parallel(loss_name=model.train_fetch_vars[0].name)
  
      if args.only_evaluate:
          s = timeit.default_timer()
          fluid.io.load_params(
              exe,
              dirname=args.checkpoint,
              main_program=model.train_program,
          )
          Evaluate(kgreader).launch_evaluation(
              exe=exe,
              reader=iter_wrapper(
                  kgreader.test_data_batch, model.test_feed_list
              ),
              fetch_list=model.test_fetch_vars,
              program=model.test_program,
              num_workers=10,
          )
          log.info(timeit.default_timer() - s)
          return None
  
      batch_iter = iter_map_wrapper(
          kgreader.training_data_batch,
          repeat=args.evaluate_per_iteration,
      )
      loader.set_batch_generator(batch_iter, places=places)
  
      for epoch in range(0, args.epoch // args.evaluate_per_iteration):
          run_round(
              batch_iter=loader,
              exe=exe,
              prefix="train",
              # program=model.train_program,
              program=prog,
              fetch_list=model.train_fetch_vars,
              log_per_step=kgreader.train_num // args.batch_size,
              epoch=epoch * args.evaluate_per_iteration,
          )
          log.info(
              "epoch\t%s" % ((1 + epoch) * args.evaluate_per_iteration)
          )
          fluid.io.save_params(
              exe,
              dirname=args.checkpoint,
              main_program=model.train_program,
          )
          if not args.noeval:
              eva = Evaluate(kgreader)
              eva.launch_evaluation(
                  exe=exe,
                  reader=iter_wrapper(
                      kgreader.test_data_batch, model.test_feed_list
                  ),
                  fetch_list=model.test_fetch_vars,
                  program=model.test_program,
                  num_workers=10,
              )
  
file_path: PGL/legacy/examples/pgl-ke/main.py
imports: import argparse
  import timeit
  import os
  import numpy
  import paddle.fluid
  from data_loader import KGLoader
  from evalutate import Evaluate
  from model import model_dict
  from model.utils import load_var
  from mp_mapper import mp_reader_mapper
  from pgl.utils.logger import log
label_desc: Use image node_id 2 for calling a global function with example usage: train(args) and returns: reader, work, None