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

Commit

Permalink
Merge pull request #280 from fch-aa/Fix-SMO-CacheSize
Browse files Browse the repository at this point in the history
Fixes SMO's CacheSize being overridden
  • Loading branch information
cesarsouza authored Aug 25, 2016
2 parents d041d80 + b6e306b commit e98bb92
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 @@ -402,7 +402,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 @@ -478,7 +478,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 @@ -575,7 +575,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 @@ -421,6 +421,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 e98bb92

Please sign in to comment.