Package weka.classifiers.functions

# Class MultilayerPerceptron

java.lang.Object
weka.classifiers.AbstractClassifier
weka.classifiers.functions.MultilayerPerceptron

All Implemented Interfaces:
  `Serializable`, `Cloneable`, `Classifier`, `IterativeClassifier`, `BatchPredictor`, `CapabilitiesHandler`, `CapabilitiesIgnorer`, `CommandlineRunnable`, `OptionHandler`, `Randomizable`, `RevisionHandler`, `WeightedInstancesHandler`

public class MultilayerPerceptron extends AbstractClassifier implements OptionHandler, WeightedInstancesHandler, Randomizable, IterativeClassifier
A classifier that uses backpropagation to learn a multi-layer perceptron to classify instances. The network can be built by hand or set up using a simple heuristic. The network parameters can also be monitored and modified during training time. The nodes in this network are all sigmoid (except for when the class is numeric, in which case the output nodes become unthresholded linear units).

Valid options are:

     -L <learning rate>
      Learning rate for the backpropagation algorithm.
      (Value should be between 0 - 1, Default = 0.3).

     -M <momentum>
      Momentum rate for the backpropagation algorithm.
      (Value should be between 0 - 1, Default = 0.2).

     -N <number of epochs>
      Number of epochs to train through.
      (Default = 500).

     -V <percentage size of validation set>
      Percentage size of validation set to use to terminate
      training (if this is non zero it can pre-empt num of epochs.
      (Value should be between 0 - 100, Default = 0).

     -S <seed>
      The value used to seed the random number generator
      (Value should be >= 0 and and a long, Default = 0).

     -E <threshold for number of consecutive errors>
      The number of consecutive increases of error allowed for validation
      testing before training terminates.
      (Value should be > 0, Default = 20).

     -G
      GUI will be opened.
      (Use this to bring up a GUI).

     -A
      Autocreation of the network connections will NOT be done.
      (This will be ignored if -G is NOT set)

     -B
      A NominalToBinary filter will NOT automatically be used.
      (Set this to not use a NominalToBinary filter).

     -H <comma separated numbers for nodes on each layer>
      The hidden layers to be created for the network.
      (Value should be a list of comma separated Natural
      numbers or the letters 'a' = (attribs + classes) / 2,
      'i' = attribs, 'o' = classes, 't' = attribs .+ classes)
      for wildcard values, Default = a).

     -C
      Normalizing a numeric class will NOT be done.
      (Set this to not normalize the class if it's numeric).

     -I
      Normalizing the attributes will NOT be done.
      (Set this to not normalize the attributes).

     -R
      Reseting the network will NOT be allowed.
      (Set this to not allow the network to reset).

     -D
      Learning rate decay will occur.
      (Set this to cause the learning rate to decay).

Version:
  $Revision: 15923 $
Author:
  Malcolm Ware (mfw4@cs.waikato.ac.nz)
See Also:
  - Serialized Form

- ## Field Summary

  ### Fields inherited from class weka.classifiers.AbstractClassifier

  `BATCH_SIZE_DEFAULT, NUM_DECIMAL_PLACES_DEFAULT`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `MultilayerPerceptron()`
  The constructor.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `String`
  `autoBuildTipText()`
  `void`
  `blocker(boolean tf)`
  A function used to stop the code that called buildclassifier from continuing on before the user has finished the decision tree.
  `void`
  `buildClassifier(Instances i)`
  Call this function to build and train a neural network for the training data provided.
  `String`
  `decayTipText()`
  `double[]`
  `distributionForInstance(Instance i)`
  Call this function to predict the class of an instance once a classification model has been built with the buildClassifier call.
  `void`
  `done()`
  Signal end of iterating, useful for any house-keeping/cleanup
  `boolean`
  `getAutoBuild()`
  `Capabilities`
  `getCapabilities()`
  Returns default capabilities of the classifier.
  `boolean`
  `getDecay()`
  `boolean`
  `getGUI()`
  `String`
  `getHiddenLayers()`
  `double`
  `getLearningRate()`
  `double`
  `getMomentum()`
  `boolean`
  `getNominalToBinaryFilter()`
  `boolean`
  `getNormalizeAttributes()`
  `boolean`
  `getNormalizeNumericClass()`
  `String[]`
  `getOptions()`
  Gets the current settings of NeuralNet.
  `boolean`
  `getReset()`
  `boolean`
  `getResume()`
  Returns true if the model is to be finalized (or has been finalized) after training.
  `String`
  `getRevision()`
  Returns the revision string.
  `int`
  `getSeed()`
  Gets the seed for the random number generations
  `int`
  `getTrainingTime()`
  `int`
  `getValidationSetSize()`
  `int`
  `getValidationThreshold()`
  `String`
  `globalInfo()`
  This will return a string describing the classifier.
  `String`
  `GUITipText()`
  `String`
  `hiddenLayersTipText()`
  `void`
  `initializeClassifier(Instances data)`
  Initializes an iterative classifier.
  `String`
  `learningRateTipText()`
  `Enumeration<Option>`
  `listOptions()`
  Returns an enumeration describing the available options.
  `static void`
  `main(String[] argv)`
  Main method for testing this class.
  `String`
  `momentumTipText()`
  `boolean`
  `next()`
  Performs one iteration.
  `String`
  `nominalToBinaryFilterTipText()`
  `String`
  `normalizeAttributesTipText()`
  `String`
  `normalizeNumericClassTipText()`
  `String`
  `resetTipText()`
  `String`
  `resumeTipText()`
  Tool tip text for resume property
  `String`
  `seedTipText()`
  `void`
  `setAutoBuild(boolean a)`
  This will set whether the network is automatically built or if it is left up to the user.
  `void`
  `setDecay(boolean d)`
  `void`
  `setGUI(boolean a)`
  This will set whether A GUI is brought up to allow interaction by the user with the neural network during training.
  `void`
  `setHiddenLayers(String h)`
  This will set what the hidden layers are made up of when auto build is enabled.
  `void`
  `setLearningRate(double l)`
  The learning rate can be set using this command.
  `void`
  `setMomentum(double m)`
  The momentum can be set using this command.
  `void`
  `setNominalToBinaryFilter(boolean f)`
  `void`
  `setNormalizeAttributes(boolean a)`
  `void`
  `setNormalizeNumericClass(boolean c)`
  `void`
  `setOptions(String[] options)`
  Parses a given list of options.
  `void`
  `setReset(boolean r)`
  This sets the network up to be able to reset itself with the current settings and the learning rate at half of what it is currently.
  `void`
  `setResume(boolean resume)`
  If called with argument true, then the next time done() is called the model is effectively "frozen" and no further iterations can be performed
  `void`
  `setSeed(int l)`
  This seeds the random number generator, that is used when a random number is needed for the network.
  `void`
  `setTrainingTime(int n)`
  Set the number of training epochs to perform.
  `void`
  `setValidationSetSize(int a)`
  This will set the size of the validation set.
  `void`
  `setValidationThreshold(int t)`
  This sets the threshold to use for when validation testing is being done.
  `String`
  `toString()`
  `String`
  `trainingTimeTipText()`
  `String`
  `validationSetSizeTipText()`
  `String`
  `validationThresholdTipText()`

  ### Methods inherited from class weka.classifiers.AbstractClassifier

  `batchSizeTipText, classifyInstance, debugTipText, distributionsForInstances, doNotCheckCapabilitiesTipText, forName, getBatchSize, getDebug, getDoNotCheckCapabilities, getNumDecimalPlaces, implementsMoreEfficientBatchPrediction, makeCopies, makeCopy, numDecimalPlacesTipText, postExecution, preExecution, run, runClassifier, setBatchSize, setDebug, setDoNotCheckCapabilities, setNumDecimalPlaces`

  ### Methods inherited from class java.lang.Object

  `equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface weka.classifiers.Classifier

  `classifyInstance`

- ## Constructor Details

  - ### MultilayerPerceptron

    public MultilayerPerceptron()
    The constructor.

- ## Method Details

  - ### main

    public static void main(String[] argv)
    Main method for testing this class.
    Parameters:
      `argv` - should contain command line options (see setOptions)

  - ### setDecay

    public void setDecay(boolean d)
    Parameters:
      `d` - True if the learning rate should decay.

  - ### getDecay

    public boolean getDecay()
    Returns:
      the flag for having the learning rate decay.

  - ### setReset

    public void setReset(boolean r)
    This sets the network up to be able to reset itself with the current settings and the learning rate at half of what it is currently. This will only happen if the network creates NaN or infinite errors. Also this will continue to happen until the network is trained properly. The learning rate will also get set back to it's original value at the end of this. This can only be set to true if the GUI is not brought up.
    Parameters:
      `r` - True if the network should restart with it's current options and set the learning rate to half what it currently is.

  - ### getReset

    public boolean getReset()
    Returns:
      The flag for reseting the network.

  - ### setNormalizeNumericClass

    public void setNormalizeNumericClass(boolean c)
    Parameters:
      `c` - True if the class should be normalized (the class will only ever be normalized if it is numeric). (Normalization puts the range between -1 - 1).

  - ### getNormalizeNumericClass

    public boolean getNormalizeNumericClass()
    Returns:
      The flag for normalizing a numeric class.

  - ### setNormalizeAttributes

    public void setNormalizeAttributes(boolean a)
    Parameters:
      `a` - True if the attributes should be normalized (even nominal attributes will get normalized here) (range goes between -1 - 1).

  - ### getNormalizeAttributes

    public boolean getNormalizeAttributes()
    Returns:
      The flag for normalizing attributes.

  - ### setNominalToBinaryFilter

    public void setNominalToBinaryFilter(boolean f)
    Parameters:
      `f` - True if a nominalToBinary filter should be used on the data.

  - ### getNominalToBinaryFilter

    public boolean getNominalToBinaryFilter()
    Returns:
      The flag for nominal to binary filter use.

  - ### setSeed

    public void setSeed(int l)
    This seeds the random number generator, that is used when a random number is needed for the network.
    Specified by:
      `setSeed` in interface `Randomizable`
    Parameters:
      `l` - The seed.

  - ### getSeed

    public int getSeed()
    Description copied from interface: `Randomizable`
    Gets the seed for the random number generations
    Specified by:
      `getSeed` in interface `Randomizable`
    Returns:
      The seed for the random number generator.

  - ### setValidationThreshold

    public void setValidationThreshold(int t)
    This sets the threshold to use for when validation testing is being done. It works by ending testing once the error on the validation set has consecutively increased a certain number of times.
    Parameters:
      `t` - The threshold to use for this.

  - ### getValidationThreshold

    public int getValidationThreshold()
    Returns:
      The threshold used for validation testing.

  - ### setLearningRate

    public void setLearningRate(double l)
    The learning rate can be set using this command. NOTE That this is a static variable so it affect all networks that are running. Must be greater than 0 and no more than 1.
    Parameters:
      `l` - The New learning rate.

  - ### getLearningRate

    public double getLearningRate()
    Returns:
      The learning rate for the nodes.

  - ### setMomentum

    public void setMomentum(double m)
    The momentum can be set using this command. THE same conditions apply to this as to the learning rate.
    Parameters:
      `m` - The new Momentum.

  - ### getMomentum

    public double getMomentum()
    Returns:
      The momentum for the nodes.

  - ### setAutoBuild

    public void setAutoBuild(boolean a)
    This will set whether the network is automatically built or if it is left up to the user. (there is nothing to stop a user from altering an autobuilt network however).
    Parameters:
      `a` - True if the network should be auto built.

  - ### getAutoBuild

    public boolean getAutoBuild()
    Returns:
      The auto build state.

  - ### setHiddenLayers

    public void setHiddenLayers(String h)
    This will set what the hidden layers are made up of when auto build is enabled. Note to have no hidden units, just put a single 0, Any more 0's will indicate that the string is badly formed and make it unaccepted. Negative numbers, and floats will do the same. There are also some wildcards. These are 'a' = (number of attributes + number of classes) / 2, 'i' = number of attributes, 'o' = number of classes, and 't' = number of attributes + number of classes.
    Parameters:
      `h` - A string with a comma seperated list of numbers. Each number is the number of nodes to be on a hidden layer.

  - ### getHiddenLayers

    public String getHiddenLayers()
    Returns:
      A string representing the hidden layers, each number is the number of nodes on a hidden layer.

  - ### setGUI

    public void setGUI(boolean a)
    This will set whether A GUI is brought up to allow interaction by the user with the neural network during training.
    Parameters:
      `a` - True if gui should be created.

  - ### getGUI

    public boolean getGUI()
    Returns:
      The true if should show gui.

  - ### setValidationSetSize

    public void setValidationSetSize(int a)
    This will set the size of the validation set.
    Parameters:
      `a` - The size of the validation set, as a percentage of the whole.

  - ### getValidationSetSize

    public int getValidationSetSize()
    Returns:
      The percentage size of the validation set.

  - ### setTrainingTime

    public void setTrainingTime(int n)
    Set the number of training epochs to perform. Must be greater than 0.
    Parameters:
      `n` - The number of epochs to train through.

  - ### getTrainingTime

    public int getTrainingTime()
    Returns:
      The number of epochs to train through.

  - ### blocker

    public void blocker(boolean tf)
    A function used to stop the code that called buildclassifier from continuing on before the user has finished the decision tree.
    Parameters:
      `tf` - True to stop the thread, False to release the thread that is waiting there (if one).

  - ### getCapabilities

    public Capabilities getCapabilities()
    Returns default capabilities of the classifier.
    Specified by:
      `getCapabilities` in interface `CapabilitiesHandler`
    Specified by:
      `getCapabilities` in interface `Classifier`
    Overrides:
      `getCapabilities` in class `AbstractClassifier`
    Returns:
      the capabilities of this classifier
    See Also:
      - `Capabilities`

  - ### initializeClassifier

    public void initializeClassifier(Instances data) throws Exception
    Initializes an iterative classifier.
    Specified by:
      `initializeClassifier` in interface `IterativeClassifier`
    Parameters:
      `data` - the instances to be used in induction
    Throws:
      `Exception` - if the model cannot be initialized

  - ### next

    public boolean next() throws Exception
    Performs one iteration.
    Specified by:
      `next` in interface `IterativeClassifier`
    Returns:
      false if no further iterations could be performed, true otherwise
    Throws:
      `Exception` - if this iteration fails for unexpected reasons

  - ### done

    public void done() throws Exception
    Signal end of iterating, useful for any house-keeping/cleanup
    Specified by:
      `done` in interface `IterativeClassifier`
    Throws:
      `Exception` - if cleanup fails

  - ### resumeTipText

    public String resumeTipText()
    Tool tip text for resume property
    Returns:
      the tool tip text for the finalize property

  - ### setResume

    public void setResume(boolean resume)
    If called with argument true, then the next time done() is called the model is effectively "frozen" and no further iterations can be performed
    Specified by:
      `setResume` in interface `IterativeClassifier`
    Parameters:
      `resume` - true if the model is to be finalized after performing iterations

  - ### getResume

    public boolean getResume()
    Returns true if the model is to be finalized (or has been finalized) after training.
    Specified by:
      `getResume` in interface `IterativeClassifier`
    Returns:
      the current value of finalize

  - ### buildClassifier

    public void buildClassifier(Instances i) throws Exception
    Call this function to build and train a neural network for the training data provided.
    Specified by:
      `buildClassifier` in interface `Classifier`
    Parameters:
      `i` - The training data.
    Throws:
      `Exception` - if can't build classification properly.

  - ### distributionForInstance

    public double[] distributionForInstance(Instance i) throws Exception
    Call this function to predict the class of an instance once a classification model has been built with the buildClassifier call.
    Specified by:
      `distributionForInstance` in interface `Classifier`
    Overrides:
      `distributionForInstance` in class `AbstractClassifier`
    Parameters:
      `i` - The instance to classify.
    Returns:
      A double array filled with the probabilities of each class type.
    Throws:
      `Exception` - if can't classify instance.

  - ### listOptions

    public Enumeration<Option> listOptions()
    Returns an enumeration describing the available options.
    Specified by:
      `listOptions` in interface `OptionHandler`
    Overrides:
      `listOptions` in class `AbstractClassifier`
    Returns:
      an enumeration of all the available options.

  - ### setOptions

    public void setOptions(String[] options) throws Exception
    Parses a given list of options.

    Valid options are:

         -L <learning rate>
          Learning rate for the backpropagation algorithm.
          (Value should be between 0 - 1, Default = 0.3).

         -M <momentum>
          Momentum rate for the backpropagation algorithm.
          (Value should be between 0 - 1, Default = 0.2).

         -N <number of epochs>
          Number of epochs to train through.
          (Default = 500).

         -V <percentage size of validation set>
          Percentage size of validation set to use to terminate
          training (if this is non zero it can pre-empt num of epochs.
          (Value should be between 0 - 100, Default = 0).

         -S <seed>
          The value used to seed the random number generator
          (Value should be >= 0 and and a long, Default = 0).

         -E <threshold for number of consecutive errors>
          The number of consecutive increases of error allowed for validation
          testing before training terminates.
          (Value should be > 0, Default = 20).

         -G
          GUI will be opened.
          (Use this to bring up a GUI).

         -A
          Autocreation of the network connections will NOT be done.
          (This will be ignored if -G is NOT set)

         -B
          A NominalToBinary filter will NOT automatically be used.
          (Set this to not use a NominalToBinary filter).

         -H <comma separated numbers for nodes on each layer>
          The hidden layers to be created for the network.
          (Value should be a list of comma separated Natural
          numbers or the letters 'a' = (attribs + classes) / 2,
          'i' = attribs, 'o' = classes, 't' = attribs .+ classes)
          for wildcard values, Default = a).

         -C
          Normalizing a numeric class will NOT be done.
          (Set this to not normalize the class if it's numeric).

         -I
          Normalizing the attributes will NOT be done.
          (Set this to not normalize the attributes).

         -R
          Resetting the network will NOT be allowed.
          (Set this to not allow the network to reset).

         -D
          Learning rate decay will occur.
          (Set this to cause the learning rate to decay).

    Specified by:
      `setOptions` in interface `OptionHandler`
    Overrides:
      `setOptions` in class `AbstractClassifier`
    Parameters:
      `options` - the list of options as an array of strings
    Throws:
      `Exception` - if an option is not supported

  - ### getOptions

    public String[] getOptions()
    Gets the current settings of NeuralNet.
    Specified by:
      `getOptions` in interface `OptionHandler`
    Overrides:
      `getOptions` in class `AbstractClassifier`
    Returns:
      an array of strings suitable for passing to setOptions()

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`
    Returns:
      string describing the model.

  - ### globalInfo

    public String globalInfo()
    This will return a string describing the classifier.
    Returns:
      The string.

  - ### learningRateTipText

    public String learningRateTipText()
    Returns:
      a string to describe the learning rate option.

  - ### momentumTipText

    public String momentumTipText()
    Returns:
      a string to describe the momentum option.

  - ### autoBuildTipText

    public String autoBuildTipText()
    Returns:
      a string to describe the AutoBuild option.

  - ### seedTipText

    public String seedTipText()
    Returns:
      a string to describe the random seed option.

  - ### validationThresholdTipText

    public String validationThresholdTipText()
    Returns:
      a string to describe the validation threshold option.

  - ### GUITipText

    public String GUITipText()
    Returns:
      a string to describe the GUI option.

  - ### validationSetSizeTipText

    public String validationSetSizeTipText()
    Returns:
      a string to describe the validation size option.

  - ### trainingTimeTipText

    public String trainingTimeTipText()
    Returns:
      a string to describe the learning rate option.

  - ### nominalToBinaryFilterTipText

    public String nominalToBinaryFilterTipText()
    Returns:
      a string to describe the nominal to binary option.

  - ### hiddenLayersTipText

    public String hiddenLayersTipText()
    Returns:
      a string to describe the hidden layers in the network.

  - ### normalizeNumericClassTipText

    public String normalizeNumericClassTipText()
    Returns:
      a string to describe the nominal to binary option.

  - ### normalizeAttributesTipText

    public String normalizeAttributesTipText()
    Returns:
      a string to describe the nominal to binary option.

  - ### resetTipText

    public String resetTipText()
    Returns:
      a string to describe the Reset option.

  - ### decayTipText

    public String decayTipText()
    Returns:
      a string to describe the Decay option.

  - ### getRevision

    public String getRevision()
    Returns the revision string.
    Specified by:
      `getRevision` in interface `RevisionHandler`
    Overrides:
      `getRevision` in class `AbstractClassifier`
    Returns:
      the revision