def _test_train_model_from_features(self, sparse_gp, multiple_kernels): gp_repurposer = GpRepurposer(self.source_model, self.source_model_layers) num_inducing = self.num_data_points_to_train gp_repurposer.NUM_INDUCING_SPARSE_GP = num_inducing if not sparse_gp: # Select a small data set to apply normal GP classification self.train_features = self.train_features[:num_inducing] self.train_labels = self.train_labels[:num_inducing] self.feature_mean = self.train_features.mean(axis=0) if multiple_kernels: trained_model = gp_repurposer._train_model_from_features( self.train_features, self.train_labels, { 'l1': self.train_feature_indices[:4], 'l2': self.train_feature_indices[4:] }) else: trained_model = gp_repurposer._train_model_from_features( self.train_features, self.train_labels, {'l1': self.train_feature_indices}) assert np.array_equal(gp_repurposer.feature_mean, self.feature_mean) self._validate_trained_gp_model(trained_model, sparse_gp, num_inducing, multiple_kernels)
def test_binary_classification(self): train_features = np.array([[0.0286274, 0.41107054, 0.30557073], [0.18646135, 0.71026038, 0.87030804], [0.46904668, 0.96190886, 0.85772885], [0.40327128, 0.5739354, 0.21895921], [0.53548, 0.9645708, 0.56493308], [0.80917639, 0.78891976, 0.96257564], [0.10951679, 0.75733494, 0.10935291]]) train_labels = np.array([0, 0, 0, 1, 0, 0, 1]) gp_repurposer = GpRepurposer(self.source_model, self.source_model_layers) gp_repurposer.target_model = gp_repurposer._train_model_from_features( train_features, train_labels, {'l1': np.arange(0, 3)}) self.assertTrue( len(gp_repurposer.target_model) == 1, "Number of GP models expected: 1. Got: {}".format( len(gp_repurposer.target_model))) # Validate predicted probabilities test_features = np.array([[0.63747595, 0.86516482, 0.21255967], [0.33403457, 0.43162212, 0.77119909], [0.1678248, 0.41870605, 0.37232554]]) test_labels = np.array([1, 0, 0]) expected_probabilities = np.array([[0.48597323, 0.51402677], [0.67488224, 0.32511776], [0.55386502, 0.44613498]]) predicted_probabilities = gp_repurposer._predict_probability_from_features( test_features) self.assertTrue( np.allclose(predicted_probabilities, expected_probabilities)) # Validate predicted labels predicted_labels = gp_repurposer._predict_label_from_features( test_features) self.assertTrue(np.array_equal(predicted_labels, test_labels))
def test_repurpose(self, mock_model_handler): # Patch model_handler and then create gp_repurposer mock_model_handler.return_value = RepurposerTestUtils.get_mock_model_handler_object() mock_model_handler.return_value.get_layer_output.return_value = {'l1': self.train_features}, self.train_labels gp_repurposer = GpRepurposer(self.source_model, self.source_model_layers) gp_repurposer.NUM_INDUCING_SPARSE_GP = 5 # To speed-up unit test running time self._run_common_repurposer_tests(gp_repurposer)
def _test_gp_serialisation(self, sparse_gp, multiple_kernels): gp_repurposer = GpRepurposer(self.source_model, self.source_model_layers, apply_l2_norm=True) num_inducing = 2 gp_repurposer.NUM_INDUCING_SPARSE_GP = num_inducing if not sparse_gp: # Select a small data set to apply normal GP classification self.train_features = self.train_features[:num_inducing] self.train_labels = self.train_labels[:num_inducing] self.feature_mean = self.train_features.mean(axis=0) if multiple_kernels: gp_repurposer.target_model = gp_repurposer._train_model_from_features( self.train_features, self.train_labels, { 'l1': self.train_feature_indices[:4], 'l2': self.train_feature_indices[4:] }) else: gp_repurposer.target_model = gp_repurposer._train_model_from_features( self.train_features, self.train_labels, {'l1': self.train_feature_indices}) # Save and load repurposer to test serialization loaded_repurposer = self._save_and_load_repurposer(gp_repurposer) # Validate repurposer properties self._compare_gp_repurposers(gp_repurposer, loaded_repurposer) # Get prediction results using both repurposers predictions_before = gp_repurposer._predict_probability_from_features( self.test_features[:self.num_data_points_to_predict]) predictions_after = loaded_repurposer._predict_probability_from_features( self.test_features[:self.num_data_points_to_predict]) # Compare probabilities predicted per test instance self.assertTrue( predictions_before.shape == predictions_after.shape, "Prediction shape is incorrect. Expected: {} Actual: {}".format( predictions_before.shape, predictions_after.shape)) for sample_id, prediction in enumerate(predictions_before): self.assertTrue( np.allclose(prediction, predictions_after[sample_id]), "Incorrect prediction for sample id: {}. Expected: {} Actual: {}" .format(sample_id, predictions_before[sample_id], predictions_after[sample_id])) # Validate if accuracy is above expected threshold predicted_labels = np.argmax(predictions_after, axis=1) accuracy = np.mean(predicted_labels == self.test_labels[:self.num_data_points_to_predict]) expected_accuracy = 0.3 self.assertTrue( accuracy >= expected_accuracy, "Accuracy {} less than {}".format(accuracy, expected_accuracy))
def test_predict_probability_from_features(self): gp_repurposer = GpRepurposer(self.source_model, self.source_model_layers, apply_l2_norm=True) gp_repurposer.target_model = gp_repurposer._train_model_from_features( self.train_features[:self.num_data_points_to_train], self.train_labels[:self.num_data_points_to_train], {'l1': self.train_feature_indices}) predictions = gp_repurposer._predict_probability_from_features(self.test_features [:self.num_data_points_to_predict]) self._validate_prediction_results(predictions, test_predict_probability=True, expected_accuracy=self.expected_accuracy_from_features, num_predictions=self.num_data_points_to_predict)