def build_subnetwork_spec(self, name, subnetwork_builder, summary, features, mode, labels=None, previous_ensemble=None, config=None, params=None): del summary del features del mode del labels del previous_ensemble del params del config return _SubnetworkSpec( name=name, subnetwork=None, builder=subnetwork_builder, step=tf.Variable(0.), predictions=None, loss=None, train_op=subnetwork_builder.build_subnetwork_train_op( *[None for _ in range(7)]), eval_metrics=tu.create_subnetwork_metrics( metric_fn=lambda: {"a": (tf.constant(1), tf.constant(1))}))
def test_metric_ops_not_duplicated_on_cpu(self): with context.graph_mode(): self.setup_graph() metric_fn = lambda: {"metric": (tf.constant(5), tf.constant(5))} best_candidate_index = 3 mode = tf.estimator.ModeKeys.EVAL ensemble_metrics = tu.create_ensemble_metrics(metric_fn) subnetwork_metrics = tu.create_subnetwork_metrics(metric_fn) iteration_metrics = tu.create_iteration_metrics( ensemble_metrics=[ensemble_metrics], subnetwork_metrics=[subnetwork_metrics]) ensemble_ops1 = ensemble_metrics.eval_metrics_ops() ensemble_ops2 = ensemble_metrics.eval_metrics_ops() subnetwork_ops1 = subnetwork_metrics.eval_metrics_ops() subnetwork_ops2 = subnetwork_metrics.eval_metrics_ops() iteration_ops1 = iteration_metrics.best_eval_metric_ops( best_candidate_index, mode) iteration_ops2 = iteration_metrics.best_eval_metric_ops( best_candidate_index, mode) self.assertEqual(subnetwork_ops1, subnetwork_ops2) self.assertEqual(ensemble_ops1, ensemble_ops2) self.assertEqual(iteration_ops1, iteration_ops2) for ops in [ensemble_ops1, subnetwork_ops1, iteration_ops1]: self.assertIsNotNone(ops)
def test_subnetwork_metrics(self, use_tpu): with context.graph_mode(): self.setup_graph() spec = self._estimator_spec if not use_tpu: spec = spec.as_estimator_spec() metrics = tu.create_subnetwork_metrics(self._metric_fn, use_tpu=use_tpu, features=self._features, labels=self._labels, estimator_spec=spec) actual = self._run_metrics(metrics.eval_metrics_tuple()) expected = {"loss": 2., "metric_1": 1., "metric_2": 2.} self.assertEqual(actual, expected)
def test_subnetwork_metrics_user_metric_fn_overrides_metrics(self): with context.graph_mode(): self.setup_graph() overridden_value = 100. def _overriding_metric_fn(): value = tf.constant(overridden_value) return {"metric_1": tf_compat.v1.metrics.mean(value)} metrics = tu.create_subnetwork_metrics( _overriding_metric_fn, features=self._features, labels=self._labels, estimator_spec=self._estimator_spec) actual = self._run_metrics(metrics.eval_metrics_tuple()) expected = {"loss": 2., "metric_1": overridden_value} self.assertEqual(actual, expected)