def get_assignment_map_from_checkpoint(tvars,
                                       init_checkpoint,
                                       my_scope,
                                       ckpt_scope,
                                       load_only_bert=False):
  """Compute the union of the current variables and checkpoint variables."""
  assignment_map = {}
  initialized_variable_names = {}

  name_to_variable = collections.OrderedDict()
  for var in tvars:
    name = var.name
    m = re.match("^(.*):\\d+$", name)
    if m is not None:
      name = m.group(1)
    if my_scope is not None:
      if name.startswith(my_scope):
        name = name[len(my_scope):]
      else:
        continue
    if load_only_bert and ("bert" not in name):
      continue
    name_to_variable[name] = var

  init_vars = tf.train.list_variables(init_checkpoint)

  assignment_map = collections.OrderedDict()
  for x in init_vars:
    (name, var) = (x[0], x[1])
    if ckpt_scope is not None:
      if name.startswith(ckpt_scope):
        name = name[len(ckpt_scope):]
      else:
        continue
    if name not in name_to_variable:
      continue
    assignment_map[ckpt_scope + name] = name_to_variable[name]
    initialized_variable_names[my_scope + name] = 1
    initialized_variable_names[my_scope + name + ":0"] = 1

  return (assignment_map, initialized_variable_names)
Compute the union of the current variables and checkpoint variables.
language/labs/drkit/run_dualencoder_qa.py