I want to run a tf.estimator.Estimator
with a model_fn
that when called under tf.estimator.ModeKeys.EVAL
returns not only the loss but also a dictionary with the predicted tensor and the labels tensor (aka the truth).
I am experimenting with regressing an image, so I can have a visual taste of the input/prediction quality.
If I run the code inside my model_fn
:
predictions = {
"predictions": td.last() # return the last tensor (prediction)
}
if mode == tf.estimator.ModeKeys.PREDICT:
# wrap predictions into a class and return EstimatorSpec object
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)
# minimize on cross-entropy
loss = tf.losses.mean_squared_error(labels=labels, predictions=td.last()) # loss is a scalar tensor
if mode == tf.estimator.ModeKeys.EVAL:
predictions['truth'] = tf.convert_to_tensor(labels, dtype=tf.float32)
# wrap predictions into a class and return EstimatorSpec object
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, loss=loss)
TensorFlow would ignore the predictions parameter in the returned EstimatorSpec when running on evaluation. When running on predict, labels are not available.
Do you know if there is any way to do that?
Thanks!