Пример #1
0
def run_evaluation(strategy,
                   test_input_fn,
                   eval_steps,
                   input_meta_data,
                   model,
                   step,
                   eval_summary_writer=None):
    """Run evaluation for SQUAD task.

  Args:
    strategy: distribution strategy.
    test_input_fn: input function for evaluation data.
    eval_steps: total number of evaluation steps.
    input_meta_data: input meta data.
    model: keras model object.
    step: current training step.
    eval_summary_writer: summary writer used to record evaluation metrics.

  """
    def _test_step_fn(inputs):
        """Replicated validation step."""

        inputs["mems"] = None
        res = model(inputs, training=False)
        return res, inputs["unique_ids"]

    @tf.function
    def _run_evaluation(test_iterator):
        """Runs validation steps."""
        res, unique_ids = strategy.experimental_run_v2(
            _test_step_fn, args=(next(test_iterator), ))
        return res, unique_ids

    # pylint: disable=protected-access
    test_iterator = data_utils._get_input_iterator(test_input_fn, strategy)
    # pylint: enable=protected-access
    cur_results = []
    eval_examples = squad_utils.read_squad_examples(
        input_meta_data["predict_file"], is_training=False)
    with tf.io.gfile.GFile(input_meta_data["predict_file"]) as f:
        orig_data = json.load(f)["data"]

    for _ in range(eval_steps):
        results, unique_ids = _run_evaluation(test_iterator)
        unique_ids = strategy.experimental_local_results(unique_ids)

        for result_key in results:
            results[result_key] = (strategy.experimental_local_results(
                results[result_key]))
        for core_i in range(strategy.num_replicas_in_sync):
            bsz = int(input_meta_data["test_batch_size"] /
                      strategy.num_replicas_in_sync)
            for j in range(bsz):
                result = {}
                for result_key in results:
                    result[result_key] = results[result_key][core_i].numpy()[j]
                result["unique_ids"] = unique_ids[core_i].numpy()[j]
                # We appended a fake example into dev set to make data size can be
                # divided by test_batch_size. Ignores this fake example during
                # evaluation.
                if result["unique_ids"] == 1000012047:
                    continue
                unique_id = int(result["unique_ids"])

                start_top_log_probs = ([
                    float(x) for x in result["start_top_log_probs"].flat
                ])
                start_top_index = [
                    int(x) for x in result["start_top_index"].flat
                ]
                end_top_log_probs = ([
                    float(x) for x in result["end_top_log_probs"].flat
                ])
                end_top_index = [int(x) for x in result["end_top_index"].flat]

                cls_logits = float(result["cls_logits"].flat[0])
                cur_results.append(
                    squad_utils.RawResult(
                        unique_id=unique_id,
                        start_top_log_probs=start_top_log_probs,
                        start_top_index=start_top_index,
                        end_top_log_probs=end_top_log_probs,
                        end_top_index=end_top_index,
                        cls_logits=cls_logits))
                if len(cur_results) % 1000 == 0:
                    logging.info("Processing example: %d", len(cur_results))

    output_prediction_file = os.path.join(input_meta_data["predict_dir"],
                                          "predictions.json")
    output_nbest_file = os.path.join(input_meta_data["predict_dir"],
                                     "nbest_predictions.json")
    output_null_log_odds_file = os.path.join(input_meta_data["predict_dir"],
                                             "null_odds.json")

    ret = squad_utils.write_predictions(
        eval_examples, input_meta_data["eval_features"], cur_results,
        input_meta_data["n_best_size"], input_meta_data["max_answer_length"],
        output_prediction_file, output_nbest_file, output_null_log_odds_file,
        orig_data, input_meta_data["start_n_top"],
        input_meta_data["end_n_top"])

    # Log current result

    log_str = "Result | "
    for key, val in ret.items():
        log_str += "{} {} | ".format(key, val)
    logging.info(log_str)
    if eval_summary_writer:
        with eval_summary_writer.as_default():
            tf.summary.scalar("best_f1", ret["best_f1"], step=step)
            tf.summary.scalar("best_exact", ret["best_exact"], step=step)
            eval_summary_writer.flush()
