forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_classifier_predictor_unittest.cc
208 lines (174 loc) · 7.52 KB
/
binary_classifier_predictor_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/assist_ranker/binary_classifier_predictor.h"
#include <memory>
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/feature_list.h"
#include "base/metrics/field_trial_params.h"
#include "base/test/scoped_feature_list.h"
#include "components/assist_ranker/fake_ranker_model_loader.h"
#include "components/assist_ranker/proto/ranker_model.pb.h"
#include "components/assist_ranker/ranker_model.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace assist_ranker {
using ::assist_ranker::testing::FakeRankerModelLoader;
class BinaryClassifierPredictorTest : public ::testing::Test {
public:
void SetUp() override;
std::unique_ptr<BinaryClassifierPredictor> InitPredictor(
std::unique_ptr<RankerModel> ranker_model,
const PredictorConfig& config);
// This model will return the value of |feature| as a prediction.
GenericLogisticRegressionModel GetSimpleLogisticRegressionModel();
PredictorConfig GetConfig();
PredictorConfig GetConfig(float predictor_threshold_replacement);
protected:
const std::string feature_ = "feature";
const float weight_ = 1.0;
const float threshold_ = 0.5;
base::test::ScopedFeatureList scoped_feature_list_;
};
void BinaryClassifierPredictorTest::SetUp() {
::testing::Test::SetUp();
scoped_feature_list_.Init();
}
std::unique_ptr<BinaryClassifierPredictor>
BinaryClassifierPredictorTest::InitPredictor(
std::unique_ptr<RankerModel> ranker_model,
const PredictorConfig& config) {
std::unique_ptr<BinaryClassifierPredictor> predictor(
new BinaryClassifierPredictor(config));
auto fake_model_loader = std::make_unique<FakeRankerModelLoader>(
base::BindRepeating(&BinaryClassifierPredictor::ValidateModel),
base::BindRepeating(&BinaryClassifierPredictor::OnModelAvailable,
base::Unretained(predictor.get())),
std::move(ranker_model));
predictor->LoadModel(std::move(fake_model_loader));
return predictor;
}
BASE_FEATURE(kTestRankerQuery,
"TestRankerQuery",
base::FEATURE_ENABLED_BY_DEFAULT);
const base::FeatureParam<std::string> kTestRankerUrl{
&kTestRankerQuery, "url-param-name", "https://default.model.url"};
PredictorConfig BinaryClassifierPredictorTest::GetConfig() {
return GetConfig(kNoPredictThresholdReplacement);
}
PredictorConfig BinaryClassifierPredictorTest::GetConfig(
float predictor_threshold_replacement) {
PredictorConfig config("model_name", "logging_name", "uma_prefix", LOG_NONE,
GetEmptyAllowlist(), &kTestRankerQuery,
&kTestRankerUrl, predictor_threshold_replacement);
return config;
}
GenericLogisticRegressionModel
BinaryClassifierPredictorTest::GetSimpleLogisticRegressionModel() {
GenericLogisticRegressionModel lr_model;
lr_model.set_bias(-0.5);
lr_model.set_threshold(threshold_);
(*lr_model.mutable_weights())[feature_].set_scalar(weight_);
return lr_model;
}
// TODO(hamelphi): Test BinaryClassifierPredictor::Create.
TEST_F(BinaryClassifierPredictorTest, EmptyRankerModel) {
auto ranker_model = std::make_unique<RankerModel>();
auto predictor = InitPredictor(std::move(ranker_model), GetConfig());
EXPECT_FALSE(predictor->IsReady());
RankerExample ranker_example;
auto& features = *ranker_example.mutable_features();
features[feature_].set_bool_value(true);
bool bool_response;
EXPECT_FALSE(predictor->Predict(ranker_example, &bool_response));
float float_response;
EXPECT_FALSE(predictor->PredictScore(ranker_example, &float_response));
}
TEST_F(BinaryClassifierPredictorTest, NoInferenceModuleForModel) {
auto ranker_model = std::make_unique<RankerModel>();
// TranslateRankerModel does not have an inference module. Validation will
// fail.
ranker_model->mutable_proto()
->mutable_translate()
->mutable_translate_logistic_regression_model()
->set_bias(1);
auto predictor = InitPredictor(std::move(ranker_model), GetConfig());
EXPECT_FALSE(predictor->IsReady());
RankerExample ranker_example;
auto& features = *ranker_example.mutable_features();
features[feature_].set_bool_value(true);
bool bool_response;
EXPECT_FALSE(predictor->Predict(ranker_example, &bool_response));
float float_response;
EXPECT_FALSE(predictor->PredictScore(ranker_example, &float_response));
}
TEST_F(BinaryClassifierPredictorTest, GenericLogisticRegressionModel) {
auto ranker_model = std::make_unique<RankerModel>();
*ranker_model->mutable_proto()->mutable_logistic_regression() =
GetSimpleLogisticRegressionModel();
auto predictor = InitPredictor(std::move(ranker_model), GetConfig());
EXPECT_TRUE(predictor->IsReady());
RankerExample ranker_example;
auto& features = *ranker_example.mutable_features();
features[feature_].set_bool_value(true);
bool bool_response;
EXPECT_TRUE(predictor->Predict(ranker_example, &bool_response));
EXPECT_TRUE(bool_response);
float float_response;
EXPECT_TRUE(predictor->PredictScore(ranker_example, &float_response));
EXPECT_GT(float_response, threshold_);
features[feature_].set_bool_value(false);
EXPECT_TRUE(predictor->Predict(ranker_example, &bool_response));
EXPECT_FALSE(bool_response);
EXPECT_TRUE(predictor->PredictScore(ranker_example, &float_response));
EXPECT_LT(float_response, threshold_);
}
TEST_F(BinaryClassifierPredictorTest,
GenericLogisticRegressionPreprocessedModel) {
auto ranker_model = std::make_unique<RankerModel>();
auto& glr = *ranker_model->mutable_proto()->mutable_logistic_regression();
glr = GetSimpleLogisticRegressionModel();
glr.clear_weights();
glr.set_is_preprocessed_model(true);
(*glr.mutable_fullname_weights())[feature_] = weight_;
auto predictor = InitPredictor(std::move(ranker_model), GetConfig());
EXPECT_TRUE(predictor->IsReady());
RankerExample ranker_example;
auto& features = *ranker_example.mutable_features();
features[feature_].set_bool_value(true);
bool bool_response;
EXPECT_TRUE(predictor->Predict(ranker_example, &bool_response));
EXPECT_TRUE(bool_response);
float float_response;
EXPECT_TRUE(predictor->PredictScore(ranker_example, &float_response));
EXPECT_GT(float_response, threshold_);
features[feature_].set_bool_value(false);
EXPECT_TRUE(predictor->Predict(ranker_example, &bool_response));
EXPECT_FALSE(bool_response);
EXPECT_TRUE(predictor->PredictScore(ranker_example, &float_response));
EXPECT_LT(float_response, threshold_);
}
TEST_F(BinaryClassifierPredictorTest,
GenericLogisticRegressionPreprocessedModelReplacedThreshold) {
auto ranker_model = std::make_unique<RankerModel>();
auto& glr = *ranker_model->mutable_proto()->mutable_logistic_regression();
glr = GetSimpleLogisticRegressionModel();
glr.clear_weights();
glr.set_is_preprocessed_model(true);
(*glr.mutable_fullname_weights())[feature_] = weight_;
float high_threshold = 0.9; // Some high threshold.
auto predictor =
InitPredictor(std::move(ranker_model), GetConfig(high_threshold));
EXPECT_TRUE(predictor->IsReady());
RankerExample ranker_example;
auto& features = *ranker_example.mutable_features();
features[feature_].set_bool_value(true);
bool bool_response;
EXPECT_TRUE(predictor->Predict(ranker_example, &bool_response));
EXPECT_FALSE(bool_response);
float float_response;
EXPECT_TRUE(predictor->PredictScore(ranker_example, &float_response));
EXPECT_GT(float_response, threshold_);
EXPECT_LT(float_response, high_threshold);
}
} // namespace assist_ranker