把 Support Vector Machines 翻译成 支持向量机 是书呆子翻译。基座向量机 不好吗。
1 文本格式
using System;
namespace Legalsoft.Truffer
{
/// <summary>
/// Support Vector Machines
/// </summary>
public class Svm
{
private Svmgenkernel gker { get; set; }
private int m { get; set; }
private int fnz { get; set; }
private int fub { get; set; }
private int niter { get; set; }
private double[] alph { get; set; }
private double[] alphold { get; set; }
private Ran ran { get; set; } = null;
private bool alphinit { get; set; }
private double dalph { get; set; }
public Svm(Svmgenkernel inker)
{
this.gker = inker;
this.m = gker.y.Length;
this.alph = new double[m];
this.alphold = new double[m];
this.ran = new Ran(21);
this.alphinit = false;
}
public double relax(double lambda, double om)
{
int iter;
int j;
int jj;
int k;
int kk;
double sum;
double[] pinsum = new double[m];
if (alphinit == false)
{
for (j = 0; j < m; j++)
{
alph[j] = 0.0;
}
alphinit = true;
}
// alphold = alph;
alphold = Globals.CopyFrom(alph);
Indexx x = new Indexx(alph);
for (fnz = 0; fnz < m; fnz++)
{
if (alph[x.indx[fnz]] != 0.0)
{
break;
}
}
for (j = fnz; j < m - 2; j++)
{
k = (int)(j + (ran.int32() % (m - j)));
Globals.SWAP(ref x.indx[j], ref x.indx[k]);
}
for (jj = 0; jj < m; jj++)
{
j = x.indx[jj];
sum = 0.0;
for (kk = fnz; kk < m; kk++)
{
k = x.indx[kk];
sum += (gker.ker[j, k] + 1.0) * gker.y[k] * alph[k];
}
alph[j] = alph[j] - (om / (gker.ker[j, j] + 1.0)) * (gker.y[j] * sum - 1.0);
alph[j] = Math.Max(0.0, Math.Min(lambda, alph[j]));
if (jj < fnz && alph[j] > 0)
{
Globals.SWAP(ref x.indx[--fnz], ref x.indx[jj]);
}
}
Indexx y = new Indexx(alph);
for (fnz = 0; fnz < m; fnz++)
{
if (alph[y.indx[fnz]] != 0.0)
{
break;
}
}
for (fub = fnz; fub < m; fub++)
{
if (alph[y.indx[fub]] == lambda)
{
break;
}
}
for (j = fnz; j < fub - 2; j++)
{
k = (int)(j + (ran.int32() % (fub - j)));
Globals.SWAP(ref y.indx[j], ref y.indx[k]);
}
for (jj = fnz; jj < fub; jj++)
{
j = y.indx[jj];
sum = 0.0;
for (kk = fub; kk < m; kk++)
{
k = y.indx[kk];
sum += (gker.ker[j, k] + 1.0) * gker.y[k] * alph[k];
}
pinsum[jj] = sum;
}
niter = Math.Max((int)(0.5 * (m + 1.0) * (m - fnz + 1.0) / (Globals.SQR(fub - fnz + 1.0))), 1);
for (iter = 0; iter < niter; iter++)
{
for (jj = fnz; jj < fub; jj++)
{
j = y.indx[jj];
sum = pinsum[jj];
for (kk = fnz; kk < fub; kk++)
{
k = y.indx[kk];
sum += (gker.ker[j, k] + 1.0) * gker.y[k] * alph[k];
}
alph[j] = alph[j] - (om / (gker.ker[j, j] + 1.0)) * (gker.y[j] * sum - 1.0);
alph[j] = Math.Max(0.0, Math.Min(lambda, alph[j]));
}
}
dalph = 0.0;
for (j = 0; j < m; j++)
{
dalph += Globals.SQR(alph[j] - alphold[j]);
}
return Math.Sqrt(dalph);
}
public double predict(int k)
{
double sum = 0.0;
for (int j = 0; j < m; j++)
{
sum += alph[j] * gker.y[j] * (gker.ker[j, k] + 1.0);
}
return sum;
}
}
}
2 代码格式
using System;
namespace Legalsoft.Truffer
{
/// <summary>
/// Support Vector Machines
/// </summary>
public class Svm
{
private Svmgenkernel gker { get; set; }
private int m { get; set; }
private int fnz { get; set; }
private int fub { get; set; }
private int niter { get; set; }
private double[] alph { get; set; }
private double[] alphold { get; set; }
private Ran ran { get; set; } = null;
private bool alphinit { get; set; }
private double dalph { get; set; }
public Svm(Svmgenkernel inker)
{
this.gker = inker;
this.m = gker.y.Length;
this.alph = new double[m];
this.alphold = new double[m];
this.ran = new Ran(21);
this.alphinit = false;
}
public double relax(double lambda, double om)
{
int iter;
int j;
int jj;
int k;
int kk;
double sum;
double[] pinsum = new double[m];
if (alphinit == false)
{
for (j = 0; j < m; j++)
{
alph[j] = 0.0;
}
alphinit = true;
}
// alphold = alph;
alphold = Globals.CopyFrom(alph);
Indexx x = new Indexx(alph);
for (fnz = 0; fnz < m; fnz++)
{
if (alph[x.indx[fnz]] != 0.0)
{
break;
}
}
for (j = fnz; j < m - 2; j++)
{
k = (int)(j + (ran.int32() % (m - j)));
Globals.SWAP(ref x.indx[j], ref x.indx[k]);
}
for (jj = 0; jj < m; jj++)
{
j = x.indx[jj];
sum = 0.0;
for (kk = fnz; kk < m; kk++)
{
k = x.indx[kk];
sum += (gker.ker[j, k] + 1.0) * gker.y[k] * alph[k];
}
alph[j] = alph[j] - (om / (gker.ker[j, j] + 1.0)) * (gker.y[j] * sum - 1.0);
alph[j] = Math.Max(0.0, Math.Min(lambda, alph[j]));
if (jj < fnz && alph[j] > 0)
{
Globals.SWAP(ref x.indx[--fnz], ref x.indx[jj]);
}
}
Indexx y = new Indexx(alph);
for (fnz = 0; fnz < m; fnz++)
{
if (alph[y.indx[fnz]] != 0.0)
{
break;
}
}
for (fub = fnz; fub < m; fub++)
{
if (alph[y.indx[fub]] == lambda)
{
break;
}
}
for (j = fnz; j < fub - 2; j++)
{
k = (int)(j + (ran.int32() % (fub - j)));
Globals.SWAP(ref y.indx[j], ref y.indx[k]);
}
for (jj = fnz; jj < fub; jj++)
{
j = y.indx[jj];
sum = 0.0;
for (kk = fub; kk < m; kk++)
{
k = y.indx[kk];
sum += (gker.ker[j, k] + 1.0) * gker.y[k] * alph[k];
}
pinsum[jj] = sum;
}
niter = Math.Max((int)(0.5 * (m + 1.0) * (m - fnz + 1.0) / (Globals.SQR(fub - fnz + 1.0))), 1);
for (iter = 0; iter < niter; iter++)
{
for (jj = fnz; jj < fub; jj++)
{
j = y.indx[jj];
sum = pinsum[jj];
for (kk = fnz; kk < fub; kk++)
{
k = y.indx[kk];
sum += (gker.ker[j, k] + 1.0) * gker.y[k] * alph[k];
}
alph[j] = alph[j] - (om / (gker.ker[j, j] + 1.0)) * (gker.y[j] * sum - 1.0);
alph[j] = Math.Max(0.0, Math.Min(lambda, alph[j]));
}
}
dalph = 0.0;
for (j = 0; j < m; j++)
{
dalph += Globals.SQR(alph[j] - alphold[j]);
}
return Math.Sqrt(dalph);
}
public double predict(int k)
{
double sum = 0.0;
for (int j = 0; j < m; j++)
{
sum += alph[j] * gker.y[j] * (gker.ker[j, k] + 1.0);
}
return sum;
}
}
}