Search code examples
pythontensorflowtensorboardtensorflow-estimator

Plot both validation and training accuracy using Custom Estimators


So i'm using this tutorial to create my own custom estimator and I can't get the tensorboard to draw both validation accuracy during the training process. This issue on Github perfectly describes my problem. As someone mentioned in the last comment by setting save_checkpoints_steps to a small value, the model should do evaluation at each step, However that's not the case for me. after I run:

classifier = tf.estimator.Estimator(
  model_fn=my_model,
  params={
      'n_classes': 4,
  },
  model_dir=model_dir_str,
  config=tf.estimator.RunConfig(save_checkpoints_steps=int(1)))

loss_hook = early_stopping.stop_if_lower_hook(classifier, "loss", 0.2, model_dir_str + 'loss_eval')
acc_hook = early_stopping.stop_if_no_increase_hook(classifier, "accuracy", 100, model_dir_str + 'acc_eval')
train_spec = tf.estimator.TrainSpec(input_fn=input_train_fn, max_steps=steps, hooks=[loss_hook, acc_hook])
eval_spec = tf.estimator.EvalSpec(input_fn=input_eval_fn, steps=1000)
results = tf.estimator.train_and_evaluate(classifier, train_spec, eval_spec)

I only get two points in the plot. I have experimented with different values for and I still get the same results. Tensorboard plot with save_checkpoints_steps = 1


Solution

  • Since no one has came up with an answer, I'm posting this messy work-around for the time being.

    loss_hook = early_stopping.stop_if_lower_hook(classifier, "loss", 0.2, 'loss_eval')
    acc_hook = early_stopping.stop_if_no_increase_hook(classifier, "accuracy", 100, 'acc_eval')    
    tf.logging.set_verbosity(False)
    for i in range(int(steps/10)):
        print(i)
        classifier.train(
            input_fn=input_train_fn,
            steps=10,
            hooks=[loss_hook, acc_hook])
        # Evaluate the model.
        eval_result = classifier.evaluate(input_fn=input_eval_fn, steps=5)
    print(eval_result)
    

    Basically you run the training for a small number of steps and then do one evaluation.