gov.sandia.cognition.learning.algorithm.perceptron.kernel
Class KernelPerceptron<InputType>

java.lang.Object
  extended by gov.sandia.cognition.util.AbstractCloneableSerializable
      extended by gov.sandia.cognition.algorithm.AbstractIterativeAlgorithm
          extended by gov.sandia.cognition.algorithm.AbstractAnytimeAlgorithm<ResultType>
              extended by gov.sandia.cognition.learning.algorithm.AbstractAnytimeBatchLearner<Collection<? extends InputOutputPair<? extends InputType,OutputType>>,ResultType>
                  extended by gov.sandia.cognition.learning.algorithm.AbstractAnytimeSupervisedBatchLearner<InputType,Boolean,DefaultKernelBinaryCategorizer<InputType>>
                      extended by gov.sandia.cognition.learning.algorithm.perceptron.kernel.KernelPerceptron<InputType>
Type Parameters:
InputType - Input class of the InputOutputPairs
All Implemented Interfaces:
AnytimeAlgorithm<DefaultKernelBinaryCategorizer<InputType>>, IterativeAlgorithm, MeasurablePerformanceAlgorithm, StoppableAlgorithm, AnytimeBatchLearner<Collection<? extends InputOutputPair<? extends InputType,Boolean>>,DefaultKernelBinaryCategorizer<InputType>>, BatchLearner<Collection<? extends InputOutputPair<? extends InputType,Boolean>>,DefaultKernelBinaryCategorizer<InputType>>, SupervisedBatchLearner<InputType,Boolean,DefaultKernelBinaryCategorizer<InputType>>, CloneableSerializable, Serializable, Cloneable

@CodeReview(reviewer="Kevin R. Dixon",
            date="2008-07-23",
            changesNeeded=false,
            comments={"Added PublicationReference to the original article.","Minor changes to javadoc.","Looks fine."})
@PublicationReference(author={"Yoav Freund","Robert E. Schapire"},
                      title="Large margin classification using the perceptron algorithm",
                      publication="Machine Learning",
                      type=Journal,
                      year=1999,
                      notes="Volume 37, Number 3",
                      pages={277,296},
                      url="http://www.cs.ucsd.edu/~yfreund/papers/LargeMarginsUsingPerceptron.pdf")
public class KernelPerceptron<InputType>
extends AbstractAnytimeSupervisedBatchLearner<InputType,Boolean,DefaultKernelBinaryCategorizer<InputType>>
implements MeasurablePerformanceAlgorithm

The KernelPerceptron class implements the kernel version of the Perceptron algorithm. That is, it replaces the inner-product used in the standard Perceptron algorithm with a kernel method. This allows the algorithm to be used with data and a kernel that would map it into a high-dimensional space but does not need to since the kernel can compute the inner-product in the high-dimensional space without actually creating the vectors for it.

Since:
2.0
Author:
Justin Basilico
See Also:
Perceptron, Serialized Form

Field Summary
static double DEFAULT_MARGIN_NEGATIVE
          The default negative margin, 0.0.
static double DEFAULT_MARGIN_POSITIVE
          The default positive margin, 0.0.
static int DEFAULT_MAX_ITERATIONS
          The default maximum number of iterations, 100.
 
Fields inherited from class gov.sandia.cognition.learning.algorithm.AbstractAnytimeBatchLearner
data, keepGoing
 
Fields inherited from class gov.sandia.cognition.algorithm.AbstractAnytimeAlgorithm
maxIterations
 
Fields inherited from class gov.sandia.cognition.algorithm.AbstractIterativeAlgorithm
DEFAULT_ITERATION, iteration
 
Constructor Summary
KernelPerceptron()
          Creates a new instance of KernelPerceptron.
KernelPerceptron(Kernel<? super InputType> kernel)
          Creates a new KernelPerceptron with the given kernel.
KernelPerceptron(Kernel<? super InputType> kernel, int maxIterations)
          Creates a new KernelPerceptron with the given kernel and maximum number of iterations.
KernelPerceptron(Kernel<? super InputType> kernel, int maxIterations, double marginPositive, double marginNegative)
          Creates a new KernelPerceptron with the given parameters.
 
