import tensorflow as tf
from tensorflow.contrib import graph_editor as ge
g = tf.Graph()
with g.as_default():
a = tf.constant(1.0, shape=[2, 3], name="a")
b = tf.constant(2.0, shape=[2, 3], name="b")
a_pl = tf.placeholder(dtype=tf.float32)
b_pl = tf.placeholder(dtype=tf.float32)
c = tf.add(a_pl, b_pl, name='c')
print g.get_operations()
with tf.Session(graph=g) as sess:
#print sess.run(c) # this prints out a lot of error, since input to c (a_pl and b_pl) are not fed any values
print sess.run(c, feed_dict={a_pl:[[1, 1, 1], [1, 1, 1]], b_pl:[[2, 2, 2], [2, 2, 2]]}) # we could feed values...
# after changing the inputs of the c to a and b --> disconnects a_pl and b_pl from c
ge.swap_inputs(c.op, [a, b])
print g.get_operations()
with tf.Session(graph=g) as sess:
print sess.run(c)
# after replacing the inpus of the c to a and b
c_1 = ge.graph_replace(c, {a: a_pl, b: b_pl}) # this appends a new node called c_1 to graph with inputs a_pl and b_pl
print g.get_operations()
with tf.Session(graph=g) as sess:
print sess.run(c)
#print sess.run(c_1) # this prints out a lot of error, since input to c_1 (a_pl and b_pl) are not fed any values
print sess.run(c_1, feed_dict={a_pl:[[1, 1, 1], [1, 1, 1]], b_pl:[[2, 2, 2], [2, 2, 2]]}) # we could feed values...
Reference
https://gist.github.com/ByungSunBae/393071e46409737cd341360a69957906
https://www.tensorflow.org/api_guides/python/contrib.graph_editor