def build_and_test_estimator(self, model_type): """Ensure that model trains and minimizes loss.""" model = wide_deep.build_estimator(self.temp_dir, model_type) # Train for 1 step to initialize model and evaluate initial loss model.train(input_fn=lambda: wide_deep.input_fn( TEST_CSV, num_epochs=1, shuffle=True, batch_size=1), steps=1) initial_results = model.evaluate(input_fn=lambda: wide_deep.input_fn( TEST_CSV, num_epochs=1, shuffle=False, batch_size=1)) # Train for 100 epochs at batch size 3 and evaluate final loss model.train(input_fn=lambda: wide_deep.input_fn( TEST_CSV, num_epochs=100, shuffle=True, batch_size=3)) final_results = model.evaluate(input_fn=lambda: wide_deep.input_fn( TEST_CSV, num_epochs=1, shuffle=False, batch_size=1)) print('%s initial results:' % model_type, initial_results) print('%s final results:' % model_type, final_results) # Ensure loss has decreased, while accuracy and both AUCs have increased. self.assertLess(final_results['loss'], initial_results['loss']) self.assertGreater(final_results['auc'], initial_results['auc']) self.assertGreater(final_results['auc_precision_recall'], initial_results['auc_precision_recall']) self.assertGreater(final_results['accuracy'], initial_results['accuracy'])
def test_input_fn(self): dataset = wide_deep.input_fn(self.input_csv, 1, False, 1) features, labels = dataset.make_one_shot_iterator().get_next() with tf.Session() as sess: features, labels = sess.run((features, labels)) # Compare the two features dictionaries. for key in TEST_INPUT_VALUES: self.assertTrue(key in features) self.assertEqual(len(features[key]), 1) feature_value = features[key][0] # Convert from bytes to string for Python 3. if isinstance(feature_value, bytes): feature_value = feature_value.decode() self.assertEqual(TEST_INPUT_VALUES[key], feature_value) self.assertFalse(labels)
def test_input_fn(self): dataset = wide_deep.input_fn(self.input_csv, 1, False, 1) features, labels = dataset.make_one_shot_iterator().get_next() with tf.Session() as sess: features, labels = sess.run((features, labels)) # Compare the two features dictionaries. for key in TEST_INPUT_VALUES: self.assertTrue(key in features) self.assertEqual(len(features[key]), 1) feature_value = features[key][0] # Convert from bytes to string for Python 3. if isinstance(feature_value, bytes): feature_value = feature_value.decode() self.assertEqual(TEST_INPUT_VALUES[key], feature_value) self.assertFalse(labels)
def input_fn(): return wide_deep.input_fn(TEST_CSV, num_epochs=num_epochs, shuffle=shuffle, batch_size=batch_size)
def input_fn(): return wide_deep.input_fn( TEST_CSV, num_epochs=num_epochs, shuffle=shuffle, batch_size=batch_size)