Method Summary
protected  void cleanupAlgorithm()
          Called to clean up the learning algorithm's state after learning has finished.
 int getErrorCount()
          Gets the error count of the most recent iteration.
 Kernel<? super InputType> getKernel()
          Gets the kernel to use.
 double getMarginNegative()
          Gets the negative margin that is enforced.
 double getMarginPositive()
          Gets the positive margin that is enforced.
 NamedValue<Integer> getPerformance()
          Gets the name-value pair that describes the current performance of the algorithm.
 DefaultKernelBinaryCategorizer<InputType> getResult()
          Gets the current result of the algorithm.
protected  LinkedHashMap<InputOutputPair<? extends InputType,? extends Boolean>,DefaultWeightedValue<InputType>> getSupportsMap()
          Gets the mapping of examples to weight objects (support vectors).
protected  boolean initializeAlgorithm()
          Called to initialize the learning algorithm's state based on the data that is stored in the data field.
protected  void setErrorCount(int errorCount)
          Sets the error count of the most recent iteration.
 void setKernel(Kernel<? super InputType> kernel)
          Sets the kernel to use.
 void setMargin(double margin)
          Sets both the positive and negative margin to the same value.
 void setMarginNegative(double marginNegative)
          Sets the negative margin that is enforced.
 void setMarginPositive(double marginPositive)
          Sets the positive margin that is enforced.
protected  void setResult(DefaultKernelBinaryCategorizer<InputType> result)
          Sets the object currently being result.
protected  void setSupportsMap(LinkedHashMap<InputOutputPair<? extends InputType,? extends Boolean>,DefaultWeightedValue<InputType>> supportsMap)
          Gets the mapping of examples to weight objects (support vectors).
protected  boolean step()
          Called to take a single step of the learning algorithm.
 
Methods inherited from class gov.sandia.cognition.learning.algorithm.AbstractAnytimeBatchLearner
clone, getData, getKeepGoing, learn, setData, setKeepGoing, stop
 
Methods inherited from class gov.sandia.cognition.algorithm.AbstractAnytimeAlgorithm
getMaxIterations, isResultValid, setMaxIterations
 
Methods inherited from class gov.sandia.cognition.algorithm.AbstractIterativeAlgorithm
addIterativeAlgorithmListener, fireAlgorithmEnded, fireAlgorithmStarted, fireStepEnded, fireStepStarted, getIteration, getListeners, removeIterativeAlgorithmListener, setIteration, setListeners
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface gov.sandia.cognition.learning.algorithm.BatchLearner
learn
 
Methods inherited from interface gov.sandia.cognition.util.CloneableSerializable
clone
 
Methods inherited from interface gov.sandia.cognition.algorithm.AnytimeAlgorithm
getMaxIterations, setMaxIterations
 
Methods inherited from interface gov.sandia.cognition.algorithm.IterativeAlgorithm
addIterativeAlgorithmListener, getIteration, removeIterativeAlgorithmListener
 
Methods inherited from interface gov.sandia.cognition.algorithm.StoppableAlgorithm
isResultValid
 

Field Detail

DEFAULT_MAX_ITERATIONS

public static final int DEFAULT_MAX_ITERATIONS
The default maximum number of iterations, 100.

See Also:
Constant Field Values

DEFAULT_MARGIN_POSITIVE

public static final double DEFAULT_MARGIN_POSITIVE
The default positive margin, 0.0.

See Also:
Constant Field Values

DEFAULT_MARGIN_NEGATIVE

public static final double DEFAULT_MARGIN_NEGATIVE
The default negative margin, 0.0.

See Also:
Constant Field Values
Constructor Detail

KernelPerceptron

public KernelPerceptron()
Creates a new instance of KernelPerceptron.


KernelPerceptron

public KernelPerceptron(Kernel<? super InputType> kernel)
Creates a new KernelPerceptron with the given kernel.

Parameters:
kernel - The kernel to use.

KernelPerceptron

public KernelPerceptron(Kernel<? super InputType> kernel,
                        int maxIterations)
Creates a new KernelPerceptron with the given kernel and maximum number of iterations.

Parameters:
kernel - The kernel to use.
maxIterations - The maximum number of iterations.

KernelPerceptron

public KernelPerceptron(Kernel<? super InputType> kernel,
                        int maxIterations,
                        double marginPositive,
                        double marginNegative)
