/  Deep Learning Interview questions and answers   /  How do you save Neural Network model in keras once after the training and tuning?
Neural Network model in keras 1 (i2tutorials)

How do you save Neural Network model in keras once after the training and tuning?

Ans: Neural Networks can be saved in keras in two formats which are JSON and YAML formats.

JSON is a simple file format for describing data hierarchically.

Keras provides the ability to describe any model using JSON format with a to_json() function. This can be saved to file and later loaded via the model_from_json() function that will create a new model from the JSON specification.

The weights are saved directly from the model using the save_weights() function and later loaded using the symmetrical load_weights() function.

The example below trains and evaluates a simple model on the Pima Indians dataset. The model is then converted to JSON format and written to model.json in the local directory. The network weights are written to model.h5 in the local directory.

The model and weight data is loaded from the saved files and a new model is created. It is important to compile the loaded model before it is used. This is so that predictions made using the model can use the appropriate efficient computation from the Keras backend.

The model is evaluated in the same way printing the same evaluation score.

Neural Network model in keras 1 (i2tutorials)

The model is described using YAML, saved to file model.yaml and later loaded into a new model via the model_from_yaml() function. Weights are handled in the same way as above in HDF5 format as model.h5.

Neural Network model in keras 2 (i2tutorials)

Leave a comment