def test_lifecycle(self, loss, subnetwork_generator, max_iteration_steps, want_loss, want_metrics=None, want_metrics_names=None, metrics=None, logits_dimension=1, ensemblers=None, ensemble_strategies=None, evaluator=None, adanet_loss_decay=0.9, dataset=None, epochs=None, steps_per_epoch=None): keras_model = model.Model(subnetwork_generator=subnetwork_generator, max_iteration_steps=max_iteration_steps, logits_dimension=logits_dimension, ensemblers=ensemblers, ensemble_strategies=ensemble_strategies, evaluator=evaluator, adanet_loss_decay=adanet_loss_decay, filepath=self.test_subdirectory) keras_model.compile(loss=loss, metrics=metrics) if want_metrics_names is None: want_metrics_names = ["loss"] # Make sure we have access to metrics_names immediately after compilation. self.assertEqual(want_metrics_names, keras_model.metrics_names) if dataset is None: dataset = lambda: tf.data.Dataset.from_tensors( # pylint: disable=g-long-lambda ({ "x": XOR_FEATURES }, XOR_LABELS)).repeat() keras_model.fit(dataset, epochs=epochs, steps_per_epoch=steps_per_epoch) eval_results = keras_model.evaluate(dataset, steps=3) self.assertAlmostEqual(want_loss, eval_results[0], places=3) if metrics: self.assertAllClose(want_metrics, eval_results[1:], 1e-3, 1e-3) # TODO: Predict not currently working for BinaryClassHead and # MultiClassHead. if loss == "mse": prediction_data = lambda: tf.data.Dataset.from_tensors( ({ # pylint: disable=g-long-lambda "x": XOR_FEATURES })) predictions = keras_model.predict(prediction_data) self.assertLen(predictions, 4)
def test_loss_exceptions(self): """Check that ValueError is raised when from_logits=False for loss.""" keras_model = model.Model(subnetwork_generator=SimpleGenerator( [_DNNBuilder("dnn")]), max_iteration_steps=1) loss = tf.keras.losses.BinaryCrossentropy(from_logits=False) with self.assertRaises(ValueError): keras_model.compile(loss=loss)
def test_compile_exceptions(self): keras_model = model.Model(subnetwork_generator=SimpleGenerator( [_DNNBuilder("dnn")]), max_iteration_steps=1) train_data = tf.data.Dataset.from_tensors(([[1., 1.]], [[1.]])) predict_data = tf.data.Dataset.from_tensors(([[1., 1.]])) with self.assertRaises(RuntimeError): keras_model.fit(train_data) with self.assertRaises(RuntimeError): keras_model.evaluate(train_data) with self.assertRaises(RuntimeError): keras_model.predict(predict_data)
def test_lifecycle(self, subnetwork_generator, max_iteration_steps, want_loss, ensemblers=None, ensemble_strategies=None, evaluator=None, adanet_loss_decay=0.9, dataset=None, epochs=None, steps_per_epoch=None): keras_model = model.Model(subnetwork_generator=subnetwork_generator, max_iteration_steps=max_iteration_steps, ensemblers=ensemblers, ensemble_strategies=ensemble_strategies, evaluator=evaluator, adanet_loss_decay=adanet_loss_decay, filepath=self.test_subdirectory) keras_model.compile(loss="mse") # Make sure we have access to metrics_names immediately after compilation. self.assertEqual(["loss"], keras_model.metrics_names) if dataset is None: dataset = tf.data.Dataset.from_tensors( (XOR_FEATURES, XOR_LABELS)).repeat() keras_model.fit(dataset, epochs=epochs, steps_per_epoch=steps_per_epoch) eval_results = keras_model.evaluate(dataset, steps=3) self.assertAlmostEqual(want_loss, eval_results["loss"], places=3) prediction_data = tf.data.Dataset.from_tensors((XOR_FEATURES)) predictions = keras_model.predict(prediction_data) self.assertLen(predictions, 4)