Skip to content

aurora: Model Loading

JonathanHallstrom edited this page May 5, 2023 · 2 revisions

Support

The ModelLoader supports loading all models with the Model implementation such as NeuralNetworkModel, LinearRegressionModel, etc.

Load a pre-trained model

In this example, we'll be loading a Neural Network model

Loading from URL

NeuralNetworkModel model = null;
try (ModelLoader loader = new ModelLoader(new URL("https://url.com/models/my_model_name"))) {
  model = loader.load(NeuralNetworkModel.class);
} catch (MalformedURLException e) {
  throw new RuntimeException(e);
}

Loading from file:

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

Loading from JSON:

Example pre-trained model:

{"version":"1.0.0","name":"chicken_turbo","activation":"sigmoid","weights_input_to_hidden":[[0.8292906753444104,64.6745187153743,68.24653809609457],[-1.8774237538879162,-1.404082802370742,3.2189387768087983],[-6.845557248896268,-10.115594809453968,-3.3411195096409294]],"weights_hidden_to_output":[[6.533597663626858],[46.216610736558884],[-42.75439516642889]],"biases_hidden":[1.371459165730676,-5.690448152503832,-16.615627630860153],"biases_output":[-24.269233259453593]}
NeuralNetworkModel model = null;
try (ModelLoader loader = new ModelLoader("my_json")) {
  model = loader.load(NeuralNetworkModel.class);
}

Using a pre-trained Model

The model comes with the same functionality as a Trainable so with the loaded NeuralNetworkModel we can do the following:

double[] input = new double[]{1, 2, 3};
double[] output = model.predict(input);

Improve / Train a pre-trained Model

Get the Trainable builder class, and use from(Model...):

NeuralNetworkTrainer nn = new NeuralNetworkBuilder()
  .from(model) // the model
  // other required parameters, such as learning rate, epochs, layers etc.
  .build();
nn.train(inputs, outputs);