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.

[Question] How to properly visualize the model structure as well as save model?

See original GitHub issue

I tried tf.keras.utils.plot_model

But it seems not to work… only print the model name, is it because we did not use Functional API here?

Also for saving the model, model.save() works but later the weights and summary can not be called even if I do loaded_model(inputs) first.

I am also a little bit confused of Model vs. Layer class. I see all tutorials here use Model class for both query and candidate tower, but -> https://www.tensorflow.org/guide/keras/custom_layers_and_models#the_model_class . It says if you do not need to call fit (which is the case for query and candidate tower), consider use layer class

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
maciejkulacommented, Jun 3, 2022

There are two ways of saving model state in TensorFlow:

  1. Using checkpoints for saving model variables during training.
  2. Using tf.saved_model.save/load to store and load models for serving.

Here are the Keras docs for checkpointing (1). For (2), you can call tf.saved_model.save on your trained Model instance.

0reactions
jasonzyxcommented, Aug 17, 2022

Below is how I save & load the model/index

scann = tfrs.layers.factorized_top_k.ScaNN(
    model.query_model, 
    num_reordering_candidates=100
)

scann.index_from_dataset(
    products.batch(128).map(
        lambda x: (x['product_token'] + ': ' + x['product_name'], # idx identifier
                   model.product_model({                          # transform embeddings 
                       "product_name": x['product_name'],
                       "product_description": x["product_description"]
                   }))
    )
)

path = './your_model_name'
tf.saved_model.save(
    scann,
    path,
    options=tf.saved_model.SaveOptions(namespace_whitelist=["Scann"])
)

loaded = tf.saved_model.load(path)

_, titles = loaded({
    "query_text": np.array(["hello world"])
})
for title in titles[0]:
    print(title)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Visualize a Deep Learning Neural Network Model in ...
We can start off by defining a simple multilayer Perceptron model in Keras that we can use as the subject for summarization and...
Read more >
Using the SavedModel format | TensorFlow Core
You can save and load a model in the SavedModel format using the following APIs: Low-level tf.saved_model API. This document describes how to...
Read more >
Visualizing Machine Learning Models: Guide and Tools
The first and main thing you can visualize is the model architecture. It tells us how many layers there are, in what order...
Read more >
Data Visualization using Python for Machine Learning and ...
From perspective of building models , By visualizing the data we can find ... of Data Science are Python & R. So your...
Read more >
Keras - Save and Load Your Deep Learning Models
Loading a model with Keras and TensorFlow · Provided the path to our testing images ( --images malaria/testing ) as well as the...
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