Repository navigation
Expand file tree
/
Copy path_sofia_ml.pyx
More file actions
94 lines (75 loc) · 3.59 KB
/
Copy path_sofia_ml.pyx
File metadata and controls
94 lines (75 loc) · 3.59 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
8000
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
# distutils: language = c++
# distutils: sources = minirank/src/sofia-ml-methods.cc ranking/src/{sf-weight-vector.cc,sf-data-set.cc}
from cython.operator cimport dereference as deref
from libcpp cimport bool
from libcpp.string cimport string
from libcpp.vector cimport vector
cimport numpy as np
import numpy as np
BUFFER_MB = 40 # default in sofia-ml
cdef extern from "src/sofia-ml-methods.h":
cdef cppclass SfDataSet:
SfDataSet(bool)
SfDataSet(string, int, bool)
cdef cppclass SfWeightVector:
SfWeightVector(int)
SfWeightVector(string)
string AsString()
float ValueOf(int)
cdef extern from "src/sofia-ml-methods.h" namespace "sofia_ml":
cdef enum LearnerType:
PEGASOS, MARGIN_PERCEPTRON, PASSIVE_AGGRESSIVE, LOGREG_PEGASOS,
LOGREG, LMS_REGRESSION, SGD_SVM, ROMMA
cdef enum EtaType:
BASIC_ETA
PEGASOS_ETA
CONSTANT
void StochasticRocLoop(SfDataSet, LearnerType, EtaType,
float, float, int, SfWeightVector*)
void BalancedStochasticOuterLoop(SfDataSet, LearnerType, EtaType,
float, float, int, SfWeightVector*)
void StochasticRankLoop(SfDataSet, LearnerType, EtaType,
float, float, int, SfWeightVector*)
void StochasticClassificationAndRankLoop(SfDataSet, LearnerType, EtaType,
float, float, float, int num_iters, SfWeightVector*)
void SvmPredictionsOnTestSet(SfDataSet test_data,
SfWeightVector, vector[float]*)
void StochasticOuterLoop(SfDataSet, LearnerType, EtaType,
float, float, int num_iters, SfWeightVector*)
void StochasticQueryNormRankLoop(SfDataSet, LearnerType, EtaType,
float, float, int num_iters, SfWeightVector*)
def train(train_data, int n_features, float alpha, int max_iter, bool fit_intercept,
model, float step_probability):
cdef SfDataSet *data = new SfDataSet(train_data, BUFFER_MB, fit_intercept)
cdef SfWeightVector *w = new SfWeightVector(n_features)
cdef float c = 0.0
cdef int i
# TODO : Select learner_type and eta_type. Rename model to loop_type.
if model == 'rank':
StochasticRankLoop(deref(data), PEGASOS, BASIC_ETA, alpha, c, max_iter, w)
elif model == 'roc':
StochasticRocLoop(deref(data), SGD_SVM, BASIC_ETA, alpha, c, max_iter, w)
elif model == 'combined-ranking':
StochasticClassificationAndRankLoop(deref(data), SGD_SVM, BASIC_ETA, alpha, c,
step_probability, max_iter, w)
elif model == 'stochastic':
StochasticOuterLoop(deref(data), SGD_SVM, BASIC_ETA, alpha, c, max_iter, w)
elif model == 'balanced-stochastic':
BalancedStochasticOuterLoop(deref(data), SGD_SVM, BASIC_ETA, alpha, c, max_iter, w)
elif model == 'query-norm-rank':
StochasticQueryNormRankLoop(deref(data), SGD_SVM, BASIC_ETA, alpha, c, max_iter, w)
else:
raise NotImplementedError
cdef np.ndarray[ndim=1, dtype=np.float64_t] coef = np.empty(n_features)
for i in range(n_features):
coef[i] = w.ValueOf(i)
return coef
def predict(test_data, string coef, bool fit_intercept):
cdef SfDataSet *test_dataset = new SfDataSet(test_data, BUFFER_MB, fit_intercept)
cdef SfWeightVector *w = new SfWeightVector(coef)
cdef vector[float] *predictions = new vector[float]()
SvmPredictionsOnTestSet(deref(test_dataset), deref(w), predictions)
cdef np.ndarray[ndim=1, dtype=np.float64_t] out = np.empty(predictions.size())
for i in range(predictions.size()):
out[i] = predictions.at(i)
return out