-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml_linear.go
130 lines (117 loc) · 3.51 KB
/
ml_linear.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
package src
/*
#cgo LDFLAGS: -llinear
#include <linear.h>
#include <stdio.h>
#include <stdlib.h>
#include "helper.h"
*/
import "C"
import "unsafe"
import (
//"errors"
//"fmt"
"github.com/gonum/matrix/mat64"
)
// Model contains a pointer to C's struct model (i.e., `*C.struct_model`). It is
// returned after training and used for predicting.
type Model struct {
// struct model
// {
// struct parameter param;
// int nr_class; /* number of classes */
// int nr_feature;
// double *w;
// int *label; /* label of each class */
// double bias;
// };
cModel *C.struct_model
}
func (f *Model) W() []float64 {
w := doubleToFloats(f.cModel.w, int(f.cModel.nr_feature)+1)
return w
}
func (f *Model) Label() int {
label := int(C.int(*f.cModel.label))
return label
}
// Wrapper for the `train` function in liblinear.
//
// `model* train(const struct problem *prob, const struct parameter *param);`
//
// The explanation of parameters are:
//
// solverType:
//
// for multi-class classification
// 0 -- L2-regularized logistic regression (primal)
// 1 -- L2-regularized L2-loss support vector classification (dual)
// 2 -- L2-regularized L2-loss support vector classification (primal)
// 3 -- L2-regularized L1-loss support vector classification (dual)
// 4 -- support vector classification by Crammer and Singer
// 5 -- L1-regularized L2-loss support vector classification
// 6 -- L1-regularized logistic regression
// 7 -- L2-regularized logistic regression (dual)
// for regression
// 11 -- L2-regularized L2-loss support vector regression (primal)
// 12 -- L2-regularized L2-loss support vector regression (dual)
// 13 -- L2-regularized L1-loss support vector regression (dual)
//
// eps is the stopping criterion.
//
// C_ is the cost of constraints violation.
//
// p is the sensitiveness of loss of support vector regression.
//
// classWeights is a map from int to float64, with the key be the class and the
// value be the weight. For example, {1: 10, -1: 0.5} means giving weight=10 for
// class=1 while weight=0.5 for class=-1
//
// If you do not want to change penalty for any of the classes, just set
// classWeights to nil.
func Train(X, y *mat64.Dense, bias float64, solverType int, c_, p, eps float64, classWeights map[int]float64) *Model {
var weightLabelPtr *C.int
var weightPtr *C.double
nRows, nCols := X.Dims()
cX := mapCDouble(X.RawMatrix().Data)
cY := mapCDouble(y.ColView(0).RawVector().Data)
nrWeight := len(classWeights)
weightLabel := []C.int{}
weight := []C.double{}
for key, val := range classWeights {
weightLabel = append(weightLabel, (C.int)(key))
weight = append(weight, (C.double)(val))
}
if nrWeight > 0 {
weightLabelPtr = &weightLabel[0]
weightPtr = &weight[0]
} else {
weightLabelPtr = nil
weightPtr = nil
}
model := C.call_train(
&cX[0], &cY[0],
C.int(nRows), C.int(nCols), C.double(bias),
C.int(solverType), C.double(c_), C.double(p), C.double(eps),
C.int(nrWeight), weightLabelPtr, weightPtr)
return &Model{
cModel: model,
}
}
// convert C double pointer to float64 slice ...
func doubleToFloats(in *C.double, size int) []float64 {
outD := (*[1 << 30]C.double)(unsafe.Pointer(in))[:size:size]
defer C.free(unsafe.Pointer(in))
out := make([]float64, size, size)
for i := 0; i < size; i++ {
out[i] = float64(outD[i])
}
return out
}
func mapCDouble(in []float64) []C.double {
out := make([]C.double, len(in), len(in))
for i, val := range in {
out[i] = C.double(val)
}
return out
}