Exemplo n.º 1
0
 def test_past_stop_threshold_none_false(self):
     """Tests that check None returns false."""
     self.assertFalse(model_helpers.past_stop_threshold(None, -1.5))
     self.assertFalse(model_helpers.past_stop_threshold(None, None))
     self.assertFalse(model_helpers.past_stop_threshold(None, 1.5))
     # Zero should be okay, though.
     self.assertTrue(model_helpers.past_stop_threshold(0, 1.5))
Exemplo n.º 2
0
 def test_past_stop_threshold(self):
     """Tests for normal operating conditions."""
     self.assertTrue(model_helpers.past_stop_threshold(0.54, 1))
     self.assertTrue(model_helpers.past_stop_threshold(54, 100))
     self.assertFalse(model_helpers.past_stop_threshold(0.54, 0.1))
     self.assertFalse(model_helpers.past_stop_threshold(-0.54, -1.5))
     self.assertTrue(model_helpers.past_stop_threshold(-0.54, 0))
     self.assertTrue(model_helpers.past_stop_threshold(0, 0))
     self.assertTrue(model_helpers.past_stop_threshold(0.54, 0.54))
Exemplo n.º 3
0
def trainmain(argv):
    parser = WideDeepArgParser()  #实例化WideDeepArgParser,用于解析启动参数
    flags = parser.parse_args(args=argv[1:])  #获得解析后的参数flags
    print("解析的参数为:", flags)

    shutil.rmtree(flags.model_dir, ignore_errors=True)  #如果模型存在,整个目录删除
    model = build_estimator(flags.model_dir, flags.model_type)  #生成估算器对象

    train_file = os.path.join(flags.data_dir, 'adult.data.csv')  #获得训练集样本文件的路径
    test_file = os.path.join(flags.data_dir, 'adult.test.csv')  #获得测试集样本文件的路径

    def train_input_fn():  #定义训练集样本输入函数
        return input_fn(  #该输入函数按照batch_size批次,迭代输入epochs_between_evals次,使用乱序处理
            train_file, flags.epochs_between_evals, True, flags.batch_size)

    def eval_input_fn():  #定义测试集样本输入函数
        return input_fn(test_file, 1, False,
                        flags.batch_size)  #该输入函数按照batch_size批次,迭代输入1次,不使用乱序处理

    loss_prefix = LOSS_PREFIX.get(flags.model_type, '')  #格式化输出loss的前缀
    train_hook = hooks_helper.get_logging_tensor_hook(                   #定义训练钩子,获得训练过程中的状态
        batch_size=flags.batch_size,
        tensors_to_log={'average_loss': loss_prefix + 'head/truediv',
                        'loss': loss_prefix + 'head/weighted_loss/Sum'})

    for n in range(
            flags.train_epochs):  #将总迭代数,按照epochs_between_evals分段。并循环对每段进行训练
        if flags.model_type == 'BoostedTrees':
            model.train(input_fn=train_input_fn)
        else:
            model.train(input_fn=train_input_fn,
                        hooks=[train_hook])  #调用估算器的train方法进行训练
        results = model.evaluate(
            input_fn=eval_input_fn)  #调用估算器的evaluate方法进行评估计算

        print('{0:-^60}'.format('evaluate at epoch %d' % ((n + 1))))  #分隔符

        for key in sorted(results):  #显示评估结果
            print('%s: %s' % (key, results[key]))

        if model_helpers.past_stop_threshold(  #根据accuracy的阈值,来判断是否需要结束训练。
                flags.stop_threshold, results['accuracy']):
            break

    if flags.export_dir is not None:  #根据设置导出冻结图模型,用于tfseving
        export_model(model, flags.model_type, flags.export_dir)
