Пример #1
0
 def _create_accumulator(
         self) -> tf_metric_accumulators.TFCompilableMetricsAccumulator:
     padding_options = None
     if self._eval_config is not None:
         model_spec = model_util.get_model_spec(self._eval_config,
                                                self._model_name)
         if model_spec is not None and model_spec.HasField(
                 'padding_options'):
             padding_options = model_spec.padding_options
     return tf_metric_accumulators.TFCompilableMetricsAccumulator(
         padding_options,
         self._output_counts,
         desired_batch_size=self._desired_batch_size)
Пример #2
0
  def create_accumulator(
      self) -> tf_metric_accumulators.TFCompilableMetricsAccumulator:
    configs = zip(self._metric_configs, self._loss_configs)
    padding_options = None
    if self._eval_config is not None:
      model_spec = model_util.get_model_spec(self._eval_config,
                                             self._model_name)
      if model_spec is not None and model_spec.HasField('padding_options'):
        padding_options = model_spec.padding_options

    return tf_metric_accumulators.TFCompilableMetricsAccumulator(
        padding_options, [len(m) + len(l) for m, l in configs],
        desired_batch_size=self._desired_batch_size)
    def testTFCompilableMetricsAccumulator(self):
        acc = tf_metric_accumulators.TFCompilableMetricsAccumulator(
            metric_counts=[1, 2], padding_options=None)

        self.assertEqual(0, acc.len_inputs())

        acc.add_input(0, np.array([1.0, 0.0]), np.array([0.5, 0.5]),
                      np.array([1.0]))
        acc.add_input(0, np.array([1.0, 1.0]), np.array([0.3, 0.7]),
                      np.array([1.0]))
        acc.add_input(1, np.array([0.0, 0.0]), np.array([0.2, 0.8]),
                      np.array([0.5]))
        acc.add_input(1, np.array([0.0, 1.0]), np.array([0.1, 0.9]),
                      np.array([0.5]))
        acc.add_input(1, np.array([1.0, 1.0]), np.array([0.6, 0.4]),
                      np.array([0.7]))
        self.assertAllClose(acc.get_inputs(0), (np.array([
            np.array([1.0, 0.0]), np.array([1.0, 1.0])
        ]), np.array([
            np.array([0.5, 0.5]), np.array([0.3, 0.7])
        ]), np.array([np.array([1.0]), np.array([1.0])])))
        self.assertAllClose(
            acc.get_inputs(1),
            (np.array([
                np.array([0.0, 0.0]),
                np.array([0.0, 1.0]),
                np.array([1.0, 1.0])
            ]),
             np.array([
                 np.array([0.2, 0.8]),
                 np.array([0.1, 0.9]),
                 np.array([0.6, 0.4])
             ]), np.array([np.array([0.5]),
                           np.array([0.5]),
                           np.array([0.7])])))

        acc.clear_inputs()
        self.assertEqual(0, acc.len_inputs())

        acc.add_weights(0, 0, np.array([1.0, 2.0]))
        acc.add_weights(1, 0, np.array([3.0, 4.0]))
        acc.add_weights(1, 1, np.array([5.0, 6.0]))
        acc.add_weights(1, 1, np.array([7.0, 8.0]))
        self.assertAllClose(acc.get_weights(0, 0), np.array([1.0, 2.0]))
        self.assertAllClose(acc.get_weights(1, 0), np.array([3.0, 4.0]))
        self.assertAllClose(acc.get_weights(1, 1), np.array([12.0, 14.0]))