Пример #1
0
def render_slicing_metrics(
    result: view_types.EvalResult,
    slicing_column: Optional[Text] = None,
    slicing_spec: Optional[Union[slicer.SingleSliceSpec,
                                 config_pb2.SlicingSpec]] = None,
    weighted_example_column: Optional[Text] = None,
    event_handlers: Optional[Callable[[Dict[Text, Union[Text, float]]],
                                      None]] = None,
) -> Optional[visualization.SlicingMetricsViewer]:  # pytype: disable=invalid-annotation
    """Renders the slicing metrics view as widget.

  Args:
    result: An tfma.EvalResult.
    slicing_column: The column to slice on.
    slicing_spec: The tfma.SlicingSpec to filter results. If neither column nor
      spec is set, show overall.
    weighted_example_column: Override for the weighted example column. This can
      be used when different weights are applied in different aprts of the model
      (eg: multi-head).
    event_handlers: The event handlers

  Returns:
    A SlicingMetricsViewer object if in Jupyter notebook; None if in Colab.
  """
    if slicing_spec and isinstance(slicing_spec, config_pb2.SlicingSpec):
        slicing_spec = slicer.SingleSliceSpec(spec=slicing_spec)
    data = util.get_slicing_metrics(result.slicing_metrics, slicing_column,
                                    slicing_spec)
    cfg = util.get_slicing_config(result.config, weighted_example_column)

    return visualization.render_slicing_metrics(data,
                                                cfg,
                                                event_handlers=event_handlers)
Пример #2
0
def render_slicing_attributions(
    result: view_types.EvalResult,
    slicing_column: Optional[Text] = None,
    slicing_spec: Optional[Union[slicer.SingleSliceSpec,
                                 config_pb2.SlicingSpec]] = None,
    metric_name: Optional[Text] = None,
    weighted_example_column: Optional[Text] = None,
    event_handlers: Optional[Callable[[Dict[Text, Union[Text, float]]],
                                      None]] = None,
) -> Optional[visualization.SlicingMetricsViewer]:  # pytype: disable=invalid-annotation
    """Renders the slicing metrics view as widget.

  Args:
    result: An tfma.EvalResult.
    slicing_column: The column to slice on.
    slicing_spec: The tfma.SlicingSpec to filter results. If neither column nor
      spec is set, show overall.
    metric_name: Name of attributions metric to show attributions for. Optional
      if only one metric used.
    weighted_example_column: Override for the weighted example column. This can
      be used when different weights are applied in different aprts of the model
      (eg: multi-head).
    event_handlers: The event handlers

  Returns:
    A SlicingMetricsViewer object if in Jupyter notebook; None if in Colab.
  """
    if slicing_spec and isinstance(slicing_spec, config_pb2.SlicingSpec):
        slicing_spec = slicer.SingleSliceSpec(spec=slicing_spec)
    data = util.get_slicing_metrics(result.attributions, slicing_column,
                                    slicing_spec)
    # Attributions have one additional level of indirection for the metric_name.
    # Filter this out using the metric_name provided.
    for d in data:
        updated_data = {}
        for output_name, per_output_items in d['metrics'].items():  # pytype: disable=attribute-error
            updated_data[output_name] = {}
            for sub_key, per_sub_key_items in per_output_items.items():
                updated_data[output_name][sub_key] = {}
                if metric_name:
                    if metric_name not in per_sub_key_items:
                        raise ValueError(
                            'metric_name={} not found in {}'.format(
                                metric_name, per_sub_key_items.keys()))
                    updated_data[output_name][sub_key] = per_sub_key_items[
                        metric_name]
                elif len(per_sub_key_items) == 1:
                    updated_data[output_name][sub_key] = list(
                        per_sub_key_items.values())[0]
                else:
                    raise ValueError(
                        'metric_name must be one of the following: {}'.format(
                            per_sub_key_items.keys()))
        d['metrics'] = updated_data

    cfg = util.get_slicing_config(result.config, weighted_example_column)

    return visualization.render_slicing_metrics(data,
                                                cfg,
                                                event_handlers=event_handlers)
Пример #3
0
 def testOverrideWeightColumnForSlicingMetricsView(self):
   overriding_weight_column = 'override'
   eval_config = self._makeEvalConfig()
   slicing_config = util.get_slicing_config(
       eval_config, weighted_example_column_to_use=overriding_weight_column)
   self.assertEqual(slicing_config['weightedExamplesColumn'],
                    overriding_weight_column)
Пример #4
0
def render_slicing_metrics(
    result: model_eval_lib.EvalResult,
    slicing_column: Optional[Text] = None,
    slicing_spec: Optional[SingleSliceSpec] = None,
    weighted_example_column: Text = None
) -> Optional[visualization.SlicingMetricsViewer]:
    """Renders the slicing metrics view as widget.

  Args:
    result: An tfma.EvalResult.
    slicing_column: The column to slice on.
    slicing_spec: The slicing spec to filter results. If neither column nor spec
      is set, show overall.
    weighted_example_column: Override for the weighted example column. This can
      be used when different weights are applied in different aprts of the model
      (eg: multi-head).

  Returns:
    A SlicingMetricsViewer object if in Jupyter notebook; None if in Colab.
  """
    data = util.get_slicing_metrics(result.slicing_metrics, slicing_column,
                                    slicing_spec)
    config = util.get_slicing_config(result.config, weighted_example_column)

    return visualization.render_slicing_metrics(data, config)
Пример #5
0
def render_slicing_metrics(result,
                           slicing_column = None,
                           slicing_spec = None
                          ):
  """Renders the slicing metrics view as widget.

  Args:
    result: An tfma.EvalResult.
    slicing_column: The column to slice on.
    slicing_spec: The slicing spec to filter results. If neither column nor spec
      is set, show overall.

  Returns:
    A SlicingMetricsViewer object if in Jupyter notebook; None if in Colab.
  """
  data = util.get_slicing_metrics(result.slicing_metrics, slicing_column,
                                  slicing_spec)
  config = util.get_slicing_config(result.config)

  return visualization.render_slicing_metrics(data, config)
Пример #6
0
 def testGetSlicingConfig(self):
     eval_config = self._makeEvalConfig()
     slicing_config = util.get_slicing_config(eval_config)
     self.assertEqual(
         slicing_config,
         {'weightedExamplesColumn': 'post_export_metrics/example_weight'})
Пример #7
0
 def testGetSlicingConfig(self):
     eval_config = self._makeEvalConfig()
     slicing_config = util.get_slicing_config(eval_config)
     self.assertEquals(slicing_config,
                       {'weightedExamplesColumn': 'testing_key'})