1

I've successfully trained and validated my TensorFlow model via the Experiment framework. I'm a little lost on how I can now make predictions on new samples from the saved model. My experiment code looks like with my feature columns defined in a previous cell:

def experiment_fn(output_dir):
    return tflearn.Experiment(
        tflearn.DNNClassifier(feature_columns=feature_cols,
                              hidden_units=[30, 2],
                              n_classes=2,
                              model_dir=output_dir),
        train_input_fn=get_train(),
        eval_input_fn=get_valid(),
        eval_metrics = {
    "accuracy":
        tf.contrib.learn.MetricSpec(
            metric_fn=tf.contrib.metrics.streaming_accuracy,
            prediction_key=tf.contrib.learn.PredictionKey.CLASSES),
    "precision":
        tf.contrib.learn.MetricSpec(
            metric_fn=tf.contrib.metrics.streaming_precision,
            prediction_key=tf.contrib.learn.PredictionKey.CLASSES),
    "recall":
        tf.contrib.learn.MetricSpec(
            metric_fn=tf.contrib.metrics.streaming_recall,
            prediction_key=tf.contrib.learn.PredictionKey.CLASSES)
}
    )

learn_runner.run(experiment_fn, 'model_trained')

Looking at this post on saving and restoring a model, I thought I may be able to retrieve the predict method on the DNNClassifier like this:

op_to_restore = graph.get_tensor_by_name("predict:0")

and then build a feed_dict with new samples and run:

print sess.run(op_to_restore,feed_dict)

However, after successfully restoring the model, TensorFlow complains it cannot find a 'predict' operation.

Any help here would be greatly appreciated. Thanks!

Vince
  • 589
  • 9
  • 16
  • If you set `schedule='train'` in the `learn_runner`, it will return an Estimator. For `schedule='train_and_evaluate` it wants an [ExportStrategy](https://www.tensorflow.org/api_docs/python/tf/contrib/learn/ExportStrategy), or you could re-construct the Estimator from the last checkpoint by passing it the same model directory as used by the Experiment. – Allen Lavoie Jul 26 '17 at 17:48
  • Thanks! Using schedule='train' works fine. I can predict on my test data. However, do you have any advice on mapping the predictions back to the 'key' column in my original data? In this case it represents the row number from the original results. – Vince Jul 26 '17 at 18:21
  • This is just using the Estimator's `predict()`? I'd avoid shuffling in the `input_fn`, then predictions should be in the same order as the inputs. – Allen Lavoie Jul 26 '17 at 19:28
  • Thanks. Yes, looks like this is working. – Vince Jul 27 '17 at 20:48
  • 1
    I might have a solution to this problem which I documented at the end of a blogpost I recently wrote: https://medium.com/onfido-tech/higher-level-apis-in-tensorflow-67bfb602e6c0 – Peter Sep 05 '17 at 20:27

0 Answers0