4

How is this graph acyclic? assign add op adds x to itself.

import tensorflow as tf
sess = tf.Session()
x = tf.Variable(1300,name="x")
y = tf.Variable(200, name="y")
z = tf.add(x, y,name="z")
b = x.assign_add(z)
init = tf.initialize_all_variables()
writer = tf.train.SummaryWriter("/tmp/logdir", sess.graph)
sess.run(init)
print(sess.run(b))

TensorBoard Graph for the computation

Clearly there is a bi-directional edge between AssignAdd and X.

Why is X depicted twice as a variable?

Himaprasoon
  • 2,609
  • 3
  • 25
  • 46

2 Answers2

4

As Olivier points out, the graph for your program is a DAG. The graph visualizer takes some liberties when rendering the graph to make it easier to understand. In particular, there are no "bidirectional" edges in the runtime itself, but instead TensorFlow includes "reference edges" for variables, which are like passing a mutable value (like a pointer or a mutable reference) into a C/C++ function, as they allow the recipient to modify the same underlying storage used for the variable.

Note that it is legal for TensorFlow graphs to contain one or more cycles, or even nested cycles. The tf.while_loop() function provides a means of creating structured cycles to represent iterative computations, for which TensorFlow can compute gradients. However, for your use with a simple variable, you do not need a cycle.

Community
  • 1
  • 1
mrry
  • 125,488
  • 26
  • 399
  • 400
3

Clearly there is a bi-directional edge between AssignAdd and X.

Each operation Assign or AssignAdd has two inputs and no output:

  • a tf.Variable: the variable to which we assign a value. Here you can see a bidirectional edge because the operation reads the old value from x and then writes back the new value
  • a tf.Tensor: the value assigned to the variable (or added to the variable)

Why is X depicted twice as a variable?

The variable x appears once in the graph in the big block named X, but is used twice:

  • in the operation tf.add(x, y): the graph reads the value of x as an input
  • in the AssignAdd operation: as said before, x is the tf.Variable input of the operation AssignAdd.

Conclusion

The graph is made acyclic because each operation wanting to update the value of x has the variable x as input, but not output. If the operation Assign had a variable as output, it would indeed lead to cycles.

Olivier Moindrot
  • 27,908
  • 11
  • 92
  • 91