According to the ML Engine documentation, an instance key is required to match the returned predictions with the input data. For simplicity purposes, I would like to use a DNNClassifier but apparently canned estimators don't seem to support instance keys yet (only custom or tensorflow core estimators).
So I looked at the Census code examples of Custom/TensorflowCore Estimators but they look quite complex for what I am trying to achieve.
I would prefer using a similar approach as described in this stackoverflow answer (wrapping a DNNClassifier into a custom estimator) but I can not make it work and I got an error saying that 'DNNClassifier' object has no attribute 'model_fn'...
How can I achieve this in a simple manner?
My code as per Eli's example:
but still unsuccessful to display the instance key in the result of predictions using ML Engine batch predictions... What do I need to change in the Experiment (or maybe in the export strategy) to make it work?
In version 1.2 the contrib estimators (
tf.contrib.learn.DNNClassifier
for example), were changed to inherit from the core estimator classtf.estimator.Estimator
which unlike it's predecessor, hides the model function as a private class member.Try
estimator._model_fn
rather thanestimator.model_fn
. You should be able to leave everything else in my previous answer the same.EDIT: I've updated my original answer here: https://stackoverflow.com/a/44443380/3597868 to reflect the necessary changes with version 1.2
System/Version Info
Approach
Fabrice, I had the same question as you and it took me a while to figure this one out (with the generous help of Eli). I took a slightly different approach. Instead of trying to create an instance key, I made the assumption that the instance key would be in the data (training, evaluation, and prediction).
Here, I use the gender field as the instance key. Obviously, I would not use the gender field in reality as an instance key, I'm only using it here for illustration purposes.
Other than those changes described here, am not making any updates to any other functions or constants from the original script other than to change some things from python 2 to python 3, e.g., changing dict.iteritems() to dict.items().
Here is a gist of my modified model.py file. I did not make any changes to the task.py file.
Updating the
key_model_fn_gen()
functionThis code relies on guidance I got from Eli. The insight for me was that I need to modify the output_alternatives dictionary in order to return the key and that I do not need to modify the predictions dictionary. (Additionally, I learned that I could get the params as an attribute of the estimator from your (Fabrice's) example, thanks for that.)
Updating the
build_estimator()
functiondeep_columns
list andwide_columns
list so that it is not used as a feature for training and evaluation.Here is the full code:
Input format for batch predictions
After the version has been uploaded to ML Engine, the prediction input takes the following form:
Output format of batch prediction
After completing the batch prediction job, I get the following output: