📄 svm.cs
字号:
{
if (y[k] == + 1)
{
if (G[k] < Gm2)
continue;
}
else if (G[k] < Gm1)
continue;
}
else
continue;
swap_index(k, active_size);
active_size++;
++k; // look at the newcomer
}
}
//UPGRADE_NOTE: 方法“calculate_rho”的访问修饰符被更改为“internal”。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1204"'
internal virtual double calculate_rho()
{
double r;
int nr_free = 0;
double ub = INF, lb = - INF, sum_free = 0;
for (int i = 0; i < active_size; i++)
{
double yG = y[i] * G[i];
if (is_lower_bound(i))
{
if (y[i] > 0)
ub = System.Math.Min(ub, yG);
else
lb = System.Math.Max(lb, yG);
}
else if (is_upper_bound(i))
{
if (y[i] < 0)
ub = System.Math.Min(ub, yG);
else
lb = System.Math.Max(lb, yG);
}
else
{
++nr_free;
sum_free += yG;
}
}
if (nr_free > 0)
r = sum_free / nr_free;
else
r = (ub + lb) / 2;
return r;
}
}
//
// Solver for nu-svm classification and regression
//
// additional constraint: e^T \alpha = constant
//
sealed class Solver_NU:Solver
{
private SolutionInfo si;
//UPGRADE_NOTE: 方法“Solve”的访问修饰符被更改为“internal”。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1204"'
internal void Solve(int l, QMatrix Q, double[] b, sbyte[] y, double[] alpha, double Cp, double Cn, double eps, SolutionInfo si, int shrinking)
{
this.si = si;
base.Solve(l, Q, b, y, alpha, Cp, Cn, eps, si, shrinking);
}
// return 1 if already optimal, return 0 otherwise
//UPGRADE_NOTE: 方法“select_working_set”的访问修饰符被更改为“internal”。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1204"'
internal override int select_working_set(int[] working_set)
{
// return i,j such that y_i = y_j and
// i: maximizes -y_i * grad(f)_i, i in I_up(\alpha)
// j: minimizes the decrease of obj value
// (if quadratic coefficeint <= 0, replace it with tau)
// -y_j*grad(f)_j < -y_i*grad(f)_i, j in I_low(\alpha)
double Gmaxp = - INF;
int Gmaxp_idx = - 1;
double Gmaxn = - INF;
int Gmaxn_idx = - 1;
int Gmin_idx = - 1;
double obj_diff_min = INF;
for (int t = 0; t < active_size; t++)
if (y[t] == + 1)
{
if (!is_upper_bound(t))
if (- G[t] >= Gmaxp)
{
Gmaxp = - G[t];
Gmaxp_idx = t;
}
}
else
{
if (!is_lower_bound(t))
if (G[t] >= Gmaxn)
{
Gmaxn = G[t];
Gmaxn_idx = t;
}
}
int ip = Gmaxp_idx;
int in_Renamed = Gmaxn_idx;
float[] Q_ip = null;
float[] Q_in = null;
if (ip != - 1)
// null Q_ip not accessed: Gmaxp=-INF if ip=-1
Q_ip = Q.get_Q(ip, active_size);
if (in_Renamed != - 1)
Q_in = Q.get_Q(in_Renamed, active_size);
for (int j = 0; j < active_size; j++)
{
if (y[j] == + 1)
{
if (!is_lower_bound(j))
{
double grad_diff = Gmaxp + G[j];
if (grad_diff >= eps)
{
double obj_diff;
double quad_coef = Q_ip[ip] + QD[j] - 2 * Q_ip[j];
if (quad_coef > 0)
obj_diff = (- (grad_diff * grad_diff)) / quad_coef;
else
obj_diff = (- (grad_diff * grad_diff)) / 1e-12;
if (obj_diff <= obj_diff_min)
{
Gmin_idx = j;
obj_diff_min = obj_diff;
}
}
}
}
else
{
if (!is_upper_bound(j))
{
double grad_diff = Gmaxn - G[j];
if (grad_diff >= eps)
{
double obj_diff;
double quad_coef = Q_in[in_Renamed] + QD[j] - 2 * Q_in[j];
if (quad_coef > 0)
obj_diff = (- (grad_diff * grad_diff)) / quad_coef;
else
obj_diff = (- (grad_diff * grad_diff)) / 1e-12;
if (obj_diff <= obj_diff_min)
{
Gmin_idx = j;
obj_diff_min = obj_diff;
}
}
}
}
}
if (Gmin_idx == - 1)
return 1;
if (y[Gmin_idx] == + 1)
working_set[0] = Gmaxp_idx;
else
working_set[0] = Gmaxn_idx;
working_set[1] = Gmin_idx;
return 0;
}
//UPGRADE_NOTE: 方法“do_shrinking”的访问修饰符被更改为“internal”。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1204"'
internal override void do_shrinking()
{
double Gmax1 = - INF; // max { -y_i * grad(f)_i | y_i = +1, i in I_up(\alpha) }
double Gmax2 = - INF; // max { y_i * grad(f)_i | y_i = +1, i in I_low(\alpha) }
double Gmax3 = - INF; // max { -y_i * grad(f)_i | y_i = -1, i in I_up(\alpha) }
double Gmax4 = - INF; // max { y_i * grad(f)_i | y_i = -1, i in I_low(\alpha) }
// find maximal violating pair first
int k;
for (k = 0; k < active_size; k++)
{
if (!is_upper_bound(k))
{
if (y[k] == + 1)
{
if (- G[k] > Gmax1)
Gmax1 = - G[k];
}
else if (- G[k] > Gmax3)
Gmax3 = - G[k];
}
if (!is_lower_bound(k))
{
if (y[k] == + 1)
{
if (G[k] > Gmax2)
Gmax2 = G[k];
}
else if (G[k] > Gmax4)
Gmax4 = G[k];
}
}
// shrinking
double Gm1 = - Gmax2;
double Gm2 = - Gmax1;
double Gm3 = - Gmax4;
double Gm4 = - Gmax3;
for (k = 0; k < active_size; k++)
{
if (is_lower_bound(k))
{
if (y[k] == + 1)
{
if (- G[k] >= Gm1)
continue;
}
else if (- G[k] >= Gm3)
continue;
}
else if (is_upper_bound(k))
{
if (y[k] == + 1)
{
if (G[k] >= Gm2)
continue;
}
else if (G[k] >= Gm4)
continue;
}
else
continue;
--active_size;
swap_index(k, active_size);
--k; // look at the newcomer
}
// unshrink, check all variables again before final iterations
if (unshrinked || System.Math.Max(- (Gm1 + Gm2), - (Gm3 + Gm4)) > eps * 10)
return ;
unshrinked = true;
reconstruct_gradient();
for (k = l - 1; k >= active_size; k--)
{
if (is_lower_bound(k))
{
if (y[k] == + 1)
{
if (- G[k] < Gm1)
continue;
}
else if (- G[k] < Gm3)
continue;
}
else if (is_upper_bound(k))
{
if (y[k] == + 1)
{
if (G[k] < Gm2)
continue;
}
else if (G[k] < Gm4)
continue;
}
else
continue;
swap_index(k, active_size);
active_size++;
++k; // look at the newcomer
}
}
//UPGRADE_NOTE: 方法“calculate_rho”的访问修饰符被更改为“internal”。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1204"'
internal override double calculate_rho()
{
int nr_free1 = 0, nr_free2 = 0;
double ub1 = INF, ub2 = INF;
double lb1 = - INF, lb2 = - INF;
double sum_free1 = 0, sum_free2 = 0;
for (int i = 0; i < active_size; i++)
{
if (y[i] == + 1)
{
if (is_lower_bound(i))
ub1 = System.Math.Min(ub1, G[i]);
else if (is_upper_bound(i))
lb1 = System.Math.Max(lb1, G[i]);
else
{
++nr_free1;
sum_free1 += G[i];
}
}
else
{
if (is_lower_bound(i))
ub2 = System.Math.Min(ub2, G[i]);
else if (is_upper_bound(i))
lb2 = System.Math.Max(lb2, G[i]);
else
{
++nr_free2;
sum_free2 += G[i];
}
}
}
double r1, r2;
if (nr_free1 > 0)
r1 = sum_free1 / nr_free1;
else
r1 = (ub1 + lb1) / 2;
if (nr_free2 > 0)
r2 = sum_free2 / nr_free2;
else
r2 = (ub2 + lb2) / 2;
si.r = (r1 + r2) / 2;
return (r1 - r2) / 2;
}
}
//
// Q matrices for various formulations
//
class SVC_Q:Kernel
{
//UPGRADE_NOTE: Final 已从“y ”的声明中移除。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1003"'
private sbyte[] y;
//UPGRADE_NOTE: Final 已从“cache ”的声明中移除。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1003"'
private Cache cache;
//UPGRADE_NOTE: Final 已从“QD ”的声明中移除。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1003"'
private float[] QD;
internal SVC_Q(svm_problem prob, svm_parameter param, sbyte[] y_):base(prob.l, prob.x, param)
{
y = new sbyte[y_.Length];
y_.CopyTo(y, 0);
//UPGRADE_WARNING: 在 C# 中,收缩转换可能产生意外的结果。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042"'
cache = new Cache(prob.l, (int) (param.cache_size * (1 << 20)));
QD = new float[prob.l];
for (int i = 0; i < prob.l; i++)
{
//UPGRADE_WARNING: 在 C# 中,收缩转换可能产生意外的结果。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042"'
QD[i] = (float) kernel_function(i, i);
}
}
//UPGRADE_NOTE: 方法“get_Q”的访问修饰符被更改为“internal”。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1204"'
internal override float[] get_Q(int i, int len)
{
float[][] data = new float[1][];
int start;
if ((start = cache.get_data(i, data, len)) < len)
{
for (int j = start; j < len; j++)
{
//UPGRADE_WARNING: 在 C# 中,收缩转换可能产生意外的结果。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042"'
data[0][j] = (float) (y[i] * y[j] * kernel_function(i, j));
}
}
return data[0];
}
//UPGRADE_NOTE: 方法“get_QD”的访问修饰符被更改为“internal”。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1204"'
internal override float[] get_QD()
{
return QD;
}
//UPGRADE_NOTE: 方法“swap_index”的访问修饰符被更改为“internal”。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1204"'
internal override void swap_index(int i, int j)
{
cache.swap_index(i, j);
base.swap_index(i, j);
do
{
sbyte _ = y[i]; y[i] = y[j]; y[j] = _;
}
while (false);
do
{
float _ = QD[i]; QD[i] = QD[j]; QD[j] = _;
}
while (false);
}
}
class ONE_CLASS_Q:Kernel
{
//UPGRADE_NOTE: Final 已从“cache ”的声明中移除。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1003"'
private Cache cache;
//UPGRADE_NOTE: Final 已从“QD ”的声明中移除。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1003"'
private float[] QD;
internal ONE_CLASS_Q(svm_problem prob, svm_parameter param):base(prob.l, prob.x, param)
{
//UPGRADE_WARNING: 在 C# 中,收缩转换可能产生意外的结果。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042"'
cache = new Cache(prob.l, (int) (param.cache_size * (1 << 20)));
QD = new float[prob.l];
for (int i = 0; i < prob.l; i++)
{
//UPGRADE_WARNING: 在 C# 中,收缩转换可能产生意外的结果。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042"'
QD[i] = (float) kernel_function(i, i);
}
}
//UPGRADE_NOTE: 方法“get_Q”的访问修饰符被更改为“internal”。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1204"'
internal override float[] get_Q(int i, int len)
{
float[][] data = new float[1][];
int start;
if ((start = cache.get_data(i, data, len)) < len)
{
for (int j = start; j < len; j++)
{
//UPGRADE_WARNING: 在 C# 中,收缩转换可能产生意外的结果。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042"'
data[0][j] = (float) kernel_function(i, j);
}
}
return data[0];
}
//UPGRADE_NOTE: 方法“get_QD”的访问修饰符被更改为“internal”。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1204"'
internal override float[] get_QD()
{
return QD;
}
//UPGRADE_NOTE: 方法“swap_index”的访问修饰符被更改为“internal”。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1204"'
internal override void swap_index(int i, int j)
{
cache.swap_index(i, j);
base.swap_index(i, j);
do
{
float _ = QD[i]; QD[i] = QD[j]; QD[j] = _;
}
while (false);
}
}
class SVR_Q:Kernel
{
//UPGRADE_NOTE: Final 已从“l ”的声明中移除。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1003"'
private int l;
//UPGRADE_NOTE: Final 已从“cache ”的声明中移除。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1003"'
private Cache cache;
//UPGRADE_NOTE: Final 已从“sign ”的声明中移除。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1003"'
private sbyte[] sign;
//UPGRADE_NOTE: Final 已从“index ”的声明中移除。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1003"'
private int[] index;
private int next_buffer;
private float[][] buffer;
//UPGRADE_NOTE: Final 已从“QD ”的声明中移除。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1003"'
private float[] QD;
internal SVR_Q(svm_problem prob, svm_parameter param):base(prob.l, prob.x, param)
{
l = prob.l;
//UPGRADE_WARNING: 在 C# 中,收缩转换可能产生意外的结果。 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042"'
cache = new Cache(l, (int) (param.cache_size * (1 << 20)));
QD = new float[2 * l];
sign = new sbyte[2 * l];
index = new int[2 * l];
for (int k = 0; k < l; k++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -