{{ message }}

Instantly share code, notes, and snippets.

# ByungSunBae/EditGraph.py

Created Dec 5, 2017
simple tensorflow graph edit example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 # from : https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/graph_editor/examples/edit_graph_example.py import numpy as np import tensorflow as tf from tensorflow.contrib import graph_editor as ge # create a graph 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") # Once print operations, g.get_operations() # Out: #[, #, #, #, #] # modify the graph (input graph) ge.swap_inputs(c.op, [a, b]) # Out: #(, #) # and print g.get_operations() g.get_operations() # Out: #[, #, #, #, #] # Same thing! # print the graph def print(g.as_graph_def()) # and print the value of c with tf.Session(graph=g) as sess: res = sess.run(c) print(res) # But graph_replace is different from swap_inputs. # One more create a graph 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") c_ = ge.graph_replace(c, {a_pl: a, b_pl: b}) # and print g.get_operations() g.get_operations() #Out: #[, #, #, #, #, #] # We can see 'c_1' op that is added. # and print the value of c with tf.Session(graph=g) as sess: res_ = sess.run(c_) print(res_) # same result of res

❤️

### LiuHao-THU commented Oct 6, 2018

nice!!! thanks!!!

### helinwang commented May 20, 2019 • edited

By `# Same thing!` on line 41 did you mean `ge.swap_inputs(c.op, [a, b])` did not do anything?
If you check the graphDef, it actually changes

``````node {
name: "c"
input: "Placeholder"
input: "Placeholder_1"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
}
``````

to

``````node {
name: "c"
input: "a"
input: "b"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
}
``````

Edit: oh, I guess you wanted to highlight the difference between `ge.swap_inputs` and `ge.graph_replace`. Thanks!