Package com.jml.neural_network
Class NeuralNetwork
java.lang.Object
com.jml.core.Model<double[][],double[][]>
com.jml.neural_network.NeuralNetwork
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
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:Custom layers or activation functions can be created using the ActivationFunction, BaseLayer, and TrainableLayer interfaces.Linear
Sigmoid
ReLU
Tanh
Softmax
-
Constructor Summary
ConstructorDescriptionConstructs 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 TypeMethodDescriptionvoid
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 thefit(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.inspect()
Forms a string of the important aspects of the model which are needed to recreate the model.
same astoString()
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
Saves a trained model to the specified file path.toString()
Forms a string of the important aspects of the model.
-
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 theVanila 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 theVanila 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 theVanila 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
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
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
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
Fits or trains the model with the given features and targets.- Specified by:
fit
in classModel<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 inargs
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 classModel<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. -
getParams
public linalg.Matrix getParams()Gets the parameters of the trained model. -
add
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 thefit(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 thefit(double[][], double[][])
method.- Parameters:
layer
- Layer to add to the neural network.
-
getLossHist
-
saveModel
Saves a trained model to the specified file path. -
inspect
Forms a string of the important aspects of the model which are needed to recreate the model.
same astoString()
-
toString
Forms a string of the important aspects of the model.
-