Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply changes of v318→v325 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions SVM/SVM/Prediction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static double Predict(
int correct = 0;
int total = 0;
double error = 0;
double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0;
StreamWriter output = outputFile != null ? new StreamWriter(outputFile) : null;

SvmType svm_type = Procedures.svm_get_svm_type(model);
Expand Down Expand Up @@ -77,16 +77,16 @@ public static double Predict(
}
for (int i = 0; i < problem.Count; i++)
{
double target = problem.Y[i];
double target_label = problem.Y[i];
Node[] x = problem.X[i];

double v;
double predict_label;
if (predict_probability && (svm_type == SvmType.C_SVC || svm_type == SvmType.NU_SVC))
{
v = Procedures.svm_predict_probability(model, x, prob_estimates);
predict_label = Procedures.svm_predict_probability(model, x, prob_estimates);
if (output != null)
{
output.Write(v + " ");
output.Write(predict_label + " ");
for (int j = 0; j < nr_class; j++)
{
output.Write(prob_estimates[j] + " ");
Expand All @@ -96,19 +96,19 @@ public static double Predict(
}
else
{
v = Procedures.svm_predict(model, x);
predict_label = Procedures.svm_predict(model, x);
if(output != null)
output.Write(v + "\n");
output.Write(predict_label + "\n");
}

if (v == target)
if (predict_label == target_label)
++correct;
error += (v - target) * (v - target);
sumv += v;
sumy += target;
sumvv += v * v;
sumyy += target * target;
sumvy += v * target;
error += (predict_label - target_label) * (predict_label - target_label);
sump += predict_label;
sumt += target_label;
sumpp += predict_label * predict_label;
sumtt += target_label * target_label;
sumpt += predict_label * target_label;
++total;
}
if(output != null)
Expand Down
44 changes: 32 additions & 12 deletions SVM/SVM/Solver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ protected virtual int select_working_set(int[] working_set)
}
}

if (Gmax + Gmax2 < eps)
if (Gmax + Gmax2 < eps || Gmin_idx == -1)
return 1;

working_set[0] = Gmax_idx;
Expand Down Expand Up @@ -1849,7 +1849,7 @@ public static Model svm_train(Problem prob, Parameter param)
model.PairwiseProbabilityA = null;
}

int nnz = 0;
int total_sv = 0;
int[] nz_count = new int[nr_class];
model.NumberOfSVPerClass = new int[nr_class];
for (i = 0; i < nr_class; i++)
Expand All @@ -1859,17 +1859,17 @@ public static Model svm_train(Problem prob, Parameter param)
if (nonzero[start[i] + j])
{
++nSV;
++nnz;
++total_sv;
}
model.NumberOfSVPerClass[i] = nSV;
nz_count[i] = nSV;
}

info("Total nSV = " + nnz + "\n");
info("Total nSV = " + total_sv + "\n");

model.SupportVectorCount = nnz;
model.SupportVectors = new Node[nnz][];
model.SupportVectorIndices = new int[nnz];
model.SupportVectorCount = total_sv;
model.SupportVectors = new Node[total_sv][];
model.SupportVectorIndices = new int[total_sv];
p = 0;
for (i = 0; i < l; i++)
if (nonzero[i])
Expand All @@ -1885,7 +1885,7 @@ public static Model svm_train(Problem prob, Parameter param)

model.SupportVectorCoefficients = new double[nr_class - 1][];
for (i = 0; i < nr_class - 1; i++)
model.SupportVectorCoefficients[i] = new double[nnz];
model.SupportVectorCoefficients[i] = new double[total_sv];

p = 0;
for (i = 0; i < nr_class; i++)
Expand Down Expand Up @@ -2172,7 +2172,13 @@ public static double svm_predict_probability(Model model, Node[] x, double[] pro
pairwise_prob[j, i] = 1 - pairwise_prob[i, j];
k++;
}
multiclass_probability(nr_class, pairwise_prob, prob_estimates);
if (nr_class == 2)
{
prob_estimates[0] = pairwise_prob[0, 1];
prob_estimates[1] = pairwise_prob[1, 0];
}
else
multiclass_probability(nr_class, pairwise_prob, prob_estimates);

int prob_max_idx = 0;
for (i = 1; i < nr_class; i++)
Expand All @@ -2189,12 +2195,26 @@ public static String svm_check_parameter(Problem prob, Parameter param)
// svm_type

SvmType svm_type = param.SvmType;

if (svm_type != SvmType.C_SVC &&
svm_type != SvmType.NU_SVC &&
svm_type != SvmType.ONE_CLASS &&
svm_type != SvmType.EPSILON_SVR &&
svm_type != SvmType.NU_SVR)
return "unknown svm type";
// kernel_type, degree

KernelType kernel_type = param.KernelType;

if (param.Gamma < 0)
if (kernel_type != KernelType.LINEAR &&
kernel_type != KernelType.POLY &&
kernel_type != KernelType.RBF &&
kernel_type != KernelType.SIGMOID &&
kernel_type != KernelType.PRECOMPUTED)
return "unknown kernel type";

if ((kernel_type == KernelType.POLY ||
kernel_type == KernelType.RBF ||
kernel_type == KernelType.SIGMOID) &&
param.Gamma < 0)
return "gamma < 0";

if (param.Degree < 0)
Expand Down