-
Notifications
You must be signed in to change notification settings - Fork 5
/
krann.go
246 lines (212 loc) · 7.74 KB
/
krann.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
package mlpack
/*
#cgo CFLAGS: -I./capi -Wall
#cgo LDFLAGS: -L. -lmlpack_go_krann
#include <capi/krann.h>
#include <stdlib.h>
*/
import "C"
import "gonum.org/v1/gonum/mat"
type KrannOptionalParam struct {
Alpha float64
FirstLeafExact bool
InputModel *raModel
K int
LeafSize int
Naive bool
Query *mat.Dense
RandomBasis bool
Reference *mat.Dense
SampleAtLeaves bool
Seed int
SingleMode bool
SingleSampleLimit int
Tau float64
TreeType string
Verbose bool
}
func KrannOptions() *KrannOptionalParam {
return &KrannOptionalParam{
Alpha: 0.95,
FirstLeafExact: false,
InputModel: nil,
K: 0,
LeafSize: 20,
Naive: false,
Query: nil,
RandomBasis: false,
Reference: nil,
SampleAtLeaves: false,
Seed: 0,
SingleMode: false,
SingleSampleLimit: 20,
Tau: 5,
TreeType: "kd",
Verbose: false,
}
}
/*
This program will calculate the k rank-approximate-nearest-neighbors of a set
of points. You may specify a separate set of reference points and query
points, or just a reference set which will be used as both the reference and
query set. You must specify the rank approximation (in %) (and optionally the
success probability).
For example, the following will return 5 neighbors from the top 0.1% of the
data (with probability 0.95) for each point in input and store the distances
in distances and the neighbors in neighbors.csv:
// Initialize optional parameters for Krann().
param := mlpack.KrannOptions()
param.Reference = input
param.K = 5
param.Tau = 0.1
distances, neighbors, _ := mlpack.Krann(param)
Note that tau must be set such that the number of points in the corresponding
percentile of the data is greater than k. Thus, if we choose tau = 0.1 with a
dataset of 1000 points and k = 5, then we are attempting to choose 5 nearest
neighbors out of the closest 1 point -- this is invalid and the program will
terminate with an error message.
The output matrices are organized such that row i and column j in the
neighbors output file corresponds to the index of the point in the reference
set which is the i'th nearest neighbor from the point in the query set with
index j. Row i and column j in the distances output file corresponds to the
distance between those two points.
Input parameters:
- Alpha (float64): The desired success probability. Default value
0.95.
- FirstLeafExact (bool): The flag to trigger sampling only after
exactly exploring the first leaf.
- InputModel (raModel): Pre-trained kNN model.
- K (int): Number of nearest neighbors to find. Default value 0.
- LeafSize (int): Leaf size for tree building (used for kd-trees, UB
trees, R trees, R* trees, X trees, Hilbert R trees, R+ trees, R++ trees,
and octrees). Default value 20.
- Naive (bool): If true, sampling will be done without using a tree.
- Query (mat.Dense): Matrix containing query points (optional).
- RandomBasis (bool): Before tree-building, project the data onto a
random orthogonal basis.
- Reference (mat.Dense): Matrix containing the reference dataset.
- SampleAtLeaves (bool): The flag to trigger sampling at leaves.
- Seed (int): Random seed (if 0, std::time(NULL) is used). Default
value 0.
- SingleMode (bool): If true, single-tree search is used (as opposed to
dual-tree search.
- SingleSampleLimit (int): The limit on the maximum number of samples
(and hence the largest node you can approximate). Default value 20.
- Tau (float64): The allowed rank-error in terms of the percentile of
the data. Default value 5.
- TreeType (string): Type of tree to use: 'kd', 'ub', 'cover', 'r',
'x', 'r-star', 'hilbert-r', 'r-plus', 'r-plus-plus', 'oct'. Default
value 'kd'.
- Verbose (bool): Display informational messages and the full list of
parameters and timers at the end of execution.
Output parameters:
- distances (mat.Dense): Matrix to output distances into.
- neighbors (mat.Dense): Matrix to output neighbors into.
- outputModel (raModel): If specified, the kNN model will be output
here.
*/
func Krann(param *KrannOptionalParam) (*mat.Dense, *mat.Dense, raModel) {
params := getParams("krann")
timers := getTimers()
disableBacktrace()
disableVerbose()
// Detect if the parameter was passed; set if so.
if param.Alpha != 0.95 {
setParamDouble(params, "alpha", param.Alpha)
setPassed(params, "alpha")
}
// Detect if the parameter was passed; set if so.
if param.FirstLeafExact != false {
setParamBool(params, "first_leaf_exact", param.FirstLeafExact)
setPassed(params, "first_leaf_exact")
}
// Detect if the parameter was passed; set if so.
if param.InputModel != nil {
setRAModel(params, "input_model", param.InputModel)
setPassed(params, "input_model")
}
// Detect if the parameter was passed; set if so.
if param.K != 0 {
setParamInt(params, "k", param.K)
setPassed(params, "k")
}
// Detect if the parameter was passed; set if so.
if param.LeafSize != 20 {
setParamInt(params, "leaf_size", param.LeafSize)
setPassed(params, "leaf_size")
}
// Detect if the parameter was passed; set if so.
if param.Naive != false {
setParamBool(params, "naive", param.Naive)
setPassed(params, "naive")
}
// Detect if the parameter was passed; set if so.
if param.Query != nil {
gonumToArmaMat(params, "query", param.Query, false)
setPassed(params, "query")
}
// Detect if the parameter was passed; set if so.
if param.RandomBasis != false {
setParamBool(params, "random_basis", param.RandomBasis)
setPassed(params, "random_basis")
}
// Detect if the parameter was passed; set if so.
if param.Reference != nil {
gonumToArmaMat(params, "reference", param.Reference, false)
setPassed(params, "reference")
}
// Detect if the parameter was passed; set if so.
if param.SampleAtLeaves != false {
setParamBool(params, "sample_at_leaves", param.SampleAtLeaves)
setPassed(params, "sample_at_leaves")
}
// Detect if the parameter was passed; set if so.
if param.Seed != 0 {
setParamInt(params, "seed", param.Seed)
setPassed(params, "seed")
}
// Detect if the parameter was passed; set if so.
if param.SingleMode != false {
setParamBool(params, "single_mode", param.SingleMode)
setPassed(params, "single_mode")
}
// Detect if the parameter was passed; set if so.
if param.SingleSampleLimit != 20 {
setParamInt(params, "single_sample_limit", param.SingleSampleLimit)
setPassed(params, "single_sample_limit")
}
// Detect if the parameter was passed; set if so.
if param.Tau != 5 {
setParamDouble(params, "tau", param.Tau)
setPassed(params, "tau")
}
// Detect if the parameter was passed; set if so.
if param.TreeType != "kd" {
setParamString(params, "tree_type", param.TreeType)
setPassed(params, "tree_type")
}
// Detect if the parameter was passed; set if so.
if param.Verbose != false {
setParamBool(params, "verbose", param.Verbose)
setPassed(params, "verbose")
enableVerbose()
}
// Mark all output options as passed.
setPassed(params, "distances")
setPassed(params, "neighbors")
setPassed(params, "output_model")
// Call the mlpack program.
C.mlpackKrann(params.mem, timers.mem)
// Initialize result variable and get output.
var distancesPtr mlpackArma
distances := distancesPtr.armaToGonumMat(params, "distances")
var neighborsPtr mlpackArma
neighbors := neighborsPtr.armaToGonumUmat(params, "neighbors")
var outputModel raModel
outputModel.getRAModel(params, "output_model")
// Clean memory.
cleanParams(params)
cleanTimers(timers)
// Return output(s).
return distances, neighbors, outputModel
}