示例#1
0
    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))
示例#2
0
 def test_predict_label_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})
     predicted_labels = gp_repurposer._predict_label_from_features(self.test_features
                                                                   [:self.num_data_points_to_predict])
     self._validate_prediction_results(predicted_labels, test_predict_probability=False,
                                       expected_accuracy=self.expected_accuracy_from_features,
                                       num_predictions=self.num_data_points_to_predict)