# Retrieving code of a saved model

**URL:** <https://dlai.staged-by-discourse.com/t/retrieving-code-of-a-saved-model/148090>\
**Category:** Convolutional Neural Networks\
**Tags:** coursera-platform\
**Created:** [June 26, 2022, 10:18pm UTC](https://dlai.staged-by-discourse.com/t/retrieving-code-of-a-saved-model/148090 "2022-06-26T22:18:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![EduardoChicago](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@EduardoChicago](https://dlai.staged-by-discourse.com/u/EduardoChicago)\
**Post date:** [June 26, 2022, 10:18pm UTC](https://dlai.staged-by-discourse.com/t/retrieving-code-of-a-saved-model/148090/1 "2022-06-26T22:18:15Z")

</div>

This question is being formulated at Week 3 of Course 4, but could equally apply to Week 2 assignments.  
I saved a trained model with tf.keras.models.save\_model(MyM), it produced a folder with the contents expected. When I retrieve it, using tf.keras.models.load\_model(MyM), everything works, except I can’t see the source code. (I am using Jupyter.notebooks). What to do if I want to continue working with the code of a “save\_model” model?

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/tmosh/32/9321_2.png) [@TMosh](https://dlai.staged-by-discourse.com/u/TMosh)\
**Post date:** [June 26, 2022, 10:53pm UTC](https://dlai.staged-by-discourse.com/t/retrieving-code-of-a-saved-model/148090/2 "2022-06-26T22:53:46Z")

</div>

If you want to save your notebook, use the “File → Download As” tool from the menu bar.

---

<div class="post-metadata">

**Author:** ![ai\_curious](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/ai_curious/32/128003_2.png) [@ai\_curious](https://dlai.staged-by-discourse.com/u/ai_curious)\
**Post date:** [June 26, 2022, 11:42pm UTC](https://dlai.staged-by-discourse.com/t/retrieving-code-of-a-saved-model/148090/3 "2022-06-26T23:42:54Z")

</div>

And you shouldn’t expect to have the notebook code saved as part of the model, since per the doc

_The SavedModel and HDF5 file contains:_

_- the model’s configuration (topology)_  
_- the model’s weights_  
_- the model’s optimizer’s state (if any)_

_Thus models can be reinstantiated in the exact same state, without any of the code used for model definition or training_.

> **[Keras documentation: Saving & serialization](https://keras.io/api/models/model_saving_apis/#save_model-function)**
>
> Keras documentation: Saving & serialization

---

<div class="post-metadata">

**Author:** ![EduardoChicago](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@EduardoChicago](https://dlai.staged-by-discourse.com/u/EduardoChicago)\
**Post date:** [June 27, 2022, 1:59pm UTC](https://dlai.staged-by-discourse.com/t/retrieving-code-of-a-saved-model/148090/4 "2022-06-27T13:59:02Z")

</div>

Thanks ai\_curious. Yes, I learnt the hard way that saving the model with save\_model does not retain the code.

---

<div class="post-metadata">

**Author:** ![ai\_curious](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/ai_curious/32/128003_2.png) [@ai\_curious](https://dlai.staged-by-discourse.com/u/ai_curious)\
**Post date:** [June 27, 2022, 6:34pm UTC](https://dlai.staged-by-discourse.com/t/retrieving-code-of-a-saved-model/148090/5 "2022-06-27T18:34:40Z")

</div>

There is nothing that says you can’t write your own Python Class that incorporates Keras Model functionality, either by composition (has-a) or inheritance (is-a), and implementing a custom save model to capture meta information about how the model was configured and trained.

---

<div class="post-metadata">

**Author:** ![EduardoChicago](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@EduardoChicago](https://dlai.staged-by-discourse.com/u/EduardoChicago)\
**Post date:** [June 27, 2022, 8:10pm UTC](https://dlai.staged-by-discourse.com/t/retrieving-code-of-a-saved-model/148090/6 "2022-06-27T20:10:02Z")

</div>

Thanks Curious. But your answer is way above my “pay level”…☹