Creates a new KernelPerceptron with the given parameters.

Parameters:
kernel - The kernel to use.
maxIterations - The maximum number of iterations.
marginPositive - The positive margin to enforce.
marginNegative - The negative margin to enforce.
Method Detail

initializeAlgorithm

protected boolean initializeAlgorithm()
Description copied from class: AbstractAnytimeBatchLearner
Called to initialize the learning algorithm's state based on the data that is stored in the data field. The return value indicates if the algorithm can be run or not based on the initialization.

Specified by:
initializeAlgorithm in class AbstractAnytimeBatchLearner<Collection<? extends InputOutputPair<? extends InputType,Boolean>>,DefaultKernelBinaryCategorizer<InputType>>
Returns:
True if the learning algorithm can be run and false if it cannot.

step

protected boolean step()
Description copied from class: AbstractAnytimeBatchLearner
Called to take a single step of the learning algorithm.

Specified by:
step in class AbstractAnytimeBatchLearner<Collection<? extends InputOutputPair<? extends InputType,Boolean>>,DefaultKernelBinaryCategorizer<InputType>>
Returns:
True if another step can be taken and false it the algorithm should halt.

cleanupAlgorithm

protected void cleanupAlgorithm()
Description copied from class: AbstractAnytimeBatchLearner
Called to clean up the learning algorithm's state after learning has finished.

Specified by:
cleanupAlgorithm in class AbstractAnytimeBatchLearner<Collection<? extends InputOutputPair<? extends InputType,Boolean>>,DefaultKernelBinaryCategorizer<InputType>>

getKernel

public Kernel<? super InputType> getKernel()
Gets the kernel to use.

Returns:
The kernel to use.

setKernel

public void setKernel(Kernel<? super InputType> kernel)
Sets the kernel to use.

Parameters:
kernel - The kernel to use.

setMargin

public void setMargin(double margin)
Sets both the positive and negative margin to the same value.

Parameters:
margin - The new value for both the positive and negative margins.

getMarginPositive

public double getMarginPositive()
Gets the positive margin that is enforced.

Returns:
The positive margin that is enforced.

setMarginPositive

public void setMarginPositive(double marginPositive)
Sets the positive margin that is enforced.

Parameters:
marginPositive - The positive margin that is enforced.

getMarginNegative

public double getMarginNegative()
Gets the negative margin that is enforced.

Returns:
The negative margin that is enforced.

setMarginNegative

public void setMarginNegative(double marginNegative)
Sets the negative margin that is enforced.

Parameters:
marginNegative - The negative margin that is enforced.

getResult

public DefaultKernelBinaryCategorizer<InputType> getResult()
Description copied from interface: AnytimeAlgorithm
Gets the current result of the algorithm.

Specified by:
getResult in interface AnytimeAlgorithm<DefaultKernelBinaryCategorizer<InputType>>
Returns:
Current result of the algorithm.

setResult

protected void setResult(DefaultKernelBinaryCategorizer<InputType> result)
Sets the object currently being result.

Parameters:
result - The object currently being result.

getErrorCount

public int getErrorCount()
Gets the error count of the most recent iteration.

Returns:
The current error count.

setErrorCount

protected void setErrorCount(int errorCount)
Sets the error count of the most recent iteration.

Parameters:
errorCount - The current error count.

getSupportsMap

protected LinkedHashMap<InputOutputPair<? extends InputType,? extends Boolean>,DefaultWeightedValue<InputType>> getSupportsMap()
Gets the mapping of examples to weight objects (support vectors).

Returns:
The mapping of examples to weight objects.

setSupportsMap

protected void setSupportsMap(LinkedHashMap<InputOutputPair<? extends InputType,? extends Boolean>,DefaultWeightedValue<InputType>> supportsMap)
Gets the mapping of examples to weight objects (support vectors).

Parameters:
supportsMap - The mapping of examples to weight objects.

getPerformance

public NamedValue<Integer> getPerformance()
Description copied from interface: MeasurablePerformanceAlgorithm
Gets the name-value pair that describes the current performance of the algorithm. For most algorithms, this is the value that they are attempting to optimize.

Specified by:
getPerformance in interface MeasurablePerformanceAlgorithm
Returns:
The name-value pair that describes the current performance of the algorithm.