def run_loop(
        estimator, schedule_manager, train_hooks=None, benchmark_logger=None,
        bleu_source=None, bleu_ref=None, bleu_threshold=None, vocab_file=None):
  """Train and evaluate model, and optionally compute model's BLEU score.

  **Step vs. Epoch vs. Iteration**

  Steps and epochs are canonical terms used in TensorFlow and general machine
  learning. They are used to describe running a single process (train/eval):
    - Step refers to running the process through a single or batch of examples.
    - Epoch refers to running the process through an entire dataset.

  E.g. training a dataset with 100 examples. The dataset is
  divided into 20 batches with 5 examples per batch. A single training step
  trains the model on one batch. After 20 training steps, the model will have
  trained on every batch in the dataset, or, in other words, one epoch.

  Meanwhile, iteration is used in this implementation to describe running
  multiple processes (training and eval).
    - A single iteration:
      1. trains the model for a specific number of steps or epochs.
      2. evaluates the model.
      3. (if source and ref files are provided) compute BLEU score.

  This function runs through multiple train+eval+bleu iterations.

  Args:
    estimator: tf.Estimator containing model to train.
    schedule_manager: A schedule.Manager object to guide the run loop.
    train_hooks: List of hooks to pass to the estimator during training.
    benchmark_logger: a BenchmarkLogger object that logs evaluation data
    bleu_source: File containing text to be translated for BLEU calculation.
    bleu_ref: File containing reference translations for BLEU calculation.
    bleu_threshold: minimum BLEU score before training is stopped.
    vocab_file: Path to vocab file that will be used to subtokenize bleu_source.

  Raises:
    ValueError: if both or none of single_iteration_train_steps and
      single_iteration_train_epochs were defined.
    NotFoundError: if the vocab file or bleu files don't exist.
  """
  if bleu_source:
    _validate_file(bleu_source)
  if bleu_ref:
    _validate_file(bleu_ref)
  if vocab_file:
    _validate_file(vocab_file)

  evaluate_bleu = bleu_source is not None and bleu_ref is not None
  if evaluate_bleu and schedule_manager.use_tpu:
    raise ValueError("BLEU score can not be computed when training with a TPU, "
                     "as it requires estimator.predict which is not yet "
                     "supported.")

  # Print details of training schedule.
  tf.logging.info("Training schedule:")
  tf.logging.info(
      "\t1. Train for {}".format(schedule_manager.train_increment_str))
  tf.logging.info("\t2. Evaluate model.")
  if evaluate_bleu:
    tf.logging.info("\t3. Compute BLEU score.")
    if bleu_threshold is not None:
      tf.logging.info("Repeat above steps until the BLEU score reaches %f" %
                      bleu_threshold)
  if not evaluate_bleu or bleu_threshold is None:
    tf.logging.info("Repeat above steps %d times." %
                    schedule_manager.train_eval_iterations)

  if evaluate_bleu:
    # Create summary writer to log bleu score (values can be displayed in
    # Tensorboard).
    bleu_writer = tf.summary.FileWriter(
        os.path.join(estimator.model_dir, BLEU_DIR))
    if bleu_threshold is not None:
      # Change loop stopping condition if bleu_threshold is defined.
      schedule_manager.train_eval_iterations = INF

  # Loop training/evaluation/bleu cycles
  for i in xrange(schedule_manager.train_eval_iterations):
    tf.logging.info("Starting iteration %d" % (i + 1))

    # Train the model for single_iteration_train_steps or until the input fn
    # runs out of examples (if single_iteration_train_steps is None).
    estimator.train(
        dataset.train_input_fn,
        steps=schedule_manager.single_iteration_train_steps,
        hooks=train_hooks)

    eval_results = estimator.evaluate(
        input_fn=dataset.eval_input_fn,
        steps=schedule_manager.single_iteration_eval_steps)

    tf.logging.info("Evaluation results (iter %d/%d):" %
                    (i + 1, schedule_manager.train_eval_iterations))
    tf.logging.info(eval_results)
    benchmark_logger.log_evaluation_result(eval_results)

    # The results from estimator.evaluate() are measured on an approximate
    # translation, which utilize the target golden values provided. The actual
    # bleu score must be computed using the estimator.predict() path, which
    # outputs translations that are not based on golden values. The translations
    # are compared to reference file to get the actual bleu score.
    if evaluate_bleu:
      uncased_score, cased_score = evaluate_and_log_bleu(
          estimator, bleu_source, bleu_ref, vocab_file)

      # Write actual bleu scores using summary writer and benchmark logger
      global_step = get_global_step(estimator)
      summary = tf.Summary(value=[
          tf.Summary.Value(tag="bleu/uncased", simple_value=uncased_score),
          tf.Summary.Value(tag="bleu/cased", simple_value=cased_score),
      ])
      bleu_writer.add_summary(summary, global_step)
      bleu_writer.flush()
      benchmark_logger.log_metric(
          "bleu_uncased", uncased_score, global_step=global_step)
      benchmark_logger.log_metric(
          "bleu_cased", cased_score, global_step=global_step)

      # Stop training if bleu stopping threshold is met.
      if model_helpers.past_stop_threshold(bleu_threshold, uncased_score):
        bleu_writer.close()
        break