def run_evaluation(strategy,
                   test_input_fn,
                   eval_steps,
                   model,
                   step,
                   eval_summary_writer=None):
    """Run evaluation for classification task.

  Args:
    strategy: distribution strategy.
    test_input_fn: input function for evaluation data.
    eval_steps: total number of evaluation steps.
    model: keras model object.
    step: current train step.
    eval_summary_writer: summary writer used to record evaluation metrics.  As
      there are fake data samples in validation set, we use mask to get rid of
      them when calculating the accuracy. For the reason that there will be
      dynamic-shape tensor, we first collect logits, labels and masks from TPU
      and calculate the accuracy via numpy locally.

  Returns:
    A float metric, accuracy.
  """
    def _test_step_fn(inputs):
        """Replicated validation step."""

        inputs["mems"] = None
        _, logits = model(inputs, training=False)
        return logits, inputs["label_ids"], inputs["is_real_example"]

    @tf.function
    def _run_evaluation(test_iterator):
        """Runs validation steps."""
        logits, labels, masks = strategy.experimental_run_v2(
            _test_step_fn, args=(next(test_iterator), ))
        return logits, labels, masks

    # pylint: disable=protected-access
    test_iterator = data_utils._get_input_iterator(test_input_fn, strategy)
    # pylint: enable=protected-access
    correct = 0
    total = 0
    for _ in range(eval_steps):
        logits, labels, masks = _run_evaluation(test_iterator)
        logits = strategy.experimental_local_results(logits)
        labels = strategy.experimental_local_results(labels)
        masks = strategy.experimental_local_results(masks)
        merged_logits = []
        merged_labels = []
        merged_masks = []

        for i in range(strategy.num_replicas_in_sync):
            merged_logits.append(logits[i].numpy())
            merged_labels.append(labels[i].numpy())
            merged_masks.append(masks[i].numpy())
        merged_logits = np.vstack(np.array(merged_logits))
        merged_labels = np.hstack(np.array(merged_labels))
        merged_masks = np.hstack(np.array(merged_masks))
        real_index = np.where(np.equal(merged_masks, 1))
        correct += np.sum(
            np.equal(np.argmax(merged_logits[real_index], axis=-1),
                     merged_labels[real_index]))
        total += np.shape(real_index)[-1]
    accuracy = float(correct) / float(total)
    logging.info("Train step: %d  /  acc = %d/%d = %f", step, correct, total,
                 accuracy)
    if eval_summary_writer:
        with eval_summary_writer.as_default():
            tf.summary.scalar("eval_acc",
                              float(correct) / float(total),
                              step=step)
            eval_summary_writer.flush()
    return accuracy
