Yes, here is a little complicated. An important concept of Tensorflow is a lazy rating, which means that the Tensorflow node graph is computed first, and the graph is only evaluated at session.run.
For this line of code, x = x + 1 here x is of type Tensor, and + here is overloaded tf.add, so x = x + 1 actually creates a graph until the calculation is performed; and at each iteration, the graph (binary tree in this case) is added with another layer (another nested sum). session.run (model) will always initialize x to 0, session.run (x) will calculate x based on the graph so far constructed in this iteration. For example, in a 4 x iteration, 1 is added 4 times, because the graph at this iteration has 4 nested sums (or layers).
If this makes sense to you, I think that “both codes are essentially the same” will also make sense.
Note: strictly speaking, in the first iteration, x on the right side is of type Variable, but these are details, not the main point I'm trying to do ...
Yao zhang
source share