in adanet/core/iteration.py [0:0]
def _best_export_outputs(self, candidates, best_candidate_index, mode,
best_predictions):
"""Returns the best `SavedModel` export outputs from a set of candidates.
Assumes that all candidate ensembles have identical export output keys and
`ExportOutput` types.
Args:
candidates: List of `_Candidate` instances to compare.
best_candidate_index: `Tensor` index of the best candidate in the list.
mode: Defines whether this is training, evaluation or inference. Export
outputs are always None during training and evaluation. See `ModeKeys`.
best_predictions: A `Tensor` or dictionary of `Tensor`s representing the
best candidate's predictions (depending on what the subnetworks return).
Returns:
A `Tensor` dictionary representing the best candidate's export outputs.
Raises:
TypeError: If the `ExportOutput` type is not supported.
"""
if mode != tf.estimator.ModeKeys.PREDICT:
return None
if len(candidates) == 1:
return candidates[0].ensemble_spec.export_outputs
with tf_compat.v1.variable_scope("best_export_outputs"):
# Group tensors by export output key and ExportOutput type.
export_outputs = {} # type: Any
for candidate in candidates:
ensemble_spec = candidate.ensemble_spec
for key in sorted(ensemble_spec.export_outputs):
export_output = ensemble_spec.export_outputs[key]
if isinstance(export_output,
tf.estimator.export.ClassificationOutput):
if key not in export_outputs:
export_outputs[key] = ([], [])
if export_output.scores is not None:
export_outputs[key][0].append(export_output.scores)
if export_output.classes is not None:
export_outputs[key][1].append(export_output.classes)
elif isinstance(export_output, tf.estimator.export.RegressionOutput):
if key not in export_outputs:
export_outputs[key] = []
export_outputs[key].append(export_output.value)
elif isinstance(export_output, tf.estimator.export.PredictOutput):
# Use self._best_predictions() below to get prediction output.
continue
else:
raise TypeError(
"Values in export_outputs must be ClassificationOutput, "
"RegressionOutput, or PredictOutput objects. Given: {}".format(
export_output))
# Stack tensor lists into correct ExportOutput type, outputting the
# correct values based on the best candidate index.
best_export_outputs = {}
for key in sorted(candidates[0].ensemble_spec.export_outputs):
export_output = candidates[0].ensemble_spec.export_outputs[key]
if isinstance(export_output, tf.estimator.export.ClassificationOutput):
scores, classes = None, None
if export_outputs[key][0]:
scores = tf.stack(export_outputs[key][0])[best_candidate_index]
if export_outputs[key][1]:
classes = tf.stack(export_outputs[key][1])[best_candidate_index]
output = tf.estimator.export.ClassificationOutput(
scores=scores, classes=classes)
elif isinstance(export_output, tf.estimator.export.RegressionOutput):
value = tf.stack(export_outputs[key])[best_candidate_index]
output = tf.estimator.export.RegressionOutput(value)
else:
predictions = copy.copy(export_output.outputs)
predictions.update(best_predictions)
output = tf.estimator.export.PredictOutput(predictions)
best_export_outputs[key] = output
return best_export_outputs