As a prerequisite for API creation, you need to run the main example source code:
https://github.com/PacktPublishing/Java-Deep-Learning-Cookbook/blob/master/03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/cookbookapp/src/main/java/com/javadeeplearningcookbook/examples/CustomerRetentionPredictionExample.java
DL4J has a utility class called ModelSerializer to save and restore models. We have used ModelSerializer to persist the model to disk, as follows:
File file = new File("model.zip");
ModelSerializer.writeModel(multiLayerNetwork,file,true);
ModelSerializer.addNormalizerToModel(file,dataNormalization);
For more information, refer to:
Also, note that we need to persist the normalizer preprocessor along with the model. Then we can reuse the same to normalize user inputs on the go. In the previously mentioned code, we persisted the normalizer by calling addNormalizerToModel() from ModelSerializer.
You also need to be aware of the following input attributes to the addNormalizerToModel() method:
- multiLayerNetwork: The model that the neural network was trained on
- dataNormalization: The normalizer that we used for our training
Please refer to the following example for a concrete API implementation:
https://github.com/PacktPublishing/Java-Deep-Learning-Cookbook/blob/master/03_Building_Deep_Neural_Networks_for_Binary_classification/sourceCode/cookbookapp/src/main/java/com/javadeeplearningcookbook/api/CustomerRetentionPredictionApi.java
In our API example, we restore the model file (model that was persisted before) to generate predictions.