-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpredictors.js
160 lines (143 loc) · 3.95 KB
/
predictors.js
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
const path = require("path");
const tf = require("@tensorflow/tfjs-node");
class PredictorBase {
constructor(modelDir) {
this.modelDir = modelDir;
this.modelName = path.basename(modelDir);
this.model = null;
}
async loadModel() {
this.model = await tf.node.loadSavedModel(this.modelDir);
}
preProcessInput(input) {
throw new Error("Not Implemented");
}
/**
* Wraps the feature values as tensor arrays
* Types are inferred by tfjs
* Keys to input should match with what the model expects
* @param {object} input final input dictionary ready to be passed to the model
* @returns a dictionary of tensor arrays as required by the model
*/
prepareX(input) {
const x = {};
for (const feature of Object.keys(input)) {
const value = input[feature];
x[feature] = tf.tensor([value], [1, 1]); // explicitly ensure it is not a rank 0 tensor
}
return x;
}
/**
* Process the input and make predictions on it
* @param {object} rawInput {[name: string]: tf.Tensor} dictionary
* @returns {class1: probability1, class2: probability2, ...}
*/
predict(rawInput) {
if (!this.model) {
throw new Error(
`Model '${this.modelName}' is not loaded. Please load it first.`
);
}
const result = tf.tidy(() => {
const input = this.preProcessInput(rawInput);
const x = this.prepareX(input);
const output = this.model.predict(x, {});
const probs = Array.from(output.probabilities.dataSync());
const classes = Array.from(output.all_class_ids.dataSync());
const result = Object.fromEntries(
classes.map((classId, i) => [classId, probs[i]])
);
return result;
});
for (const [key, value] of Object.entries(result)) {
if (typeof value !== "number" || isNaN(value)) {
throw new Error(
`Invalid confidence score (${value}) causing bad prediction result: ${JSON.stringify(
result
)}`
);
}
}
return result;
}
}
class MWebJan2022Predictor extends PredictorBase {
constructor(modelDir) {
super(modelDir);
this._features = [
"asn_number",
"browser_major_version",
"browser_major_version_na",
"browser_name",
"country_code",
"osfamily",
"osmajor",
"osmajor_na"
];
this._defaults = {
"browser_major_version": 15.0,
"osmajor": 14.0,
"asn_number": '**',
"country_code": '**',
"browser_name": '**',
"osfamily": '**'
};
this._normalizer = {
"means": { "browser_major_version": 52.65782220933843, "osmajor": 13.372263709715911 },
"stds": { "browser_major_version": 41.48294747389074, "osmajor": 2.376855002582524 }
};
}
async loadModel() {
this.model = await tf.node.loadSavedModel(
this.modelDir,
["serve"],
"predict"
);
}
_normalizeNumericalFetaures(x) {
const { means, stds } = this._normalizer;
for (const feature in means) {
x[feature] = (parseFloat(x[feature]) - means[feature]) / stds[feature];
}
return x;
}
_checkNA(value) {
return (
value === null ||
value === undefined ||
value < 0 ||
value === "" ||
value === "unknown"
);
}
_fillNA(x) {
for (const feature of Object.keys(x)) {
if (this._checkNA(x[feature])) {
x[feature] = this._defaults[feature];
}
}
return x;
}
_addNAFetaures(x) {
x.browser_major_version_na = "False";
x.osmajor_na = "False";
if (this._checkNA(x.browser_major_version)) {
x.browser_major_version_na = "True";
}
if (this._checkNA(x.osmajor)) {
x.osmajor_na = "True";
}
return x;
}
preProcessInput(input) {
let x = {};
for (const feature of this._features) {
x[feature] = input[feature];
}
x = this._addNAFetaures(x);
x = this._fillNA(x);
x = this._normalizeNumericalFetaures(x);
return x;
}
}
module.exports = { MWebJan2022Predictor };