tf.trainable_variables() returns blank list outside model_fn In a downloaded tensorflow code, when below model_fn_builder is called, then model_fn (down below) loads the tf.trainable_variables().   

I tried to move tvars = tf.trainable_variables() out of model_fn so that variables do not get loaded everytime the prediction is called (model_fn is called on every prediction generator parsing).  

However tf.trainable_variables() returns blank list. Why is this so? Is tf different within model_fn and outside? 

model_fn = model_fn_builder(
  bert_config=bert_config,
  num_labels=len(label_list),
  init_checkpoint=FLAGS.init_checkpoint,
  learning_rate=FLAGS.learning_rate,
  num_train_steps=num_train_steps,
  num_warmup_steps=num_warmup_steps,
  use_tpu=FLAGS.use_tpu,
  use_one_hot_embeddings=FLAGS.use_tpu)


In below code #THIS I AM TRYING TO MOVE OUT OF MODEL_FN is the comment which to be focused:

def model_fn_builder(bert_config, num_labels, init_checkpoint, learning_rate,
                     num_train_steps, num_warmup_steps, use_tpu,
                     use_one_hot_embeddings):
  """Returns `model_fn` closure for TPUEstimator."""

  global tvars , initialized_variable_names , scaffold_fn
  tvars = tf.trainable_variables()  #THIS I AM TRYING TO MOVE OUT OF MODEL_FN

  if init_checkpoint:
      (assignment_map, initialized_variable_names
       ) = modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint)
      if use_tpu:

          def tpu_scaffold():
              tf.train.init_from_checkpoint(init_checkpoint, assignment_map)
              return tf.train.Scaffold()

          scaffold_fn = tpu_scaffold
      else:
          tf.train.init_from_checkpoint(init_checkpoint, assignment_map)

  def model_fn(features, labels, mode, params):  # pylint: disable=unused-argument
    """The `model_fn` for TPUEstimator."""

    tf.logging.info("*** Features ***")
    for name in sorted(features.keys()):
      tf.logging.info("  name = %s, shape = %s" % (name, features[name].shape))

    input_ids = features["input_ids"]
    input_mask = features["input_mask"]
    segment_ids = features["segment_ids"]
    label_ids = features["label_ids"]
    is_real_example = None
    if "is_real_example" in features:
      is_real_example = tf.cast(features["is_real_example"], dtype=tf.float32)
    else:
      is_real_example = tf.ones(tf.shape(label_ids), dtype=tf.float32)

    is_training = (mode == tf.estimator.ModeKeys.TRAIN)

    (total_loss, per_example_loss, logits, probabilities) = create_model(
        bert_config, is_training, input_ids, input_mask, segment_ids, label_ids,
        num_labels, use_one_hot_embeddings)

    # tvars = tf.trainable_variables() #THIS IS ORIGINAL PLACE WHICH WORKS
    # initialized_variable_names = {}
    # scaffold_fn = None
    # if init_checkpoint:
    #   (assignment_map, initialized_variable_names
    #   ) = modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint)
    #   if use_tpu:
    #
    #     def tpu_scaffold():
    #       tf.train.init_from_checkpoint(init_checkpoint, assignment_map)
    #       return tf.train.Scaffold()
    #
    #     scaffold_fn = tpu_scaffold
    #   else:
    #     tf.train.init_from_checkpoint(init_checkpoint, assignment_map)

    tf.logging.info("**** Trainable Variables ****")
This problem is resolved... tensorflow.python.framework.errors_impl.NotFoundError: Key output_bias not found in checkpoint – Actually there were model.ckpt-10949 files.. and tensorflow was picking up this checkpoint automatically... these files seem to be incomplete or corrupt. After deleting 1model.ckpt-10949* files key NotFoundError went away