Package io.bhagat.ai.supervised
Class Perceptron
- java.lang.Object
-
- io.bhagat.ai.supervised.Perceptron
-
public class Perceptron extends java.lang.ObjectA 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>activationFunctionThe activation function that is run on the output data to constrain the data to known limitsprivate doublebiasThe bias, or the weight for the invisible input of 1private doublebiasLearningRateFactorThe "input" for the bias weightstatic Function<java.lang.Double,java.lang.Integer>defaultActivationFunctiondefault activation function to use if something is positive or negative to 1 or 0static doubledefaultBiasLearningRateFactordefault bias learning rate factorstatic doubledefaultLearningRatedefault learning ratestatic doubledefaultLearningRateFactordefault learning rate factorstatic intdefaultNdefault number of inputsprivate doublelearningRateThe step that it takes in the direction of the errorprivate doublelearningRateFactorA factor that the learning rate is multiplied with each time the perceptron is trainedprivate VectorweightsA 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 parametersPerceptron(int n)constructs a Perceptron with the default learning rate factor, activation function, bias learning rate, and learning ratePerceptron(int n, double learningRate)constructs a Perceptron with the default learning rate factor, bias learning rate, and activation functionPerceptron(int n, double learningRate, double learningRateFactor)constructs a Perceptron with the default bias learning rate, and activation functionPerceptron(int n, double learningRate, double learningRateFactor, double biasLearningRateFactor)constructs a Perceptron with a default activation functionPerceptron(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()doublegetBias()doublegetLearningRate()doublegetLearningRateFactor()VectorgetWeights()intguess(double[] inputs)takes in an input array and converts it to a vector and sends it to guess(Vector)intguess(Vector inputs)takes a Vector for inputs and then guesses the outputvoidsetActivationFunction(Function<java.lang.Double,java.lang.Integer> activationFunction)voidsetLearningRate(double learningRate)voidsetLearningRateFactor(double learningRateFactor)voidtrain(double[] inputs, int target)converts the input array into a Vector and sends it to the train(Vector, integer) methodvoidtrain(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
-
-
-
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
-
defaultN
public static final int defaultN
default number of inputs- See Also:
- Constant Field Values
-
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 vectorslearningRate- the learning ratelearningRateFactor- the factor for the learning rate to multiply on trainingbiasLearningRateFactor- the factor which will be multiplied with the learning rate when adding to the biasactivationFunction- 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 vectorslearningRate- the learning ratelearningRateFactor- the learning rate factorbiasLearningRateFactor- 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 vectorslearningRate- the learning ratelearningRateFactor- 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 vectorslearningRate- 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 inputstarget- 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 arraytarget- 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
-
-