How To Save A Tensorflow Graph Without Initialise A Variable?
The problem I encountered can be reflected as follow: tf.reset_default_graph() x = tf.placeholder(dtype=tf.int32, shape=()) init = tf.zeros(shape=tf.squeeze(x), dtype=tf.float32)
Solution 1:
You could save your graph as meta graph object without initializing the variables like this:
import tensorflow as tf
import json
x = tf.placeholder(dtype=tf.int32, shape=(), name='x')
init = tf.zeros(shape=tf.squeeze(x), dtype=tf.float32, name='init')
v = tf.get_variable('foo', initializer=init, validate_shape=False)
tensor_names = {
'x': x.name,
'v': v.name
}
withopen('tensor_names.json', 'w') as fo:
json.dump(tensor_names, fo)
fname = 'graph.meta'
proto = tf.train.export_meta_graph(filename=fname,
graph=tf.get_default_graph())
And later restore this graph:
import tensorflow as tf
import json
withopen('tensor_names.json', 'r') as fo:
tensor_names = json.load(fo)
graph = tf.Graph()
with graph.as_default():
tf.train.import_meta_graph(fname)
x = graph.get_tensor_by_name(tensor_names['x'])
v = graph.get_tensor_by_name(tensor_names['v'])
# works as expected: with tf.Session(graph=graph) as sess:
sess.run(tf.global_variables_initializer(), {x:5})
print(v.eval()) # [0. 0. 0. 0. 0.]
Post a Comment for "How To Save A Tensorflow Graph Without Initialise A Variable?"