gov.sandia.cognition.learning.algorithm.perceptron
Class Perceptron

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<Vectorizable,Boolean,LinearBinaryCategorizer>
                      extended by gov.sandia.cognition.learning.algorithm.perceptron.Perceptron
All Implemented Interfaces:
AnytimeAlgorithm<LinearBinaryCategorizer>, IterativeAlgorithm, MeasurablePerformanceAlgorithm, StoppableAlgorithm, AnytimeBatchLearner<Collection<? extends InputOutputPair<? extends Vectorizable,Boolean>>,LinearBinaryCategorizer>, BatchLearner<Collection<? extends InputOutputPair<? extends Vectorizable,Boolean>>,LinearBinaryCategorizer>, SupervisedBatchLearner<Vectorizable,Boolean,LinearBinaryCategorizer>, VectorFactoryContainer, CloneableSerializable, Serializable, Cloneable

@CodeReview(reviewer="Kevin R. Dixon",
            date="2008-07-23",
            changesNeeded=false,
            comments={"Added PublicationReference to Wikiepedia article.","Minor changes to javadoc.","Looks fine."})
@PublicationReference(author="Wikipedia",
                      title="Perceptron Learning algorithm",
                      type=WebPage,
                      year=2008,
                      url="http://en.wikipedia.org/wiki/Perceptron#Learning_algorithm")
public class Perceptron
extends AbstractAnytimeSupervisedBatchLearner<Vectorizable,Boolean,LinearBinaryCategorizer>
implements MeasurablePerformanceAlgorithm, CloneableSerializable, VectorFactoryContainer

The Perceptron class implements the standard Perceptron learning algorithm that learns a binary classifier based on vector input. This implementation also allows for margins to be defined in learning in order to find a hyperplane.

Since:
2.0
Author:
Justin Basilico
See Also:
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
Perceptron()
          Creates a new instance of Perceptron.
Perceptron(int maxIterations)
          Creates a new instance of Perceptron with the given maximum number of iterations.
Perceptron(int maxIterations, double marginPositive, double marginNegative)
          Creates a new instance of Perceptron with the given parameters
Perceptron(int maxIterations, double marginPositive, double marginNegative, VectorFactory<?> vectorFactory)
          Creates a new instance of Perceptron with the given parameters
 
Method Summary
protected  void cleanupAlgorithm()
          Called to clean up the learning algorithm's state after learning has finished.
 Perceptron clone()
          This makes public the clone method on the Object class and removes the exception that it throws.
 int getErrorCount()
          Gets the error count of the most recent iteration.
 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.
 LinearBinaryCategorizer getResult()
          Gets the current result of the algorithm.
 VectorFactory<?> getVectorFactory()
          Gets the vector factory the object to use to create new 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 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(LinearBinaryCategorizer result)
          Sets the object currently being result.
 void setVectorFactory(VectorFactory<?> vectorFactory)
          Sets the VectorFactory used to create the weight vector.
protected  boolean step()
          Called to take a single step of the learning algorithm.
 
Methods inherited from class gov.sandia.cognition.learning.algorithm.AbstractAnytimeBatchLearner
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.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

Perceptron

public Perceptron()
Creates a new instance of Perceptron.


Perceptron

public Perceptron(int maxIterations)
Creates a new instance of Perceptron with the given maximum number of iterations.

Parameters:
maxIterations - The maximum number of iterations.

Perceptron

public Perceptron(int maxIterations,
                  double marginPositive,
                  double marginNegative)
Creates a new instance of Perceptron with the given parameters

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

Perceptron

public Perceptron(int maxIterations,
                  double marginPositive,
                  double marginNegative,
                  VectorFactory<?> vectorFactory)
Creates a new instance of Perceptron with the given parameters

Parameters:
maxIterations - The maximum number of iterations.
marginPositive - The positive margin to enforce.
marginNegative - The negative margin to enforce.
vectorFactory - The VectorFactory to use to create the weight vector.
Method Detail

clone

public Perceptron clone()
Description copied from class: AbstractCloneableSerializable
This makes public the clone method on the Object class and removes the exception that it throws. Its default behavior is to automatically create a clone of the exact type of object that the clone is called on and to copy all primitives but to keep all references, which means it is a shallow copy. Extensions of this class may want to override this method (but call super.clone() to implement a "smart copy". That is, to target the most common use case for creating a copy of the object. Because of the default behavior being a shallow copy, extending classes only need to handle fields that need to have a deeper copy (or those that need to be reset). Some of the methods in ObjectUtil may be helpful in implementing a custom clone method. Note: The contract of this method is that you must use super.clone() as the basis for your implementation.

Specified by:
clone in interface CloneableSerializable
Overrides:
clone in class AbstractAnytimeBatchLearner<Collection<? extends InputOutputPair<? extends Vectorizable,Boolean>>,LinearBinaryCategorizer>
Returns:
A clone of this object.

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 Vectorizable,Boolean>>,LinearBinaryCategorizer>
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 Vectorizable,Boolean>>,LinearBinaryCategorizer>
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 Vectorizable,Boolean>>,LinearBinaryCategorizer>

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.

getVectorFactory

public VectorFactory<?> getVectorFactory()
Description copied from interface: VectorFactoryContainer
Gets the vector factory the object to use to create new vectors.

Specified by:
getVectorFactory in interface VectorFactoryContainer
Returns:
The vector factory.

setVectorFactory

public void setVectorFactory(VectorFactory<?> vectorFactory)
Sets the VectorFactory used to create the weight vector.

Parameters:
vectorFactory - The VectorFactory used to create the weight vector.

getResult

public LinearBinaryCategorizer getResult()
Description copied from interface: AnytimeAlgorithm
Gets the current result of the algorithm.

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

setResult

protected void setResult(LinearBinaryCategorizer 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.

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.