import tensorflow as tf
import numpy as np
Remember
1) Tensors that should be retrieved must be added to collection
2) Varibales are automatically added
tf.reset_default_graph()
w = tf.Variable(tf.random_normal((5,5),seed=101),name="w")
b = tf.placeholder(tf.float32,(5,),name="b")
x = tf.matmul(w,tf.expand_dims(b,axis=1),name="x")
tf.add_to_collection('output',x)
saver = tf.train.Saver()
!rm models/*
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
W,o = sess.run([w,x],{b:np.ones((5,))})
print(W,o)
saver.save(sess,'models/my_first_model.ckpt')
with tf.Session() as sess:
saver.restore(sess,'models/my_first_model.ckpt')
W,o = sess.run([w,x],{b:np.ones((5,))})
print(W,o)
import tensorflow as tf
import numpy as np
Load the graph
tf.reset_default_graph()
saver = tf.train.import_meta_graph('models/my_first_model.ckpt.meta')
sess = tf.Session()
saver.restore(sess,tf.train.latest_checkpoint('models/'))
Check if the data is available
sess.graph.get_all_collection_keys()
sess.graph.get_collection('output')
Run
W,o = sess.run(['w:0','x:0'],{'b:0':np.ones((5,))})
print(W,o)
# import the inspect_checkpoint library
from tensorflow.python.tools import inspect_checkpoint as chkp
chkp.print_tensors_in_checkpoint_file("models/my_first_model.ckpt",tensor_name='', all_tensors=True,all_tensor_names=True)