Skip to content

aurora: Neural Network

Clouke edited this page May 3, 2023 · 4 revisions

Build your trainable Neural Network

NeuralNetworkTrainer nn = new NeuralNetworkBuilder()
  .name("chicken-turbo") // name your model
  .learningRate(0.1) // define the learning rate
  .epochs(1_000_000) // define the amount of epochs to train the model for
  .optimizer(new StochasticGradientDescent()) // define the optimizer, SGD is selected by default
  .earlyStops(new Stagnation(20)) // enable early stopping if the model stagnates for 20 steps (got stuck / no longer improving)
  .printing(Bar.CLASSIC) // this is optional, but it will print a progress bar to the console
  .activationFunction(ActivationFunction.SIGMOID)
  .epochActions(new EpochAutoSave(10_000, "C:\\Users\\my_user\\models")) // save the model every 10K epochs
  .layers(mapper -> mapper
    .inputLayers(2) // must match the input size
    .hiddenLayers(2)
    .outputLayers(1)) // must match the output size
  .build();

Train your Neural Network

Create a Data Set:

double[][] inputs = { // 2 inputs
  {0.0, 0.0},
  {0.0, 1.0},
  {1.0, 0.0},
  {1.0, 1.0}
};

double[][] outputs = { // 1 output
  {0.0},
  {1.0},
  {1.0},
  {0.0}
};

Train the network:

nn.train(inputs, outputs);

Train asynchronously:

new Thread(() -> nn.train(inputs, outputs)).start();
nn.onCompletion(network -> System.out.println("Finished training!")); // Support for threading - consumer is accepted when training is finished

Printing: Printing comes with attributes, providing information about the training process:

  • Loss: Represents the decreasing error which means the model is improving
  • Stage: Represents the current stage in the training process, which goes to 100 when it is completed
  • Accuracy: Represents the accuracy score of the model, whereas you may use HyperparameterTuning for the best score
  • Time left: Approximates remaining training time
  • Epoch: Represents the current iteration
[###########====================] Loss: 5.3310587149957474E-20 | Stage: 34 | Accuracy: 99.97835414449399 | Time left: 10.4s | Epoch: 3400000 (|)

Predict

double[] output = nn.predict(new double[]{0.1, 0.9}); // output should be close to 0.9 since we trained the output to be 1

Evaluate your model

Create a Test Set:

Map<double[], double[]> data = new HashMap<>();
data.put(new double[]{0.0, 0.0}, new double[]{0.0});
data.put(new double[]{0.0, 1.0}, new double[]{1.0});
data.put(new double[]{1.0, 0.0}, new double[]{1.0});
data.put(new double[]{1.0, 1.0}, new double[]{0.0});
TestSet testSet = new TestSet(data);

Evaluate:

Evaluation eval = nn.evaluate(testSet);
eval.printSummary();

Prints:

Evaluation Summary of Type: Neural Network
 Accuracy: 1.0
 Precision: 1.0
 Recall: 1.0
 F1 Score: 1.0

Deploy your model

Save your Neural Network Model:

Model model = nn.toModel();
model.save("my_directory");

Load a pre-trained model

Load from file:

NeuralNetworkModel model = null;
try (ModelLoader loader = new ModelLoader(new File("my_directory"))) {
  model = loader.load(NeuralNetworkModel.class);
}

Load from URL:

NeuralNetworkModel model = null;
try (ModelLoader loader = new ModelLoader(new URL("my_model_url"))) {
  model = loader.load(NeuralNetworkModel.class);
} catch (MalformedURLException e) {
  throw new RuntimeException(e);
}