tf.contrib.estimator.forward_features(
estimator,
keys=None,
sparse_default_values=None
)
Forward features to predictions dictionary.
In some cases, user wants to see some of the features in estimators prediction output. As an example, consider a batch prediction service: The service simply runs inference on the users graph and returns the results. Keys are essential because there is no order guarantee on the outputs so they need to be rejoined to the inputs via keys or transclusion of the inputs in the outputs. Example:
def input_fn():
features, labels = ...
features['unique_example_id'] = ...
features, labels
estimator = tf.estimator.LinearClassifier(...)
estimator = tf.contrib.estimator.forward_features(
estimator, 'unique_example_id')
estimator.train(...)
assert 'unique_example_id' in estimator.predict(...)
Args:
estimator
: Atf.estimator.Estimator
object.keys
: Astring
or alist
ofstring
. If it isNone
, all of thefeatures
indict
is forwarded to thepredictions
. If it is astring
, only given key is forwarded. If it is alist
of strings, all the givenkeys
are forwarded.sparse_default_values
: A dict ofstr
keys mapping the name of the sparse features to be converted to dense, to the default value to use. Only sparse features indicated in the dictionary are converted to dense and the provided default value is used.
Returns:
A new tf.estimator.Estimator
which forwards features to predictions.
Raises:
ValueError
: * ifkeys
is already part ofpredictions
. We don't allow override.- if 'keys' does not exist in
features
.
- if 'keys' does not exist in
TypeError
: ifkeys
type is not one ofstring
or list/tuple ofstring
.