Package libai.nn

Class NeuralNetwork

  • All Implemented Interfaces:
    java.io.Serializable
    Direct Known Subclasses:
    SupervisedLearning, UnsupervisedLearning

    public abstract class NeuralNetwork
    extends java.lang.Object
    implements java.io.Serializable
    Neural network abstraction. Provides the methods to train, simulate and calculate the error.
    See Also:
    Serialized Form
    • Field Detail

      • random

        protected final java.util.Random random
      • plotter

        protected transient Plotter plotter
    • Constructor Detail

      • NeuralNetwork

        public NeuralNetwork()
      • NeuralNetwork

        public NeuralNetwork​(java.util.Random rand)
    • Method Detail

      • getDefaultRandomGenerator

        public static final java.util.Random getDefaultRandomGenerator()
      • open

        public static final <NN extends NeuralNetwork> NN open​(java.lang.String path)
                                                        throws java.io.IOException,
                                                               java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • open

        public static final <NN extends NeuralNetwork> NN open​(java.io.File file)
                                                        throws java.io.IOException,
                                                               java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • open

        public static final <NN extends NeuralNetwork> NN open​(java.io.InputStream input)
                                                        throws java.io.IOException,
                                                               java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • euclideanDistance2

        public static double euclideanDistance2​(double[] a,
                                                double[] b)
        Calculates the square Euclidean distance between two vectors.

        NOTE: Assertions of the dimensions are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        a - Vector a.
        b - Vector b.
        Returns:
        The square Euclidean distance.
      • euclideanDistance2

        public static double euclideanDistance2​(Column a,
                                                Column b)
        Calculates the square Euclidean distance between two column matrix.

        NOTE: Assertions of the dimensions are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        a - Column matrix a.
        b - Column matrix b.
        Returns:
        The square Euclidean distance.
      • gaussian

        public static double gaussian​(double u2,
                                      double sigma)
        Calculates the Gaussian function with standard deviation sigma and input parameter u^2
        Parameters:
        u2 - u2
        sigma - sigma
        Returns:
        e^(-u^2/2.sigma)
      • getPlotter

        public Plotter getPlotter()
      • setPlotter

        public void setPlotter​(Plotter plotter)
      • train

        public abstract void train​(Column[] patterns,
                                   Column[] answers,
                                   double alpha,
                                   int epochs,
                                   int offset,
                                   int length,
                                   double minerror)
        Trains this neural network with the list of patterns and the expected answers.

        Use the learning rate alpha for many epochs. Take length patterns from the position offset until the minerror is reached.

        patterns and answers must be arrays of non-null column matrices

        Parameters:
        patterns - The patterns to be learned.
        answers - The expected answers.
        alpha - The learning rate.
        epochs - The maximum number of iterations
        offset - The first pattern position
        length - How many patterns will be used.
        minerror - The minimal error expected.
      • train

        public void train​(Column[] patterns,
                          Column[] answers,
                          double alpha,
                          int epochs)
        Alias of train(patterns, answers, alpha, epochs, 0, patterns.length, 1.e-5).

        patterns and answers must be arrays of non-null column matrices

        Parameters:
        patterns - The patterns to be learned.
        answers - The expected answers.
        alpha - The learning rate.
        epochs - The maximum number of iterations
        See Also:
        train(Column[], Column[], double, int, int, int, double)
      • train

        public void train​(Column[] patterns,
                          Column[] answers,
                          double alpha,
                          int epochs,
                          int offset,
                          int length)
        Alias of train(patterns, answers, alpha, epochs, offset, length, 1.e-5).

        patterns and answers must be arrays of non-null column matrices

        Parameters:
        patterns - The patterns to be learned.
        answers - The expected answers.
        alpha - The learning rate.
        epochs - The maximum number of iterations
        offset - The first pattern position
        length - How many patterns will be used.
        See Also:
        train(Column[], Column[], double, int, int, int, double)
      • simulate

        public abstract Column simulate​(Column pattern)
        Calculates the output for the pattern.
        Parameters:
        pattern - Pattern to use as input.
        Returns:
        The output for the neural network.
      • simulate

        public abstract void simulate​(Column pattern,
                                      Column result)
        Calculates the output for the pattern and left the result in result.
        Parameters:
        pattern - Pattern to use as input.
        result - The output for the input.
      • save

        public boolean save​(java.lang.String path)
        Saves the neural network to the file in the given path
        Parameters:
        path - The path for the output file.
        Returns:
        true if the file can be created and written, false otherwise.
      • error

        public double error​(Column[] patterns,
                            Column[] answers)
        Calculates from a set of patterns. Alias of error(patterns, answers, 0, patterns.length)

        patterns and answers must be arrays of non-null column matrices

        Parameters:
        patterns - The array with the patterns to test
        answers - The array with the expected answers for the patterns.
        Returns:
        The error calculate for the patterns.
        See Also:
        error(Column[], Column[], int, int)
      • error

        public double error​(Column[] patterns,
                            Column[] answers,
                            int offset,
                            int length)
        Calculates the mean quadratic error. It is the standard error metric for neural networks. Just a few networks needs a different type of error metric.

        patterns and answers must be arrays of non-null column matrices

        NOTE: Assertions of the dimensions are made with assert statement. You must enable this on runtime to be effective.

        Parameters:
        patterns - The array with the patterns to test
        answers - The array with the expected answers for the patterns.
        offset - The initial position inside the array.
        length - How many patterns must be taken from the offset.
        Returns:
        The mean quadratic error.
      • initializeProgressBar

        protected void initializeProgressBar​(int maximum)