def my_transform_op_handler(info, op, new_inputs): add_noise = op.name.startswith("Add") op_, op_outputs_ = gde.transform.copy_op_handler( info, op, new_inputs) if not add_noise: return op_, op_outputs_ # add some noise to op # Old code: # with info.graph_.as_default(): # t_ = math_ops.add( # constant_op.constant(1.0, shape=[10], name="Noise"), # op_.outputs[0], # name="AddNoise") noise_op = gde.make_const(info.graph_, "Noise", np.full([10], 1., dtype=np.float32), uniquify_name=True) add_noise_op = info.graph_.add_node("AddNoise", "Add", uniquify_name=True) add_noise_op.add_attr("T", tf.float32) add_noise_op.set_inputs([noise_op.outputs[0], op_.outputs[0]]) add_noise_op.infer_outputs() t_ = add_noise_op.outputs[0] # return the "noisy" op return op_, [t_]
def test_copy_with_input_replacements(self): # Original code: # with self.graph.as_default(): # _ = tf.constant(10.0, shape=[10], name="Input") # New code adds node as a NodeDef ten_node = gde.make_const(self.graph, "Ten", np.full([10], 10., dtype=np.float32)) ten_tensor = ten_node.output(0) sgv, _ = gde.copy_with_input_replacements( # self.o is an identity on top of a tree of add ops [self.o, self.o.inputs[0].node], # Drill down to second input to outer add() {self.o.inputs[0].node.inputs[1]: ten_tensor} ) after_graph = tf.Graph() with after_graph.as_default(): tf.import_graph_def(self.graph.to_graph_def(), name="") with tf.Session() as sess: val = sess.run(sgv.outputs[0].name) print("val is {}".format(val)) self.assertNear( np.linalg.norm(val - np.array([11])), 0.0, ERROR_TOLERANCE)
def main(_): # Create a graph tf_g = tf.Graph() with tf_g.as_default(): a = tf.constant(1.0, shape=[2, 3], name="a") c = tf.add(tf.placeholder(dtype=np.float32), tf.placeholder(dtype=np.float32), name="c") # Serialize the graph g = gde.Graph(tf_g.as_graph_def()) print("Before:\n{}".format(_indent(g.to_graph_def()))) # Modify the graph. # In this case we replace the two input placeholders with constants. # One of the constants (a) is a node that was in the original graph. # The other one (b) we create here. b = gde.make_const(g, "b", np.full([2, 3], 2.0, dtype=np.float32)) gde.swap_inputs(g[c.op.name], [g[a.name], b.output(0)]) print("After:\n{}".format(_indent(g.to_graph_def()))) # Reconstitute the modified serialized graph as TensorFlow graph... with g.to_tf_graph().as_default(): # ...and print the value of c, which should be 2x3 matrix of 3.0's with tf.Session() as sess: res = sess.run(c.name) print("Result is:\n{}".format(_indent(res)))
def test_connect(self): """Test for gde.connect.""" # Original code: # with self.graph.as_default(): # x = constant_op.constant([1., 1.], shape=[2], name="x") # y = constant_op.constant([2., 2.], shape=[2], name="y") # z = math_ops.add(x, y, name="z") x = gde.make_const(self.graph, "x", np.array([1., 1.], dtype=np.float32)) y = gde.make_const(self.graph, "y", np.array([2., 2.], dtype=np.float32)) z = self.graph.add_node("z", "Add") z.add_attr("T", tf.float32) z.set_inputs([x.outputs[0], y.outputs[0]]) z.infer_outputs() sgv = gde.sgv(x, y, z) gde.connect(sgv, gde.sgv(self.e.op).remap_inputs([0])) self.assertTrue( gde.OpMatcher("^foo/bar/e$").input_ops("^z$", "foo/d$")(self.e.op))
def test_multiswap(self): # Original code: # with self.graph.as_default(): # a3 = constant_op.constant(3.0, shape=[2], name="a3") # New code adds a NodeDef to the graph: a3_node = gde.make_const(self.graph, "a3", np.full([2], 3.0, dtype=np.float32)) gde.swap_ios( gde.sgv(a3_node).remap_outputs([0, 0]), gde.sgv(self.a0.op, self.a1.op)) self.assertTrue(gde.OpMatcher("c0").input_ops("a3", "b0")(self.c0.op)) self.assertTrue(gde.OpMatcher("c1").input_ops("a3", "b1")(self.c1.op))