question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Efficient Ways for Saving and Loading weights

See original GitHub issue

I’m sorry if it’s not the right place as I could not find the discussions or forum page.

I was wondering what are some of the most efficient ways to save and load models (also verify it’s properly loaded into GPU)?

1, In the docs, its given as save_the_model using Tensorflow 2. I also understand that the weights of haiku network are stored in a dictionary, as an example

{'linear': {'b': ndarray(..., shape=(300,), dtype=float32),
            'w': ndarray(..., shape=(28, 300), dtype=float32)},
 'linear_1': {'b': ndarray(..., shape=(100,), dtype=float32),
              'w': ndarray(..., shape=(1000, 100), dtype=float32)}}

Does haiku have some inbuilt function to save and load models? It becomes crucial in transfer learning tasks. Thanks in advance,

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
tomhennigancommented, Jun 3, 2021

Hi @VIGNESHinZONE , in general we recommend keeping a copy of your Python code which defines your model, and saving weights after training to disk. This provides the most flexibility. You can save weights produced by Haiku using pickle (https://colab.research.google.com/gist/tomhennigan/77a7e4ea04d716e9650bc54acf26f468/saving-and-loading-parameters-using-pickle.ipynb).

The integration with TensorFlow is an example of how to save the implementation of your model (e.g. the operations required to execute your neural network) as a TensorFlow graph, which is a suitable long term serialisation format for ML models. In general the best way to save the implementation of your model is to keep a copy of your Python code. In some situations (e.g. production serving) it is useful to have an intermediate format (like TF graphs) instead.

0reactions
tomhennigancommented, Jun 6, 2021

Haiku itself doesn’t do anything special to support pmap/vmap. The key idea with Haiku is that once you’ve hk.transformed your function it is a “pure function” and you can use this directly with jax.pmap etc.

For an example of data parallel training with pmap see our resnet/imagenet example https://github.com/deepmind/dm-haiku/blob/master/examples/imagenet/train.py. In there the train_step function is parallel mapped across all available GPUs. You might observe that in this code we use Haiku to define a forward function with our neural net, and the rest is pure JAX with some other libraries in our ecosystem (e.g. optax for optimization).

As a meta point, its probably worth opening new issues for different questions to help other users who are searching.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Save and load models | TensorFlow Core
Manually save weights ... To save weights manually, use tf.keras.Model.save_weights . By default, tf.keras —and the Model.save_weights method in ...
Read more >
How to Save and Load Your Keras Deep Learning Model
The model and weight data is loaded from the saved files, and a new model is created. It is important to compile the...
Read more >
Everything You Need To Know About Saving Weights In ...
We either save the learnt weights or the entire model so that we could ... will now learn 2 of the widely known...
Read more >
Save and load models in Tensorflow - GeeksforGeeks
Now you can simply save the weights of all the layers using the save_weights() method. It saves the weights of the layers contained...
Read more >
Saving and loading models in TensorFlow - KDnuggets
Saving your models to maximize reusability is key for efficient ... Now to save the weights only using the simple way, you just...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found