Class NeuralNetwork

java.lang.Object
com.jml.core.Model<double[][],​double[][]>
com.jml.neural_network.NeuralNetwork

public class NeuralNetwork extends Model<double[][],​double[][]>
A class that supports the creation and training of neural networks. A neural network is a supervised learning model that is structured in layers.
Neural networks are created sequentially one layer at a time by using the add(BaseLayer) method. Activation functions can be specified for applicable layers.
 Built-in Layers:
      Dense
      Linear
      Dropout
 Built-in Activation Functions:
      Linear
      Sigmoid
      ReLU
      Tanh
      Softmax
Custom layers or activation functions can be created using the ActivationFunction, BaseLayer, and TrainableLayer interfaces.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a neural network with default hyper-parameters.
    NeuralNetwork​(double learningRate, int epochs)
    Constructs a neural network with the specified hyper-parameters.
    NeuralNetwork​(double learningRate, int epochs, int batchSize)
    Constructs a neural network with the specified hyper-parameters.
    NeuralNetwork​(double learningRate, int epochs, int batchSize, double threshold)
    Constructs a neural network with the specified hyper-parameters.
    NeuralNetwork​(Optimizer optim, int epochs)
    Creates a neural network with the specified hyper-parameters and optimizer.
    NeuralNetwork​(Optimizer optim, int epochs, int batchSize)
    Creates a neural network with the specified hyper-parameters and optimizer.
    NeuralNetwork​(Optimizer optim, int epochs, int batchSize, double threshold)
    Creates a neural network with the specified hyper-parameters and optimizer.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add​(BaseLayer layer)
    Adds specified layer to the network.

    The first layer must have a specified input dimension that matches the number of columns of a feature array that will be used in the fit(double[][], double[][]) method.
    fit​(double[][] features, double[][] targets)
    Fits or trains the model with the given features and targets.
     
    linalg.Matrix
    Gets the parameters of the trained model.
    Forms a string of the important aspects of the model which are needed to recreate the model.
    same as toString()
    double[][]
    predict​(double[][] features)
    Uses fitted/trained model to make predictions on features.
    linalg.Matrix
    predict​(linalg.Matrix X, linalg.Matrix w)
    Makes a prediction using a model by specifying the parameters of the model.
    void
    saveModel​(String filePath)
    Saves a trained model to the specified file path.
    Forms a string of the important aspects of the model.

    Methods inherited from class com.jml.core.Model

    load

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • NeuralNetwork

      public NeuralNetwork()
      Constructs a neural network with default hyper-parameters.
           Learning Rate: 0.01
           epochs: 10
           batchSize: 1
           threshold: 1E-5
           optimizer: Vanila Gradient Descent
       
    • NeuralNetwork

      public NeuralNetwork(double learningRate, int epochs)
      Constructs a neural network with the specified hyper-parameters. Uses a default batchSize of 1 and the Vanila Gradient Descent optimizer.
      Parameters:
      learningRate - Learning to be used during optimization.
      epochs - Number of epochs to train the network for.
    • NeuralNetwork

      public NeuralNetwork(double learningRate, int epochs, int batchSize)
      Constructs a neural network with the specified hyper-parameters. Uses the Vanila Gradient Descent optimizer as the default optimizer.
      Parameters:
      learningRate - Learning to be used during optimization.
      epochs - Number of epochs to train the network for.
      batchSize - The batch size to use during training.
    • NeuralNetwork

      public NeuralNetwork(double learningRate, int epochs, int batchSize, double threshold)
      Constructs a neural network with the specified hyper-parameters. Uses the Vanila Gradient Descent optimizer as the default optimizer.
      Parameters:
      learningRate - The learning rate to be using during optimization.
      epochs - The number of epochs to train the network for.
      batchSize - The batch size to use during training.
      threshold - The threshold for the loss to stop early. If the loss drops below this threshold before the specified number of epochs has been reached, the training will stop early.
    • NeuralNetwork

      public NeuralNetwork(Optimizer optim, int epochs)
      Creates a neural network with the specified hyper-parameters and optimizer. Note that when an optimizer is specified, the learning rate will be specified upon creation of the optimizer and is not needed as a parameter. A default batchSize of 1 will be used.
      Parameters:
      optim - The optimizer to use during training.
      epochs - The number of epochs to train the neural network for.
    • NeuralNetwork

      public NeuralNetwork(Optimizer optim, int epochs, int batchSize)
      Creates a neural network with the specified hyper-parameters and optimizer. Note that when an optimizer is specified, the learning rate will be specified upon creation of the optimizer and is not needed as a parameter.
      Parameters:
      optim - The optimizer to use during training.
      epochs - The number of epochs to train the neural network for.
      batchSize - The batch size to use during training.
    • NeuralNetwork

      public NeuralNetwork(Optimizer optim, int epochs, int batchSize, double threshold)
      Creates a neural network with the specified hyper-parameters and optimizer. Note that when an optimizer is specified, the learning rate will be specified upon creation of the optimizer and is not needed as a parameter.
      Parameters:
      optim - The optimizer to use during training.
      epochs - The number of epochs to train the neural network for.
      batchSize - The batch size to use during training.
      threshold - The threshold for the loss to stop early. If the loss drops below this threshold before the specified number of epochs has been reached, the training will stop early.
  • Method Details

    • fit

      public NeuralNetwork fit(double[][] features, double[][] targets)
      Fits or trains the model with the given features and targets.
      Specified by:
      fit in class Model<double[][],​double[][]>
      Parameters:
      features - The features of the training set.
      targets - The targets of the training set.
      Returns:
      Returns details of the fitting / training process.
      Throws:
      IllegalArgumentException - Can be thrown for the following reasons
      - If key, value pairs in args are unspecified or invalid arguments.
      - If the features and targets are not correctly sized per the specification when the model was compiled.
    • predict

      public double[][] predict(double[][] features)
      Uses fitted/trained model to make predictions on features.
      Specified by:
      predict in class Model<double[][],​double[][]>
      Parameters:
      features - The features to make predictions on.
      Returns:
      The models predicted labels.
      Throws:
      IllegalArgumentException - Thrown if the features are not correctly sized per the specification when the model was compiled.
    • predict

      public linalg.Matrix predict(linalg.Matrix X, linalg.Matrix w)
      Makes a prediction using a model by specifying the parameters of the model. Unlike the other predict method, no model needs to be trained to use this method since the parameters provided define a model.
      Specified by:
      predict in class Model<double[][],​double[][]>
      Parameters:
      X - Features to make prediction on.
      w - Parameters of the model.
      Returns:
      prediction on the features using the given model parameters.
    • getParams

      public linalg.Matrix getParams()
      Gets the parameters of the trained model.
      Specified by:
      getParams in class Model<double[][],​double[][]>
      Returns:
      A matrix containing the parameters of the trained model.
    • add

      public void add(BaseLayer layer)
      Adds specified layer to the network.

      The first layer must have a specified input dimension that matches the number of columns of a feature array that will be used in the fit(double[][], double[][]) method.
      Subsequent layers may have no input dimension defined. In this case the input dimension will be inferred from the output dimension of the previous layer.

      The final layers output dimension must match the number of columns of the target array that is used in the fit(double[][], double[][]) method.
      Parameters:
      layer - Layer to add to the neural network.
    • getLossHist

      public List<Double> getLossHist()
    • saveModel

      public void saveModel(String filePath)
      Saves a trained model to the specified file path.
      Specified by:
      saveModel in class Model<double[][],​double[][]>
      Parameters:
      filePath - File path, including extension, to save fitted / trained model to.
    • inspect

      public String inspect()
      Forms a string of the important aspects of the model which are needed to recreate the model.
      same as toString()
      Specified by:
      inspect in class Model<double[][],​double[][]>
      Returns:
      Details of model as string.
    • toString

      public String toString()
      Forms a string of the important aspects of the model.
      Specified by:
      toString in class Model<double[][],​double[][]>
      Returns:
      String representation of model.