def _check_labels_and_predictions(predictions_dict, labels_dict): """Raise TypeError if the predictions and labels cannot be understood.""" if not (types.is_tensor(predictions_dict) or prediction_keys.PredictionKeys.LOGISTIC in predictions_dict or prediction_keys.PredictionKeys.PREDICTIONS in predictions_dict): raise TypeError( 'cannot find predictions in %s. It is expected that either' 'predictions_dict is a tensor or it contains PredictionKeys.LOGISTIC' 'or PredictionKeys.PREDICTIONS.' % predictions_dict) if not types.is_tensor(labels_dict): raise TypeError('labels_dict is %s, which is not a tensor' % labels_dict)
def is_compatible(self, features_dict, predictions_dict, labels_dict): if (self._example_weight_key and self._example_weight_key not in features_dict): raise ValueError( 'example weight key %s not found in features_dict. ' 'features were: %s' % (self._example_weight_key, features_dict.keys())) if (types.is_tensor(predictions_dict) or prediction_keys.PredictionKeys.LOGISTIC in predictions_dict or prediction_keys.PredictionKeys.PREDICTIONS in predictions_dict): if types.is_tensor(labels_dict): return True return False
def _get_prediction_tensor(predictions_dict): """Returns prediction Tensor for a specific Estimators. Returns the prediction Tensor for some regression Estimators. Args: predictions_dict: Predictions dictionary. Returns: Predictions tensor, or None if none of the expected keys are found in the predictions_dict. """ if types.is_tensor(predictions_dict): return predictions_dict key_precedence = (prediction_keys.PredictionKeys.LOGISTIC, prediction_keys.PredictionKeys.PREDICTIONS, prediction_keys.PredictionKeys.PROBABILITIES, prediction_keys.PredictionKeys.LOGITS) for key in key_precedence: ref_tensor = predictions_dict.get(key) if ref_tensor is not None: return ref_tensor return None
def _get_prediction_tensor(predictions_dict): """Returns prediction Tensor for a specific Estimators. Returns the prediction Tensor for some regression Estimators. Args: predictions_dict: Predictions dictionary. Returns: Predictions tensor. Raises: KeyError: No expected keys are found in predictions_dict. """ if types.is_tensor(predictions_dict): return predictions_dict key_precedence = (prediction_keys.PredictionKeys.LOGISTIC, prediction_keys.PredictionKeys.PREDICTIONS, prediction_keys.PredictionKeys.PROBABILITIES, prediction_keys.PredictionKeys.LOGITS) for key in key_precedence: ref_tensor = predictions_dict.get(key) if ref_tensor is not None: return ref_tensor raise KeyError('cannot find any keys %s in predictions_dict %s.' % (key_precedence, predictions_dict))
def _get_prediction_tensor(predictions_dict): """Returns prediction Tensor for a specific Estimators. Returns the prediction Tensor for some regression Estimators. Args: predictions_dict: Predictions dictionary. Returns: Predictions tensor. """ if types.is_tensor(predictions_dict): ref_tensor = predictions_dict else: ref_tensor = predictions_dict.get( prediction_keys.PredictionKeys.LOGISTIC) if ref_tensor is None: ref_tensor = predictions_dict.get( prediction_keys.PredictionKeys.PREDICTIONS) return ref_tensor
def check_compatibility(self, features_dict, predictions_dict, labels_dict): if not isinstance(predictions_dict, dict): raise TypeError( 'predictions_dict should be a dict. predictions_dict ' 'was: %s' % predictions_dict) if prediction_keys.PredictionKeys.CLASSES not in predictions_dict: raise KeyError( 'predictions_dict should contain PredictionKeys.CLASSES. ' 'predictions_dict was: %s' % predictions_dict) if prediction_keys.PredictionKeys.PROBABILITIES not in predictions_dict: raise KeyError( 'predictions_dict should contain ' 'PredictionKeys.PROBABILITIES. predictions_dict was: %s' % predictions_dict) if not types.is_tensor(labels_dict): raise TypeError( 'labels_dict should be a tensor. labels_dict was: %s' % labels_dict) _check_weight_present(features_dict, self._example_weight_key)
def get_metric_ops(self, features_dict, predictions_dict, labels_dict): ref_tensor = _get_prediction_tensor(predictions_dict) if ref_tensor is None: # Note that if predictions_dict is a Tensor and not a dict, # get_predictions_tensor will return predictions_dict, so if we get # here, if means that predictions_dict is a dict without any of the # standard keys. # # If we can't get any of standard keys, then pick the first key # in alphabetical order if the predictions dict is non-empty. # If the predictions dict is empty, try the labels dict. # If that is empty too, default to the empty Tensor. tf.logging.info( 'ExampleCount post export metric: could not find any of ' 'the standard keys in predictions_dict (keys were: %s)', predictions_dict.keys()) if predictions_dict is not None and predictions_dict.keys(): first_key = sorted(predictions_dict.keys())[0] ref_tensor = predictions_dict[first_key] tf.logging.info( 'Using the first key from predictions_dict: %s', first_key) elif labels_dict is not None: if types.is_tensor(labels_dict): ref_tensor = labels_dict tf.logging.info('Using the labels Tensor') elif labels_dict.keys(): first_key = sorted(labels_dict.keys())[0] ref_tensor = labels_dict[first_key] tf.logging.info('Using the first key from labels_dict: %s', first_key) if ref_tensor is None: tf.logging.info( 'Could not find a reference Tensor for example count. ' 'Defaulting to the empty Tensor.') ref_tensor = tf.constant([]) return { metric_keys.EXAMPLE_COUNT: metrics.total(tf.shape(ref_tensor)[0]) }
def is_compatible(self, features_dict, predictions_dict, labels_dict): if (types.is_tensor(predictions_dict) or prediction_keys.PredictionKeys.LOGISTIC in predictions_dict or prediction_keys.PredictionKeys.PREDICTIONS in predictions_dict): return True
def _check_labels(labels_dict): """Raise TypeError if the labels cannot be understood.""" if not types.is_tensor(labels_dict): raise TypeError('labels_dict is %s, which is not a tensor' % labels_dict)