rasbt/python-machine-learning-book-2nd-edition

Why not use tf.train.import_meta_graph() instead of callin build_cnn() in Chaper 15 page 527

LasaniHussain opened this issue · 1 comments

Why cannot we rebuild the model by calling tf.train.import_meta_graph() rather than building the whole graph again with build_cnn, as you illustrated in Ch 14, Page 477 Saving and restoring a model in tensorflow. Also , will I be able to give just my saved model to someone without all the fc,conv2d and build_cnn functions code, and would that person be able to just restore the model and do prediction ?
I would be really greatful if you could help with a clear explanation.

rasbt commented

Hi, there,

Why cannot we rebuild the model by calling tf.train.import_meta_graph() rather than building the whole graph again with build_cnn, as you illustrated in Ch 14, Page 477

I think you are referring to the following text in the book?

Restoring a trained model requires two steps:

  1. Rebuild the graph that has the same nodes and names as the saved model.
  2. Restore the saved variables in a new tf.Session environment.

For the first step, we can run the statements, as we did in the first place, to build the graph g. But there is a much easier way to do this. Note that all of the information regarding the graph is saved as metadata in the file with the .meta extension. Using the following code, we rebuild the graph by importing it from the meta file: ...

Sorry if this was unclear! The steps 1 & 2 are highlighting the general procedure, and

"For the first step, we can run the statements, as we did in the first place, to build the graph g"

is just a particular suggestion how to do it. The "easier" way is NOT to do this so but just use the meta-file instead:

But there is a much easier way to do this. Note that all of the information regarding the graph is saved as metadata in the file with the .meta extension.

I hope this is more clear know, and otherwise, please let me know!


Also , will I be able to give just my saved model to someone without all the fc,conv2d and build_cnn functions code

Yes, you would be definitely able to do that! The following page (page 478) shows an example:

>>> import tensorflow as tf
>>> import numpy as np
>>>
>>> g2 = tf.Graph()
>>> with tf.Session(graph=g2) as sess:
... new_saver = tf.train.import_meta_graph('./trained-model.meta')
... new_saver.restore(sess, './trained-model')
... y_pred = sess.run('y_hat:0',
                      feed_dict={'tf_x:0': x_test})