/
util.go
152 lines (110 loc) · 2.99 KB
/
util.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package lingo
import (
"gonum.org/v1/gonum/mat"
"gonum.org/v1/hdf5"
"math"
)
const (
// ErrorThreshold is used in tests as the default error expected between predicted and expected values.
ErrorThreshold = 0.00001
)
type Validator interface {
validate(x []float64) error
}
// VecToArrayFloat64 converts a vector into an array of floats.
func VecToArrayFloat64(v mat.Vector) []float64 {
r, _ := v.Dims()
o := make([]float64, r)
for i := 0; i < r; i++ {
o[i] = v.At(i, 0)
}
return o
}
// Argmax gets the i, j associated with the largest value in a dense matrix.
func Argmax(a mat.Vector) (int, int) {
r, c := a.Dims()
maxVal := math.Inf(-1)
maxI := 0
maxJ := 0
for i := 0; i < r; i++ {
for j := 0; j < c; j++ {
val := a.At(i, j)
if val > maxVal {
maxVal = val
maxI = i
maxJ = j
}
}
}
return maxI, maxJ
}
// loadArray loads a 1D float array from a HDF5 dataset.
func loadArray(dataset *hdf5.Dataset) ([]float64, int, int) {
rows, cols := 0, 0
rowsAttr, err := dataset.OpenAttribute("n")
if err != nil {
panic("failed to open attribute 'n'")
}
colsAttr, err := dataset.OpenAttribute("m")
if err != nil {
panic("failed to open attribute 'n'")
}
err = colsAttr.Read(&rows, hdf5.T_NATIVE_UINT32)
if err != nil {
panic("failed to read 'rows'")
}
err = rowsAttr.Read(&cols, hdf5.T_NATIVE_UINT32)
if err != nil {
panic("failed to read 'cols'")
}
params := make([]float64, rows*cols)
err = dataset.Read(¶ms)
return params, rows, cols
}
// Load loads a linear model from a HDF5 file.
func Load(fileName string) (modelType string, model *LinearModel) {
file, err := hdf5.OpenFile(fileName, hdf5.F_ACC_RDONLY)
if err != nil {
panic("failed to open target HDF5 file: " + fileName)
}
defer file.Close()
modelGroup, err := file.OpenGroup("model")
if err != nil {
panic("failed to open group 'model'")
}
modelTypeAttr, err := modelGroup.OpenAttribute("estimatorType")
if err != nil {
panic("failed to open attribute 'estimatorType'")
}
modelTypeAttr.Read(&modelType, hdf5.T_GO_STRING)
thetaDataset, err := modelGroup.OpenDataset("theta")
defer thetaDataset.Close()
if err != nil {
panic("failed to open dataset 'theta'")
}
interceptDataset, err := modelGroup.OpenDataset("intercept")
defer interceptDataset.Close()
if err != nil {
panic("failed to open dataset 'intercept'")
}
theta, _, nVars := loadArray(thetaDataset)
intercept, _, _ := loadArray(interceptDataset)
model = NewLinearModel(theta, intercept, nVars)
return
}
func LoadClassifier(fileName string) (model *LinearClassifier) {
modelType, coreModel := Load(fileName)
if modelType != "classifier" {
panic("expected model of type 'classifier', got: " + modelType)
}
model = &LinearClassifier{coreModel}
return
}
func LoadRegressor(fileName string) (model *LinearRegressor) {
modelType, coreModel := Load(fileName)
if modelType != "regressor" {
panic("expected model of type 'regressor', got: " + modelType)
}
model = &LinearRegressor{coreModel}
return
}