Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "append" Op at the beginning of a TensorFlow graph?

Tags:

tensorflow

I have a GraphDef proto file which I am importing using tf.import_graph_def. Ops can be added at the end of the graph like this:

final_tensor = tf.import_graph_def(graph_def, name='', return_elements=['final_tensor'])
new_tensor = some_op(final_tensor)

But I want to add Ops at the beginning of the graph, so essentially the first Op in the graph_def needs to take the output of my Op as input, how do I do it?

like image 900
Priyatham Avatar asked Jan 28 '17 11:01

Priyatham


People also ask

What is OPS in TensorFlow?

TensorFlow Operations, also known as Ops, are nodes that perform computations on or with Tensor objects. After computation, they return zero or more tensors, which can be used by other Ops later in the graph.

What is AutoGraph in TensorFlow?

AutoGraph transforms a subset of Python which operates on TensorFlow objects into equivalent TensorFlow graph code. When executing the graph, it has the same effect as if you ran the original code in eager mode.

What is tf Reset_default_graph ()?

Clears the default graph stack and resets the global default graph.


1 Answers

Finally found a way to do this. I am sure the function Yarolsav mentioned in the comments does something similar internally.

new_input = graph_def.node.add()
new_input.op = 'new_op_name'  # eg: 'Const', 'Placeholder', 'Add' etc
new_input.name = 'some_new_name'
# set any attributes you want for new_input here
old_input.input[0] = 'some_new_name'  #  must match with the name above

For details about how to set the attributes, see this file.

like image 96
Priyatham Avatar answered Oct 18 '22 10:10

Priyatham