コード例 #1
0
    def testTotalAttributionsWithSubKeys(self, sub_key, expected_values):
        computations = attributions.TotalAttributions().computations(
            sub_keys=[sub_key])

        example1 = {
            'attributions': {
                'feature1': [1.11, 1.13, 1.12],
                'feature2': [1.21, 1.23, 1.22]
            }
        }
        example2 = {
            'attributions': {
                'feature1': [2.11, 2.13, 2.12],
                'feature2': [2.21, 2.23, 2.22]
            }
        }
        example3 = {
            'attributions': {
                'feature1': np.array([3.11, 3.13, 3.12]),
                'feature2': np.array([3.21, 3.23, 3.22])
            }
        }

        with beam.Pipeline() as pipeline:
            # pylint: disable=no-value-for-parameter
            result = (
                pipeline
                | 'Create' >> beam.Create([example1, example2, example3])
                | 'Process' >> beam.ParDo(computations[0].preprocessor)
                | 'AddSlice' >> beam.Map(lambda x: ((), x))
                | 'CombineAttributions' >> beam.CombinePerKey(
                    computations[0].combiner)
                | 'ComputeResult' >>
                beam.Map(  # comment to add lamda on own line
                    lambda x: (x[0], computations[1].result(x[1]))))

            # pylint: enable=no-value-for-parameter

            def check_result(got):
                try:
                    self.assertLen(got, 1)
                    got_slice_key, got_attributions = got[0]
                    self.assertEqual(got_slice_key, ())
                    total_attributions_key = metric_types.AttributionsKey(
                        name='total_attributions', sub_key=sub_key)
                    self.assertIn(total_attributions_key, got_attributions)
                    self.assertAllClose(
                        got_attributions[total_attributions_key],
                        expected_values)

                except AssertionError as err:
                    raise util.BeamAssertException(err)

            util.assert_that(result, check_result, label='result')
コード例 #2
0
 def testHasAttributionsMetrics(self):
   specs_with_attributions = metric_specs.specs_from_metrics({
       'output_name': [
           tf.keras.metrics.MeanSquaredError('mse'),
           attributions.TotalAttributions()
       ]
   })
   self.assertTrue(
       attributions.has_attributions_metrics(specs_with_attributions))
   specs_without_attributions = metric_specs.specs_from_metrics([
       tf.keras.metrics.MeanSquaredError('mse'),
   ])
   self.assertFalse(
       attributions.has_attributions_metrics(specs_without_attributions))
コード例 #3
0
    def testTotalAttributionsWithMultiModelsAndOutputs(self, model_name,
                                                       output_name, examples,
                                                       expected_values):
        computations = attributions.TotalAttributions().computations(
            model_names=[model_name], output_names=[output_name])

        with beam.Pipeline() as pipeline:
            # pylint: disable=no-value-for-parameter
            result = (
                pipeline
                | 'Create' >> beam.Create(examples)
                | 'Process' >> beam.ParDo(computations[0].preprocessor)
                | 'AddSlice' >> beam.Map(lambda x: ((), x))
                | 'CombineAttributions' >> beam.CombinePerKey(
                    computations[0].combiner)
                | 'ComputeResult' >>
                beam.Map(  # comment to add lamda on own line
                    lambda x: (x[0], computations[1].result(x[1]))))

            # pylint: enable=no-value-for-parameter

            def check_result(got):
                try:
                    self.assertLen(got, 1)
                    got_slice_key, got_attributions = got[0]
                    self.assertEqual(got_slice_key, ())
                    total_attributions_key = metric_types.AttributionsKey(
                        name='total_attributions',
                        model_name=model_name,
                        output_name=output_name)
                    self.assertIn(total_attributions_key, got_attributions)
                    self.assertDictElementsAlmostEqual(
                        got_attributions[total_attributions_key],
                        expected_values)

                except AssertionError as err:
                    raise util.BeamAssertException(err)

            util.assert_that(result, check_result, label='result')