Class Perceptron


  • public class Perceptron
    extends java.lang.Object
    A class that will act as a Perceptron that takes in inputs and then generates an outputs based on previous training
    Author:
    Bhagat
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private Function<java.lang.Double,​java.lang.Integer> activationFunction
      The activation function that is run on the output data to constrain the data to known limits
      private double bias
      The bias, or the weight for the invisible input of 1
      private double biasLearningRateFactor
      The "input" for the bias weight
      static Function<java.lang.Double,​java.lang.Integer> defaultActivationFunction
      default activation function to use if something is positive or negative to 1 or 0
      static double defaultBiasLearningRateFactor
      default bias learning rate factor
      static double defaultLearningRate
      default learning rate
      static double defaultLearningRateFactor
      default learning rate factor
      static int defaultN
      default number of inputs
      private double learningRate
      The step that it takes in the direction of the error
      private double learningRateFactor
      A factor that the learning rate is multiplied with each time the perceptron is trained
      private Vector weights
      A vector that holds the weights for the inputs (for the weighted sum)
    • Constructor Summary

      Constructors 
      Constructor Description
      Perceptron()
      constructs a Perceptron with all the default parameters
      Perceptron​(int n)
      constructs a Perceptron with the default learning rate factor, activation function, bias learning rate, and learning rate
      Perceptron​(int n, double learningRate)
      constructs a Perceptron with the default learning rate factor, bias learning rate, and activation function
      Perceptron​(int n, double learningRate, double learningRateFactor)
      constructs a Perceptron with the default bias learning rate, and activation function
      Perceptron​(int n, double learningRate, double learningRateFactor, double biasLearningRateFactor)
      constructs a Perceptron with a default activation function
      Perceptron​(int n, double learningRate, double learningRateFactor, double biasLearningRateFactor, Function<java.lang.Double,​java.lang.Integer> activationFunction)
      constructs the Perceptron with custom parameters
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Function<java.lang.Double,​java.lang.Integer> getActivationFunction()  
      double getBias()  
      double getLearningRate()  
      double getLearningRateFactor()  
      Vector getWeights()  
      int guess​(double[] inputs)
      takes in an input array and converts it to a vector and sends it to guess(Vector)
      int guess​(Vector inputs)
      takes a Vector for inputs and then guesses the output
      void setActivationFunction​(Function<java.lang.Double,​java.lang.Integer> activationFunction)  
      void setLearningRate​(double learningRate)  
      void setLearningRateFactor​(double learningRateFactor)  
      void train​(double[] inputs, int target)
      converts the input array into a Vector and sends it to the train(Vector, integer) method
      void train​(Vector inputs, int target)
      takes in a Vector for the inputs and then compares the guess and the target output and adjusts the weights and bias accordingly
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • weights

        private Vector weights
        A vector that holds the weights for the inputs (for the weighted sum)
      • learningRate

        private double learningRate
        The step that it takes in the direction of the error
      • activationFunction

        private Function<java.lang.Double,​java.lang.Integer> activationFunction
        The activation function that is run on the output data to constrain the data to known limits
      • learningRateFactor

        private double learningRateFactor
        A factor that the learning rate is multiplied with each time the perceptron is trained
      • bias

        private double bias
        The bias, or the weight for the invisible input of 1
      • biasLearningRateFactor

        private double biasLearningRateFactor
        The "input" for the bias weight
      • defaultLearningRate

        public static final double defaultLearningRate
        default learning rate
        See Also:
        Constant Field Values
      • defaultActivationFunction

        public static final Function<java.lang.Double,​java.lang.Integer> defaultActivationFunction
        default activation function to use if something is positive or negative to 1 or 0
      • defaultLearningRateFactor

        public static final double defaultLearningRateFactor
        default learning rate factor
        See Also:
        Constant Field Values
      • defaultBiasLearningRateFactor

        public static final double defaultBiasLearningRateFactor
        default bias learning rate factor
        See Also:
        Constant Field Values
    • Constructor Detail

      • Perceptron

        public Perceptron​(int n,
                          double learningRate,
                          double learningRateFactor,
                          double biasLearningRateFactor,
                          Function<java.lang.Double,​java.lang.Integer> activationFunction)
        constructs the Perceptron with custom parameters
        Parameters:
        n - size of input and weight vectors
        learningRate - the learning rate
        learningRateFactor - the factor for the learning rate to multiply on training
        biasLearningRateFactor - the factor which will be multiplied with the learning rate when adding to the bias
        activationFunction - the activation function
      • Perceptron

        public Perceptron​(int n,
                          double learningRate,
                          double learningRateFactor,
                          double biasLearningRateFactor)
        constructs a Perceptron with a default activation function
        Parameters:
        n - size of input and weight vectors
        learningRate - the learning rate
        learningRateFactor - the learning rate factor
        biasLearningRateFactor - the bias learning rate
      • Perceptron

        public Perceptron​(int n,
                          double learningRate,
                          double learningRateFactor)
        constructs a Perceptron with the default bias learning rate, and activation function
        Parameters:
        n - size of input and weight vectors
        learningRate - the learning rate
        learningRateFactor - the learning rate factor
      • Perceptron

        public Perceptron​(int n,
                          double learningRate)
        constructs a Perceptron with the default learning rate factor, bias learning rate, and activation function
        Parameters:
        n - size of input and weight vectors
        learningRate - the learning rate
      • Perceptron

        public Perceptron​(int n)
        constructs a Perceptron with the default learning rate factor, activation function, bias learning rate, and learning rate
        Parameters:
        n - size of input and weight vectors
      • Perceptron

        public Perceptron()
        constructs a Perceptron with all the default parameters
    • Method Detail

      • guess

        public int guess​(Vector inputs)
        takes a Vector for inputs and then guesses the output
        Parameters:
        inputs - the inputs
        Returns:
        the guess of what the output should be
      • guess

        public int guess​(double[] inputs)
        takes in an input array and converts it to a vector and sends it to guess(Vector)
        Parameters:
        inputs - the input array
        Returns:
        the guess of what the output should be
      • train

        public void train​(Vector inputs,
                          int target)
        takes in a Vector for the inputs and then compares the guess and the target output and adjusts the weights and bias accordingly
        Parameters:
        inputs - the inputs
        target - the target output
      • train

        public void train​(double[] inputs,
                          int target)
        converts the input array into a Vector and sends it to the train(Vector, integer) method
        Parameters:
        inputs - the inputs array
        target - the target output
      • getLearningRate

        public double getLearningRate()
        Returns:
        the learningRate
      • setLearningRate

        public void setLearningRate​(double learningRate)
        Parameters:
        learningRate - the learningRate to set
      • getActivationFunction

        public Function<java.lang.Double,​java.lang.Integer> getActivationFunction()
        Returns:
        the activationFunction
      • setActivationFunction

        public void setActivationFunction​(Function<java.lang.Double,​java.lang.Integer> activationFunction)
        Parameters:
        activationFunction - the activationFunction to set
      • getLearningRateFactor

        public double getLearningRateFactor()
        Returns:
        the learningRateFactor
      • setLearningRateFactor

        public void setLearningRateFactor​(double learningRateFactor)
        Parameters:
        learningRateFactor - the learningRateFactor to set
      • getWeights

        public Vector getWeights()
        Returns:
        the weights
      • getBias

        public double getBias()
        Returns:
        the bias