Repository navigation
Expand file tree
/
Copy pathdelegation_test.go
More file actions
122 lines (107 loc) · 3.79 KB
/
Copy pathdelegation_test.go
File metadata and controls
122 lines (107 loc) · 3.79 KB
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
package leaves
import (
"math"
"path/filepath"
"testing"
"github.com/linkerlin/leaves/v2/mat"
"github.com/linkerlin/leaves/v2/tree"
)
// TestEnsembleDelegatesToModel 根包 Predict* 经 model.Ensemble 代理后与旧实现一致。
func TestEnsembleDelegatesToModel(t *testing.T) {
modelPath := filepath.Join("testdata", "lg_breast_cancer.txt")
dataPath := filepath.Join("testdata", "lg_breast_cancer_data.txt")
ens, err := LGEnsembleFromFile(modelPath, false)
if err != nil {
t.Fatal(err)
}
testMat, err := mat.DenseMatFromCsvFile(dataPath, 0, false, " ", 0.0)
if err != nil {
t.Fatal(err)
}
// 默认委托开启后,PredictSingle 与显式 WithEngineOptions 应一致。
explicit := ens.WithEngineOptions(DefaultEngineOptions())
fvals := testMat.Values[:testMat.Cols]
got := ens.PredictSingle(fvals, 0)
want := explicit.PredictSingle(fvals, 0)
if math.Abs(got-want) > 1e-9 {
t.Errorf("default delegate=%v explicit=%v", got, want)
}
}
func TestEnsembleDelegateLeafIndex(t *testing.T) {
modelPath := filepath.Join("testdata", "lg_breast_cancer.txt")
dataPath := filepath.Join("testdata", "lg_breast_cancer_data.txt")
predLeavesPath := filepath.Join("testdata", "lg_breast_cancer_data_pred_leaves.txt")
ens, err := LGEnsembleFromFile(modelPath, false)
if err != nil {
t.Fatal(err)
}
ens = ens.EnsembleWithLeafPredictions()
testMat, err := mat.DenseMatFromCsvFile(dataPath, 0, false, " ", 0.0)
if err != nil {
t.Fatal(err)
}
wantMat, err := mat.DenseMatFromCsvFile(predLeavesPath, 0, false, " ", 0.0)
if err != nil {
t.Fatal(err)
}
got := make([]float64, testMat.Rows*ens.NOutputGroups())
if err := ens.PredictDense(testMat.Values, testMat.Rows, testMat.Cols, got, 0, 0); err != nil {
t.Fatal(err)
}
for row := 0; row < testMat.Rows; row++ {
for col := 0; col < wantMat.Cols; col++ {
g := got[row*wantMat.Cols+col]
w := wantMat.Values[row*wantMat.Cols+col]
if math.Abs(g-w) > 1e-6 {
t.Fatalf("leaf row=%d col=%d: got %v want %v", row, col, g, w)
}
}
}
}
func TestDelegationCSRMultiThread(t *testing.T) {
testPath := filepath.Join("testdata", "agaricus_test.libsvm")
modelPath := filepath.Join("testdata", "xgagaricus.model")
csr, err := mat.CSRMatFromLibsvmFile(testPath, 0, true)
if err != nil {
t.Skip(err)
}
legacy, err := XGEnsembleFromFile(modelPath, true)
if err != nil {
t.Fatal(err)
}
delegated := legacy.WithEngineOptions(DefaultEngineOptions())
for _, nThreads := range []int{1, 2, 4} {
legacyPred := make([]float64, csr.Rows())
delegPred := make([]float64, csr.Rows())
_ = legacy.PredictCSR(csr.RowHeaders, csr.ColIndexes, csr.Values, legacyPred, 0, nThreads)
_ = delegated.PredictCSR(csr.RowHeaders, csr.ColIndexes, csr.Values, delegPred, 0, nThreads)
for i := range legacyPred {
if math.Abs(legacyPred[i]-delegPred[i]) > 1e-6 {
t.Fatalf("nThreads=%d row %d: legacy=%f delegated=%f", nThreads, i, legacyPred[i], delegPred[i])
}
}
}
}
func TestWithEngineOptionsSimpleGo(t *testing.T) {
modelPath := filepath.Join("testdata", "lg_breast_cancer.txt")
dataPath := filepath.Join("testdata", "lg_breast_cancer_data.txt")
ens, err := LGEnsembleFromFile(modelPath, false)
if err != nil {
t.Fatal(err)
}
testMat, err := mat.DenseMatFromCsvFile(dataPath, 0, false, " ", 0.0)
if err != nil {
t.Fatal(err)
}
native := ens.WithEngineOptions(&EngineOptions{Backend: tree.BackendNative})
simple := ens.WithEngineOptions(&EngineOptions{Backend: tree.BackendBornCPU})
outN := make([]float64, testMat.Rows)
outS := make([]float64, testMat.Rows)
_ = native.PredictDense(testMat.Values, testMat.Rows, testMat.Cols, outN, 0, 0)
_ = simple.PredictDense(testMat.Values, testMat.Rows, testMat.Cols, outS, 0, 0)
for i := range outN {
if math.Abs(outN[i]-outS[i]) > 1e-5 {
t.Errorf("row %d: native=%v simplego=%v", i, outN[i], outS[i])
}
}
}