forked from ewalker544/libsvm-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modelio.go
347 lines (277 loc) · 8.9 KB
/
modelio.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
** Copyright 2014 Edward Walker
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http ://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** Description: Input/output routines for the Support Vector Machine model
** @author: Ed Walker
*/
package libSvm
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
func (model *Model) Dump(file string) error {
f, err := os.Create(file)
if err != nil {
return fmt.Errorf("Fail to open file %s\n", file)
}
defer f.Close() // close f on method return
var output []string
//svm_type_string := [5]string{"c_svc", "nu_svc", "one_class", "epsilon_svr", "nu_svr"}
output = append(output, fmt.Sprintf("svm_type %s\n", svm_type_string[model.param.SvmType]))
output = append(output, fmt.Sprintf("kernel_type %s\n", kernel_type_string[model.param.KernelType]))
if model.param.KernelType == POLY {
output = append(output, fmt.Sprintf("degree %d\n", model.param.Degree))
}
if model.param.KernelType == POLY || model.param.KernelType == RBF || model.param.KernelType == SIGMOID {
output = append(output, fmt.Sprintf("gamma %.6g\n", model.param.Gamma))
}
if model.param.KernelType == POLY || model.param.KernelType == SIGMOID {
output = append(output, fmt.Sprintf("coef0 %.6g\n", model.param.Coef0))
}
var nrClass int = model.nrClass
output = append(output, fmt.Sprintf("nr_class %d\n", nrClass))
var l int = model.l
output = append(output, fmt.Sprintf("total_sv %d\n", l))
output = append(output, "rho")
total_models := nrClass * (nrClass - 1) / 2
for i := 0; i < total_models; i++ {
output = append(output, fmt.Sprintf(" %.6g", model.rho[i]))
}
output = append(output, "\n")
if len(model.label) > 0 {
output = append(output, "label")
for i := 0; i < nrClass; i++ {
output = append(output, fmt.Sprintf(" %d", model.label[i]))
}
output = append(output, "\n")
}
if len(model.probA) > 0 {
output = append(output, "probA")
for i := 0; i < total_models; i++ {
output = append(output, fmt.Sprintf(" %.8g", model.probA[i]))
}
output = append(output, "\n")
}
if len(model.probB) > 0 {
output = append(output, "probB")
for i := 0; i < total_models; i++ {
output = append(output, fmt.Sprintf(" %.8g", model.probB[i]))
}
output = append(output, "\n")
}
if len(model.nSV) > 0 {
output = append(output, "nr_sv")
for i := 0; i < nrClass; i++ {
output = append(output, fmt.Sprintf(" %d", model.nSV[i]))
}
output = append(output, "\n")
}
output = append(output, "SV\n")
for i := 0; i < l; i++ {
for j := 0; j < nrClass-1; j++ {
output = append(output, fmt.Sprintf("%.16g ", model.svCoef[j][i]))
}
i_idx := model.sV[i]
if model.param.KernelType == PRECOMPUTED {
output = append(output, fmt.Sprintf("0:%d ", model.svSpace[i_idx]))
} else {
for model.svSpace[i_idx].index != -1 {
index := model.svSpace[i_idx].index
value := model.svSpace[i_idx].value
output = append(output, fmt.Sprintf("%d:%.8g ", index, value))
i_idx++
}
output = append(output, "\n")
}
}
f.WriteString(strings.Join(output, ""))
return nil
}
func (model *Model) readHeader(reader *bufio.Reader) error {
for {
var i int = 0
var err error
var line string
line, err = readline(reader)
if err != nil { // We should not encounter an EOF. If we do, it is an error.
return err
}
tokens := strings.Fields(line)
switch tokens[0] {
case "svm_type":
for i = 0; i < len(svm_type_string); i++ {
if svm_type_string[i] == tokens[1] {
model.param.SvmType = i
break
}
}
if i == len(svm_type_string) {
return fmt.Errorf("fail to parse svm model %s\n", tokens[1])
}
case "kernel_type":
for i = 0; i < len(kernel_type_string); i++ {
if kernel_type_string[i] == tokens[1] {
model.param.KernelType = i
break
}
}
if i == len(kernel_type_string) {
return fmt.Errorf("fail to parse kernel type %s\n", tokens[1])
}
case "degree":
if model.param.Degree, err = strconv.Atoi(tokens[1]); err != nil {
return err
}
case "gamma":
if model.param.Gamma, err = strconv.ParseFloat(tokens[1], 64); err != nil {
return err
}
case "coef0":
if model.param.Coef0, err = strconv.ParseFloat(tokens[1], 64); err != nil {
return err
}
case "nr_class":
if model.nrClass, err = strconv.Atoi(tokens[1]); err != nil {
return err
}
case "total_sv":
if model.l, err = strconv.Atoi(tokens[1]); err != nil {
return err
}
case "rho":
total_class_comparisons := model.nrClass * (model.nrClass - 1) / 2
if total_class_comparisons != len(tokens)-1 {
return fmt.Errorf("Number of rhos %d does not mactch the required number %d\n", len(tokens)-1, total_class_comparisons)
}
model.rho = make([]float64, total_class_comparisons)
for i = 0; i < total_class_comparisons; i++ {
if model.rho[i], err = strconv.ParseFloat(tokens[i+1], 64); err != nil {
return err
}
}
case "label":
if model.nrClass != len(tokens)-1 {
return fmt.Errorf("Number of labels %d does not appear in the file\n", model.nrClass)
}
model.label = make([]int, model.nrClass)
for i = 0; i < model.nrClass; i++ {
if model.label[i], err = strconv.Atoi(tokens[i+1]); err != nil {
return err
}
}
case "probA":
total_class_comparisons := model.nrClass * (model.nrClass - 1) / 2
if total_class_comparisons != len(tokens)-1 {
return fmt.Errorf("Number of probA %d does not mactch the required number %d\n", len(tokens)-1, total_class_comparisons)
}
model.probA = make([]float64, total_class_comparisons)
for i = 0; i < total_class_comparisons; i++ {
if model.probA[i], err = strconv.ParseFloat(tokens[i+1], 64); err != nil {
return err
}
}
case "probB":
total_class_comparisons := model.nrClass * (model.nrClass - 1) / 2
if total_class_comparisons != len(tokens)-1 {
return fmt.Errorf("Number of probB %d does not mactch the required number %d\n", len(tokens)-1, total_class_comparisons)
}
model.probB = make([]float64, total_class_comparisons)
for i = 0; i < total_class_comparisons; i++ {
if model.probB[i], err = strconv.ParseFloat(tokens[i+1], 64); err != nil {
return err
}
}
case "nr_sv":
if model.nrClass != len(tokens)-1 {
return fmt.Errorf("Number of nSV %d does not appear in the file %v\n", model.nrClass, tokens)
}
model.nSV = make([]int, model.nrClass)
for i = 0; i < model.nrClass; i++ {
if model.nSV[i], err = strconv.Atoi(tokens[i+1]); err != nil {
return err
}
}
case "SV":
return nil // done reading the header!
default:
return fmt.Errorf("unknown text in model file: [%s]\n", tokens[0])
}
}
return fmt.Errorf("Fail to completely read header")
}
func (model *Model) ReadModel(file string) error {
f, err := os.Open(file)
if err != nil {
return fmt.Errorf("Fail to open file %s\n", file)
}
defer f.Close() // close f on method return
return model.ReadModelFromReader(f)
}
func (model *Model) ReadModelFromReader(r io.Reader) error {
reader := bufio.NewReader(r)
if err := model.readHeader(reader); err != nil {
return err
}
var l int = model.l // read l from header
var m int = model.nrClass - 1 // read nrClass from header
model.svCoef = make([][]float64, m)
for i := 0; i < m; i++ {
model.svCoef[i] = make([]float64, l)
}
model.sV = make([]int, l)
var i int = 0
for {
line, err := readline(reader) // read a line
if err != nil {
break
}
tokens := strings.Fields(line) // get all the word tokens (seperated by white spaces)
if len(tokens) < 2 { // there should be at least 2 fields -- label + SV
continue
}
if i >= l {
return fmt.Errorf("Error in reading support vectors. i=%d and l=%d\n", i, l)
}
model.sV[i] = len(model.svSpace) // starting index into svSpace for this SV
var k int = 0
for _, token := range tokens {
if k < m {
model.svCoef[k][i], err = strconv.ParseFloat(token, 64)
k++
} else {
node := strings.Split(token, ":")
if len(node) < 2 {
return fmt.Errorf("Fail to parse svSpace from token %v\n", token)
}
var index int
var value float64
if index, err = strconv.Atoi(node[0]); err != nil {
return fmt.Errorf("Fail to parse index from token %v\n", token)
}
if value, err = strconv.ParseFloat(node[1], 64); err != nil {
return fmt.Errorf("Fail to parse value from token %v\n", token)
}
model.svSpace = append(model.svSpace, snode{index: index, value: value})
}
}
model.svSpace = append(model.svSpace, snode{index: -1})
i++
}
return nil
}