Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
Fixes SMO's CacheSize being overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
fch-aa committed Aug 18, 2016
1 parent 7309a56 commit b6e306b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ public abstract class BaseSequentialMinimalOptimization<TModel, TKernel, TInput>
// Error cache to speed up computations
private double[] errors;

private int cacheSize;
private int cacheSize = -1;
private KernelFunctionCache<TKernel, TInput> kernelCache;

private SelectionStrategy strategy = SelectionStrategy.WorstPair;
Expand Down Expand Up @@ -569,7 +569,7 @@ public int CacheSize
get { return cacheSize; }
set
{
if (cacheSize < 0)
if (value < 0)
throw new ArgumentOutOfRangeException("value");
this.cacheSize = value;
}
Expand Down Expand Up @@ -666,7 +666,10 @@ protected override void InnerRun()
this.errors = new double[samples];

// Kernel cache
this.cacheSize = samples;
if (this.cacheSize == -1)
{
this.cacheSize = samples;
}


// Lagrange multipliers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,63 @@ public void LearnTest5()
}
}

[Test]
public void Learn_UnspecifiedCacheSize_CacheSizeEqualsInputLength()
{
double[][] inputs =
{
new double[] { -1, -1 },
new double[] { -1, 1 },
new double[] { 1, -1 },
new double[] { 1, 1 }
};

int[] xor =
{
-1,
1,
1,
-1
};

KernelSupportVectorMachine svm = new KernelSupportVectorMachine(new Polynomial(2), inputs[0].Length);
SequentialMinimalOptimization smo = new SequentialMinimalOptimization(svm, inputs, xor);

smo.Run();

Assert.AreEqual(smo.CacheSize, inputs.Length);
}

[Test]
public void Learn_CacheSizeZero_CacheSizeShouldBeZero()
{
double[][] inputs =
{
new double[] { -1, -1 },
new double[] { -1, 1 },
new double[] { 1, -1 },
new double[] { 1, 1 }
};

int[] xor =
{
-1,
1,
1,
-1
};

KernelSupportVectorMachine svm = new KernelSupportVectorMachine(new Polynomial(2), inputs[0].Length);
SequentialMinimalOptimization smo = new SequentialMinimalOptimization(svm, inputs, xor)
{
CacheSize = 0
};

smo.Run();

Assert.AreEqual(smo.CacheSize, 0);
}

[Test]
public void LargeLearningTest1()
{
Expand Down

0 comments on commit b6e306b

Please sign in to comment.