-
Notifications
You must be signed in to change notification settings - Fork 17
/
mlpimpl_test.go
72 lines (65 loc) · 1.74 KB
/
mlpimpl_test.go
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
package movielens
import (
"context"
"fmt"
"math/rand"
"testing"
"github.com/auxten/edgeRec/nn/metrics"
rcmd "github.com/auxten/edgeRec/recommend"
. "github.com/smartystreets/goconvey/convey"
"gonum.org/v1/gonum/mat"
)
func TestSimpleMLPOnMovielens(t *testing.T) {
rand.Seed(42)
rcmd.DebugUserId = 429
//rcmd.DebugItemId = 588
var (
movielens = &MovielensRec{
DataPath: "movielens.db",
SampleCnt: 79948,
//SampleCnt: 10000,
}
model rcmd.Predictor
err error
)
Convey("Train din model", t, func() {
mlpImpl := &mlpImpl{
predBatchSize: 100,
batchSize: 200,
epochs: 200,
earlyStop: 20,
}
trainCtx := context.Background()
model, err = rcmd.Train(trainCtx, movielens, mlpImpl)
So(err, ShouldBeNil)
So(model, ShouldNotBeNil)
})
Convey("Predict din model", t, func() {
testCount := 20600
rows, err := db.Query(
"SELECT userId, movieId, rating, timestamp FROM ratings_test ORDER BY timestamp, userId ASC LIMIT ?", testCount)
So(err, ShouldBeNil)
var (
userId int
itemId int
rating float64
timestamp int64
yTrue = mat.NewDense(testCount, 1, nil)
sampleKeys = make([]rcmd.Sample, 0, testCount)
)
for i := 0; rows.Next(); i++ {
err = rows.Scan(&userId, &itemId, &rating, ×tamp)
if err != nil {
t.Errorf("scan error: %v", err)
}
yTrue.Set(i, 0, BinarizeLabel(rating))
sampleKeys = append(sampleKeys, rcmd.Sample{userId, itemId, 0, timestamp})
}
batchPredictCtx := context.Background()
yPred, err := rcmd.BatchPredict(batchPredictCtx, model, sampleKeys)
So(err, ShouldBeNil)
rocAuc := metrics.ROCAUCScore(yTrue, yPred, "", nil)
rowCount, _ := yTrue.Dims()
fmt.Printf("rocAuc on test set %d: %f\n", rowCount, rocAuc)
})
}