Пример #3
0
def train(
    strategy: tf.distribute.Strategy,
    model_fn: Callable,
    input_meta_data: Dict,
    train_input_fn: Callable,
    total_training_steps: int,
    steps_per_epoch: int,
    steps_per_loop: int,
    optimizer: tf.keras.optimizers.Optimizer,
    learning_rate_fn: tf.keras.optimizers.schedules.LearningRateSchedule,
    eval_fn: Optional[Callable[[tf.keras.Model, int, tf.summary.SummaryWriter],
                               Any]] = None,
    metric_fn: Optional[Callable[[], tf.keras.metrics.Metric]] = None,
    test_input_fn: Optional[Callable] = None,
    init_checkpoint: Optional[Text] = None,
    model_dir: Optional[Text] = None,
    save_steps: Optional[int] = None,
    run_eagerly: Optional[bool] = False):
  """Runs customized training.

  Args:
      strategy: Distribution strategy on which to run low level training loop.
      model_fn: The function returns a keras.Model.
      input_meta_data: A dictionary of params: `mem_len`, `lr_layer_decay_rate`,
        `n_layer`, `batch_size_per_core` and `d_model`.
      train_input_fn: Function returns a tf.data.Dataset used for training.
      total_training_steps: Number of steps to train in total.
      steps_per_epoch: Number of steps to run per epoch. At the end of each
        epoch, model checkpoint will be saved and evaluation will be conducted
        if evaluation dataset is provided.
      steps_per_loop: Number of steps per graph-mode loop. In order to reduce
        communication in eager context, training logs are printed every
        steps_per_loop.
      optimizer: The optimizer for model.
      learning_rate_fn: the learning rate schedule.
      eval_fn: A callback of evaluation function, that takes a keras.Model,
        current step and evaluation summary writer.
      metric_fn: A metrics function returns a Keras Metric object to record
        evaluation result using evaluation dataset or with training dataset
        after every epoch.
      test_input_fn:  Function returns a evaluation dataset. If none, evaluation
        is skipped.
      init_checkpoint: Optional checkpoint to load to `sub_model` returned by
        `model_fn`.
      model_dir: The directory of model (checkpoints, summaries).
      save_steps: The frequency to save checkpoints. Every save_steps, we save a
        model checkpoint.
      run_eagerly: Whether to run training eagerly.

  Returns:
      Last training step logits if training happens, otherwise returns None.
  Raises:
    TypeError: if model directory is not specified.
  """
  required_arguments = [
      train_input_fn, total_training_steps, steps_per_epoch, steps_per_loop,
      optimizer, learning_rate_fn
  ]
  if [arg for arg in required_arguments if arg is None]:
    raise ValueError("`train_input_fn`, `total_training_steps`, "
                     "`steps_per_epoch`, `steps_per_loop`, `optimizer` and "
                     "`learning_rate_fn` are required parameters.")
  if not model_dir:
    raise TypeError("Model directory must be specified.")
  # pylint: disable=protected-access
  train_iterator = data_utils._get_input_iterator(train_input_fn, strategy)
  # pylint: enable=protected-access
  train_summary_writer = None
  eval_summary_writer = None
  if not tf.io.gfile.exists(model_dir):
    tf.io.gfile.mkdir(model_dir)
  if test_input_fn:
    eval_summary_writer = tf.summary.create_file_writer(
        os.path.join(model_dir, "summaries/eval"))
  if steps_per_loop >= _MIN_SUMMARY_STEPS:
    # Only writes summary when the stats are collected sufficiently over
    # enough steps.
    train_summary_writer = tf.summary.create_file_writer(
        os.path.join(model_dir, "summaries/train"))

  with strategy.scope():
    model = model_fn()

    if init_checkpoint:
      logging.info("restore from %s", init_checkpoint)
      checkpoint = tf.train.Checkpoint(model=model)
      checkpoint.restore(init_checkpoint)

    model.optimizer = optimizer

    if not hasattr(model, "optimizer"):
      raise ValueError("User should set optimizer attribute to model.")

    train_loss_metric = tf.keras.metrics.Mean("training_loss", dtype=tf.float32)
    train_metric = None
    if metric_fn:
      train_metric = metric_fn()

    def _replicated_step(inputs, mem=None):
      """Replicated training step."""

      inputs["mems"] = mem
      with tf.GradientTape() as tape:
        mem, logits = model(inputs, training=True)
        loss = model.losses
        train_loss_metric.update_state(loss)
        if train_metric:
          train_metric.update_state(inputs["label_ids"], logits)
        scaled_loss = loss[0] * 1.0 / float(strategy.num_replicas_in_sync)

      # Collects training variables.
      tvars = model.trainable_variables
      grads = tape.gradient(scaled_loss, tvars)
      clipped, _ = tf.clip_by_global_norm(grads, clip_norm=1.0)

      if input_meta_data["lr_layer_decay_rate"] != 1.0:
        n_layer = 0
        for i in range(len(clipped)):
          m = re.search(r"model/transformer/layer_(\d+?)/", tvars[i].name)
          if not m:
            continue
          n_layer = max(n_layer, int(m.group(1)) + 1)

        for i in range(len(clipped)):
          for l in range(n_layer):
            if "model/transformer/layer_{}/".format(l) in tvars[i].name:
              abs_rate = input_meta_data["lr_layer_decay_rate"]**(
                  n_layer - 1 - l)
              clipped[i] *= abs_rate
              logging.info("Apply mult {:.4f} to layer-{} grad of {}".format(
                  abs_rate, l, tvars[i].name))
              break

      optimizer.apply_gradients(zip(clipped, tvars))
      if input_meta_data["mem_len"] > 0:
        return mem

    def train_steps(iterator, steps):
      """Performs distributed training steps in a loop.

      Args:
        iterator: the distributed iterator of training datasets.
        steps: an tf.int32 integer tensor to specify number of steps to run
          inside host training loop.

      Raises:
        ValueError: Any of the arguments or tensor shapes are invalid.

      Returns:
        logits: logits computed.
      """
      if not isinstance(steps, tf.Tensor):
        raise ValueError("steps should be an Tensor. Python object may cause "
                         "retracing.")

      def cache_fn():
        """Initializes memory tensor used in XLNet pretraining."""
        mems = []
        if input_meta_data["mem_len"] > 0:
          for _ in range(input_meta_data["n_layer"]):
            zeros = tf.zeros([
                input_meta_data["mem_len"],
                input_meta_data["batch_size_per_core"],
                input_meta_data["d_model"]
            ],
                             dtype=tf.float32)
            mems.append(zeros)
        return mems

      if input_meta_data["mem_len"] > 0:
        mem = strategy.experimental_run_v2(cache_fn)
        for _ in tf.range(steps):
          mem = strategy.experimental_run_v2(
              _replicated_step, args=(
                  next(iterator),
                  mem,
              ))
      else:
        for _ in tf.range(steps):
          strategy.experimental_run_v2(_replicated_step, args=(next(iterator),))

    if not run_eagerly:
      train_steps = tf.function(train_steps)

    logging.info("Start training...")
    checkpoint = tf.train.Checkpoint(model=model, optimizer=optimizer)
    latest_checkpoint_file = tf.train.latest_checkpoint(model_dir)
    if latest_checkpoint_file:
      logging.info("Checkpoint file %s found and restoring from checkpoint",
                   latest_checkpoint_file)
      checkpoint.restore(latest_checkpoint_file)
      logging.info("Loading from checkpoint file completed")

    current_step = optimizer.iterations.numpy()
    checkpoint_name = "xlnet_step_{step}.ckpt"

    while current_step < total_training_steps:
      train_loss_metric.reset_states()
      if train_metric:
        train_metric.reset_states()

      steps = _steps_to_run(current_step, steps_per_epoch, steps_per_loop)
      train_steps(train_iterator, tf.convert_to_tensor(steps, dtype=tf.int32))
      current_step += steps
      train_loss = _float_metric_value(train_loss_metric)
      log_stream = "Train step: %d/%d  /  lr = %.9f  /  loss = %.7f" % (
          current_step, total_training_steps, learning_rate_fn(current_step),
          train_loss)
      if train_metric:
        log_stream += "  /  %s = %f" % (train_metric.name,
                                        _float_metric_value(train_metric))
      logging.info(log_stream)
      if train_summary_writer:
        with train_summary_writer.as_default():
          tf.summary.scalar(
              "learning_rate",
              learning_rate_fn(current_step),
              step=current_step)
          tf.summary.scalar(
              train_loss_metric.name, train_loss, step=current_step)
          if train_metric:
            tf.summary.scalar(
                train_metric.name,
                _float_metric_value(train_metric),
                step=current_step)
          train_summary_writer.flush()
      if model_dir:
        if (save_steps is None) or (save_steps and
                                    current_step % save_steps == 0):
          _save_checkpoint(checkpoint, model_dir,
                           checkpoint_name.format(step=current_step))

      if test_input_fn and current_step % steps_per_epoch == 0:

        logging.info("Running evaluation after step: %s.", current_step)

        eval_fn(model, current_step, eval_summary_writer)
    if model_dir:
      _save_checkpoint(checkpoint, model_dir,
                       checkpoint_name.format(step=current_step))
    if test_input_fn:
      logging.info("Running final evaluation after training is complete.")
      eval_fn(model, current_step, eval_summary_writer)

    return model