def main(argv):
    parser = MNISTArgParser()
    flags = parser.parse_args(args=argv[1:])

    model_function = model_fn

    if flags.multi_gpu:
        validate_batch_size_for_multi_gpu(flags.batch_size)

        # There are two steps required if using multi-GPU: (1) wrap the model_fn,
        # and (2) wrap the optimizer. The first happens here, and (2) happens
        # in the model_fn itself when the optimizer is defined.
        model_function = tf.contrib.estimator.replicate_model_fn(
            model_fn, loss_reduction=tf.losses.Reduction.MEAN)

    data_format = flags.data_format
    if data_format is None:
        data_format = ('channels_first'
                       if tf.test.is_built_with_cuda() else 'channels_last')
    mnist_classifier = tf.estimator.Estimator(model_fn=model_function,
                                              model_dir=flags.model_dir,
                                              params={
                                                  'data_format': data_format,
                                                  'multi_gpu': flags.multi_gpu
                                              })

    # Set up training and evaluation input functions.
    def train_input_fn():
        """Prepare data for training."""

        # When choosing shuffle buffer sizes, larger sizes result in better
        # randomness, while smaller sizes use less memory. MNIST is a small
        # enough dataset that we can easily shuffle the full epoch.
        ds = dataset.train(flags.data_dir)
        ds = ds.cache().shuffle(buffer_size=50000).batch(flags.batch_size)

        # Iterate through the dataset a set number (`epochs_between_evals`) of times
        # during each training session.
        ds = ds.repeat(flags.epochs_between_evals)
        return ds

    def eval_input_fn():
        return dataset.test(flags.data_dir).batch(
            flags.batch_size).make_one_shot_iterator().get_next()

    # Set up hook that outputs training logs every 100 steps.
    train_hooks = hooks_helper.get_train_hooks(flags.hooks,
                                               batch_size=flags.batch_size)

    # Train and evaluate model.
    for _ in range(flags.train_epochs // flags.epochs_between_evals):
        mnist_classifier.train(input_fn=train_input_fn, hooks=train_hooks)
        eval_results = mnist_classifier.evaluate(input_fn=eval_input_fn)
        print('\nEvaluation results:\n\t%s\n' % eval_results)

        if model_helpers.past_stop_threshold(flags.stop_threshold,
                                             eval_results['accuracy']):
            break

    # Export the model
    if flags.export_dir is not None:
        image = tf.placeholder(tf.float32, [None, 28, 28])
        input_fn = tf.estimator.export.build_raw_serving_input_receiver_fn({
            'image':
            image,
        })
        mnist_classifier.export_savedmodel(flags.export_dir, input_fn)
Exemplo n.º 6
0
    def test_past_stop_threshold_not_number(self):
        """Tests for error conditions."""
        with self.assertRaises(ValueError):
            model_helpers.past_stop_threshold("str", 1)

        with self.assertRaises(ValueError):
            model_helpers.past_stop_threshold("str", tf.constant(5))

        with self.assertRaises(ValueError):
            model_helpers.past_stop_threshold("str", "another")

        with self.assertRaises(ValueError):
            model_helpers.past_stop_threshold(0, None)

        with self.assertRaises(ValueError):
            model_helpers.past_stop_threshold(0.7, "str")

        with self.assertRaises(ValueError):
            model_helpers.past_stop_threshold(tf.constant(4), None)