def testCalibratedRtlMonotonicClassifierTraining(self): # Construct the following training/testing pair. # # Training: (x, y) # ([0., 0.], 0.0) # ([0., 1.], 1.0) # ([1., 0.], 1.0) # ([1., 1.], 0.0) # # Test: (x, y) # ([0., 0.], 0.0) # ([0., 1.], 1.0) # ([1., 0.], 1.0) # ([1., 1.], 1.0) # # Note that training example has a noisy sample, ([1., 1.], 0.0), and test # examples are generated by the logical-OR function. Therefore by enforcing # increasing monotonicity to all features, we should be able to work well # in the test examples. x0 = np.array([0.0, 0.0, 1.0, 1.0]) x1 = np.array([0.0, 1.0, 0.0, 1.0]) x_samples = {'x0': x0, 'x1': x1} training_y = np.array([[False], [True], [True], [False]]) test_y = np.array([[False], [True], [True], [True]]) train_input_fn = numpy_io.numpy_input_fn( x=x_samples, y=training_y, batch_size=4, num_epochs=1000, shuffle=False) test_input_fn = numpy_io.numpy_input_fn( x=x_samples, y=test_y, shuffle=False) # Define monotonic lattice classifier. feature_columns = [ feature_column_lib.numeric_column('x0'), feature_column_lib.numeric_column('x1'), ] def init_fn(): return keypoints_initialization.uniform_keypoints_for_signal( 2, 0., 1., 0., 1.) hparams = tfl_hparams.CalibratedRtlHParams( num_keypoints=2, num_lattices=3, lattice_rank=2) # Monotonic calibrated lattice. hparams.set_param('monotonicity', +1) hparams.set_param('learning_rate', 0.1) hparams.set_param('interpolation_type', 'hypercube') estimator = scrtl.separately_calibrated_rtl_classifier( feature_columns=feature_columns, hparams=hparams, keypoints_initializers_fn=init_fn) estimator.train(input_fn=train_input_fn) results = estimator.evaluate(input_fn=test_input_fn) # We should expect 1.0 accuracy. self.assertGreater(results['accuracy'], 0.999)
def setUp(self): self.hparams = tfl_hparams.CalibratedRtlHParams(feature_names=['x']) self.hparams.set_param('lattice_size', 2) self.hparams.set_param('calibrator_output_min', 0) self.hparams.set_param('calibrator_output_max', 1) self.hparams.set_param('calibration_bound', True) self.hparams.set_param('lattice_rank', 2) self.hparams.set_param('num_lattices', 10) self.empty_estimator = scrtl.separately_calibrated_rtl_classifier( hparams=self.hparams)
def _CalibratedRtlClassifier(self, feature_columns, num_lattices=1, lattice_rank=1, **hparams_args): def init_fn(): return keypoints_initialization.uniform_keypoints_for_signal( _NUM_KEYPOINTS, -1., 1., 0., 1.) hparams = tfl_hparams.CalibratedRtlHParams( num_keypoints=_NUM_KEYPOINTS, num_lattices=num_lattices, lattice_rank=lattice_rank, **hparams_args) # Turn off monotonic calibrator. hparams.set_param('calibration_monotonic', None) hparams.set_param('learning_rate', 0.1) return scrtl.separately_calibrated_rtl_classifier( feature_columns=feature_columns, hparams=hparams, keypoints_initializers_fn=init_fn)