Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save and load my neural network model after training along with weights in python?

I have trained a single layer neural network model in python (a simple model without keras and tensorflow). How canI save it after training along with weights in python, and how to load it later?

like image 779
S.hassan Avatar asked Mar 11 '19 13:03

S.hassan


People also ask

How do I save a trained neural network model in python?

Save Your Neural Network Model to JSONThis can be saved to a 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.

How do you save and load machine learning model in python?

Save Your Model with pickle Pickle is the standard way of serializing objects in Python. You can use the pickle operation to serialize your machine learning algorithms and save the serialized format to a file. Later you can load this file to deserialize your model and use it to make new predictions.

How do you save a model after training in Keras?

There are two formats you can use to save an entire model to disk: the TensorFlow SavedModel format, and the older Keras H5 format. The recommended format is SavedModel. It is the default when you use model. save() .


2 Answers

All the trainable parameters like weights and biases could be treated as either Python lists or NumPy arrays ( which is mostly preferred ).

For Python lists :

If your trainable parameters are Python lists then you can use pickle. You can pickle them like this :

import pickle
# weights is a Python array
pickle.dump( weights , open( 'weights.pkl' , 'wb' ) )

You can group together several objects in a set or list and pickle that so you have a single file. For reading it,

weights = pickle.load( open( 'weights.pkl' , 'rb' ))

For NumPy arrays :

That makes all the code easy. A NumPy array could be saved by using np.array.save() method.

np.save( 'weights.npy' , weights )

And load it,

weights = np.load( 'weights.npy' )

Apart from these prevalent methods like writing the weights and biases to a text file or a csv file may also work. Also, a JSON file may help.

like image 52
Shubham Panchal Avatar answered Sep 29 '22 12:09

Shubham Panchal


So you write it down yourself. You need some simple steps:

  1. In your code for neural network, store weights in a variable. It could be simply done by using self.weights.weights are numpy ndarrays. for example if weights are between layer with 10 neurons to layer with 100 neurons, it is a 10 * 100(or 100* 10) nd array.
  2. Use numpy.save to save the ndarray.
  3. For next use of your network, use numpy.load to load weights
  4. In the first initialization of your network, use weights you've loaded. Don't forget, if your network is trained, weights should be frozen. It can be done by zeroing learning rate.
like image 28
mohammad hassan bigdeli shamlo Avatar answered Sep 29 '22 10:09

mohammad hassan bigdeli shamlo