()
return model_fn
doc.
doc.
doc .
def get_model_fn():
  """doc."""
  def model_fn(features, labels, mode, params):
    """doc."""
    #### Training or Evaluation
    is_training = (mode == tf.estimator.ModeKeys.TRAIN)
    assert is_training

    #### Retrieve `mems` from `params["cache"]`
    mems = {}
    idx = 0
    if FLAGS.mem_len > 0:
      mems["mems"] = params["cache"]

    #### Get loss from inputs
    total_loss, new_mems, monitor_dict = function_builder.get_loss(
        FLAGS, features, labels, mems, is_training)

    #### Turn `new_mems` into `new_cache`
    new_cache = []
    if FLAGS.mem_len > 0:
      new_cache += new_mems["mems"]

    #### Check model parameters
    num_params = sum([np.prod(v.shape) for v in tf.trainable_variables()])
    tf.logging.info("#params: {}".format(num_params))

    #### Configuring the optimizer
    train_op, learning_rate, gnorm = model_utils.get_train_op(
        FLAGS, total_loss)
    monitor_dict["lr"] = learning_rate
    monitor_dict["gnorm"] = gnorm

    #### Customized initial checkpoint
    scaffold_fn = model_utils.init_from_checkpoint(FLAGS, global_vars=True)

    #### Creating host calls
    host_call = function_builder.construct_scalar_host_call(
        monitor_dict=monitor_dict,
        model_dir=FLAGS.model_dir,
        prefix="train/",
        reduce_fn=tf.reduce_mean)

    #### Constucting training TPUEstimatorSpec with new cache.
    train_spec = tf.contrib.tpu.TPUEstimatorSpec(
        mode=mode, loss=total_loss, train_op=train_op, host_call=host_call,
        scaffold_fn=scaffold_fn)

    train_spec.cache = new_cache

    return train_spec

  return model_fn
def get_model_fn ( ) : def model_fn ( features , labels , mode , params ) : """doc.""" #### Training or Evaluation is_training = ( mode == tf . estimator . ModeKeys . TRAIN ) assert is_training #### Retrieve `mems` from `params["cache"]` mems = { } idx = 0 if FLAGS . mem_len > 0 : mems [ "mems" ] = params [ "cache" ] #### Get loss from inputs total_loss , new_mems , monitor_dict = function_builder . get_loss ( FLAGS , features , labels , mems , is_training ) #### Turn `new_mems` into `new_cache` new_cache = [ ] if FLAGS . mem_len > 0 : new_cache += new_mems [ "mems" ] #### Check model parameters num_params = sum ( [ np . prod ( v . shape ) for v in tf . trainable_variables ( ) ] ) tf . logging . info ( "#params: {}" . format ( num_params ) ) #### Configuring the optimizer train_op , learning_rate , gnorm = model_utils . get_train_op ( FLAGS , total_loss ) monitor_dict [ "lr" ] = learning_rate monitor_dict [ "gnorm" ] = gnorm #### Customized initial checkpoint scaffold_fn = model_utils . init_from_checkpoint ( FLAGS , global_vars = True ) #### Creating host calls host_call = function_builder . construct_scalar_host_call ( monitor_dict = monitor_dict , model_dir = FLAGS . model_dir , prefix = "train/" , reduce_fn = tf . reduce_mean ) #### Constucting training TPUEstimatorSpec with new cache. train_spec = tf . contrib . tpu . TPUEstimatorSpec ( mode = mode , loss = total_loss , train_op = train_op , host_call = host_call , scaffold_fn = scaffold_fn ) train_spec . cache = new_cache return train_spec return model_fn