-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathKernel.cs
More file actions
226 lines (207 loc) · 6.61 KB
/
Copy pathKernel.cs
File metadata and controls
226 lines (207 loc) · 6.61 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Bigtree.Algorithm.SVM
{
internal interface IQMatrix
{
float[] GetQ(int column, int len);
double[] GetQD();
void SwapIndex(int i, int j);
}
internal abstract class Kernel : IQMatrix
{
private Node[][] _x;
private double[] _xSquare;
private KernelType _kernelType;
private int _degree;
private double _gamma;
private double _coef0;
public abstract float[] GetQ(int column, int len);
public abstract double[] GetQD();
public virtual void SwapIndex(int i, int j)
{
_x.SwapIndex(i, j);
if (_xSquare != null)
{
_xSquare.SwapIndex(i, j);
}
}
private static double powi(double value, int times)
{
double tmp = value, ret = 1.0;
for (int t = times; t > 0; t /= 2)
{
if (t % 2 == 1) ret *= tmp;
tmp = tmp * tmp;
}
return ret;
}
public double KernelFunction(int i, int j)
{
switch (_kernelType)
{
case KernelType.LINEAR:
return dot(_x[i], _x[j]);
case KernelType.POLY:
return powi(_gamma * dot(_x[i], _x[j]) + _coef0, _degree);
case KernelType.RBF:
return Math.Exp(-_gamma * (_xSquare[i] + _xSquare[j] - 2 * dot(_x[i], _x[j])));
case KernelType.SIGMOID:
return Math.Tanh(_gamma * dot(_x[i], _x[j]) + _coef0);
case KernelType.PRECOMPUTED:
return _x[i][(int)(_x[j][0].Value)].Value;
default:
return 0;
}
}
public Kernel(int l, Node[][] x_, Parameter param)
{
_kernelType = param.KernelType;
_degree = param.Degree;
_gamma = param.Gamma;
_coef0 = param.Coefficient0;
_x = (Node[][])x_.Clone();
if (_kernelType == KernelType.RBF)
{
_xSquare = new double[l];
for (int i = 0; i < l; i++)
_xSquare[i] = dot(_x[i], _x[i]);
}
else _xSquare = null;
}
private static double dot(Node[] xNodes, Node[] yNodes)
{
double sum = 0;
int xlen = xNodes.Length;
int ylen = yNodes.Length;
int i = 0;
int j = 0;
Node x = xNodes[0];
Node y = yNodes[0];
while (true)
{
if (x._index == y._index)
{
sum += x._value * y._value;
i++;
j++;
if (i < xlen && j < ylen)
{
x = xNodes[i];
y = yNodes[j];
}
else if (i < xlen)
{
x = xNodes[i];
break;
}
else if (j < ylen)
{
y = yNodes[j];
break;
}
else break;
}
else
{
if (x._index > y._index)
{
++j;
if (j < ylen)
y = yNodes[j];
else break;
}
else
{
++i;
if (i < xlen)
x = xNodes[i];
else break;
}
}
}
return sum;
}
private static double computeSquaredDistance(Node[] xNodes, Node[] yNodes)
{
Node x = xNodes[0];
Node y = yNodes[0];
int xLength = xNodes.Length;
int yLength = yNodes.Length;
int xIndex = 0;
int yIndex = 0;
double sum = 0;
while (true)
{
if (x._index == y._index)
{
double d = x._value - y._value;
sum += d * d;
xIndex++;
yIndex++;
if (xIndex < xLength && yIndex < yLength)
{
x = xNodes[xIndex];
y = yNodes[yIndex];
}
else if(xIndex < xLength){
x = xNodes[xIndex];
break;
}
else if(yIndex < yLength){
y = yNodes[yIndex];
break;
}else break;
}
else if (x._index > y._index)
{
sum += y._value * y._value;
if (++yIndex < yLength)
y = yNodes[yIndex];
else break;
}
else
{
sum += x._value * x._value;
if (++xIndex < xLength)
x = xNodes[xIndex];
else break;
}
}
for (; xIndex < xLength; xIndex++)
{
double d = xNodes[xIndex]._value;
sum += d * d;
}
for (; yIndex < yLength; yIndex++)
{
double d = yNodes[yIndex]._value;
sum += d * d;
}
return sum;
}
public static double KernelFunction(Node[] x, Node[] y, Parameter param)
{
switch (param.KernelType)
{
case KernelType.LINEAR:
return dot(x, y);
case KernelType.POLY:
return powi(param.Degree * dot(x, y) + param.Coefficient0, param.Degree);
case KernelType.RBF:
{
double sum = computeSquaredDistance(x, y);
return Math.Exp(-param.Gamma * sum);
}
case KernelType.SIGMOID:
return Math.Tanh(param.Gamma * dot(x, y) + param.Coefficient0);
case KernelType.PRECOMPUTED:
return x[(int)(y[0].Value)].Value;
default:
return 0;
}
}
}
}