-
Notifications
You must be signed in to change notification settings - Fork 17
/
dinimpl_test.go
86 lines (79 loc) · 2.05 KB
/
dinimpl_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package movielens
import (
"context"
"fmt"
"math/rand"
"testing"
rcmd "github.com/auxten/go-ctr/recommend"
"github.com/auxten/go-ctr/utils"
. "github.com/smartystreets/goconvey/convey"
)
type dnnPredictor struct {
rcmd.PreRanker
rcmd.Predictor
rcmd.UserBehavior
}
func TestDinOnMovielens(t *testing.T) {
rand.Seed(42)
var (
movielens = &MovielensRec{
//DataPath: "movielens-20m.db",
//SampleCnt: 14400000,
DataPath: "movielens.db",
SampleCnt: 79948,
//SampleCnt: 10000,
}
model rcmd.Predictor
err error
)
Convey("Train din model", t, func() {
dinModel := &dinImpl{
//PredBatchSize: 5000,
PredBatchSize: 100,
//BatchSize: 5000,
BatchSize: 200,
epochs: 200,
earlyStop: 20,
}
trainCtx := context.Background()
model, err = rcmd.Train(trainCtx, movielens, dinModel)
So(err, ShouldBeNil)
So(model, ShouldNotBeNil)
})
Convey("Predict din model", t, func() {
//testCount := 5610000
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 float32
timestamp int64
yTrue []float32
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))
yTrue = append(yTrue, BinarizeLabel32(rating))
sampleKeys = append(sampleKeys, rcmd.Sample{userId, itemId, 0, timestamp})
}
batchPredictCtx := context.Background()
dinPred := &dnnPredictor{
PreRanker: movielens,
Predictor: model,
UserBehavior: movielens,
}
yPred, err := rcmd.BatchPredict(batchPredictCtx, dinPred, sampleKeys)
So(err, ShouldBeNil)
rocAuc := utils.RocAuc32(yPred.Data().([]float32), yTrue)
//rocAuc := metrics.ROCAUCScore(yTrue, yPred, "", nil)
rowCount := len(yTrue)
fmt.Printf("rocAuc on test set %d: %f\n", rowCount, rocAuc)
})
}