-
Notifications
You must be signed in to change notification settings - Fork 38
/
lbfgs.js
104 lines (87 loc) · 3.34 KB
/
lbfgs.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
/*
* Copyright 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function exit() {
process.exit();
}
function stop(e) {
if (e) {
console.log(e);
}
sc.stop().then(exit).catch(exit);
}
function run(sc, spark) {
return new Promise(function(resolve, reject) {
var data = spark.mllib.util.MLUtils.loadLibSVMFile(sc, __dirname + "/data/sample_binary_classification_data.txt");
var ret = {};
data.take(1).then(function(results) {
var numFeatures = results[0].features.size;
// Split initial RDD into two... [60% training data, 40% testing data].
var trainingInit = data.sample(false, 0.6, 11);
var test = data.subtract(trainingInit);
// Append 1 into the training data as intercept.
var training = data.map(function (lp, Tuple2, MLUtils) {
return new Tuple2(lp.getLabel(), MLUtils.appendBias(lp.getFeatures()));
}, [spark.Tuple2, spark.mllib.util.MLUtils]);
training.cache();
// Run training algorithm to build the model.
var numCorrections = 10;
var convergenceTol = 0.0001;
var maxNumIterations = 20;
var regParam = 0.1;
var w = [];
for (var i = 0; i < numFeatures + 1; i++) {
w.push(0.0);
}
var initialWeightsWithIntercept = spark.mllib.linalg.Vectors.dense(w);
var run = spark.mllib.optimization.LBFGS.runLBFGS(
training,
new spark.mllib.optimization.LogisticGradient(),
new spark.mllib.optimization.SquaredL2Updater(),
numCorrections,
convergenceTol,
maxNumIterations,
regParam,
initialWeightsWithIntercept);
run.then(function(result) {
var weightsWithIntercept = result[0];
var loss = result[1];
var copyOfWeightsWithIntercept = [];
for (var i = 0; i < weightsWithIntercept.values.length - 1; i++) {
copyOfWeightsWithIntercept.push(weightsWithIntercept.values[i]);
}
var model = new spark.mllib.classification.LogisticRegressionModel(spark.mllib.linalg.Vectors.dense(copyOfWeightsWithIntercept), copyOfWeightsWithIntercept.length);
var scoreAndLabels = test.map(function (lp, model, Tuple2) {
return new Tuple2(model.predict(lp.getFeatures()), lp.getLabel());
}, [model, spark.Tuple2]);
// Get evaluation metrics.
var metrics = new spark.mllib.evaluation.BinaryClassificationMetrics(scoreAndLabels);
metrics.areaUnderROC().then(resolve).catch(reject);
}).catch(reject);
}).catch(reject);
});
}
if (global.SC) {
// we are being run as part of a test
module.exports = run;
} else {
var eclairjs = require('../../lib/index.js');
var spark = new eclairjs();
var sc = new spark.SparkContext("local[*]", "LBFGS");
run(sc, spark).then(function(result) {
console.log('Area under ROC:', result);
stop();
}).catch(stop);
}