Home > Software engineering >  Is there a way to save the weights and load them on another file
Is there a way to save the weights and load them on another file

Time:03-15

So, is simple as the title of the question, is there a way to save the weights after training like this

model.save_weights("path")

and then load them on another project only with

model = load_weights("path")
model.predict(x)

is it possible ?

CodePudding user response:

yes. it is possible if you call the right path

for instance, you have this path

- project1/cool.py
- project2/another_cool.py

you train with cool.py and the model is saved inside project1's folder. then you want to load the model in another_cool.py

just call load_model function with path ../project1/weigh.h5

CodePudding user response:

If you only want to save/load the weights, you can use

model.save_weights("path/to/my_model_weights.hdf5")

and then to reload (potentially in another python project / in another interpreter, you just have to update the path accordingly)

other_model.load_weights("path/to/my_model_weights.hdf5")

However both models should have the same architecture (instances of the same class), and Python/tensorflow/keras versions should be the same. See the doc for more info.

You can save both weights and architecture through model.save("path/to/my_model.hdf5")for saving on disk and keras.models.load_model("path/to/my_model.hdf5")for loading from disk (once again, the documentation should provide details).

Once loaded in memory, you can retrain your model, or use predict on it, predictions should be identical between projects

  • Related