-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
57 lines (53 loc) · 1.8 KB
/
test.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
"use strict"
const test = require("tape")
const ftrl = require(".")
const rand = require("prob.js")
test("fit small data", (t) => {
const optimizer = ftrl(3)
t.equal(optimizer.weights()[0], 0)
t.equal(optimizer.weights()[1], 0)
t.equal(optimizer.weights()[2], 0)
optimizer.fit([1, 2, 3], 1)
t.equal(optimizer.weights()[0], 0.03333333333333333)
t.equal(optimizer.weights()[1], 0.05)
t.equal(optimizer.weights()[2], 0.06)
t.end()
})
test("fit random data", (t) => {
// here we learn if the first component of the training vector is negative
// the other two components are irrelevant
const rnorm = rand.normal(0, 1)
const optimizer = ftrl(3, 10, 1)
for (let i = 0; i < 10000; i++) {
const x = rnorm()
optimizer.fit([x, rnorm(), rnorm()], x < 0 ? 1 : 0)
}
t.true(optimizer.predict([1, 0, 3]) < 0.2)
t.true(optimizer.predict([-1, 0, 3]) > 0.8)
t.end()
})
test("save/load model", (t) => {
const optimizer = ftrl(3, 1, 2, 0.5, 2)
optimizer.fit([1, 2, 3], 1)
const weights = optimizer.weights()
const saved = optimizer.save()
const newOptimizer = ftrl(10, 20, 30, 40, 50)
newOptimizer.load(saved)
const newWeights = newOptimizer.weights()
for (let i = 0; i < 3; i++) {
t.equal(weights[i], newWeights[i])
}
t.equal(optimizer.predict([1, 2, 3]), newOptimizer.predict([1, 2, 3]))
const newSaved = newOptimizer.save()
t.equal(newSaved.config.lambda1, saved.config.lambda1)
t.equal(newSaved.config.lambda2, saved.config.lambda2)
t.equal(newSaved.config.alpha, saved.config.alpha)
t.equal(newSaved.config.beta, saved.config.beta)
t.equal(newSaved.config.n_features, saved.config.n_features)
for (let i = 0; i < 3; i++) {
t.equal(newSaved.z[i], saved.z[i])
t.equal(newSaved.n[i], saved.n[i])
t.equal(newSaved.lastWeights[i], saved.lastWeights[i])
}
t.end()
})