Daniyal Javaid
Daniyal Javaid

Reputation: 1636

Variables, Constants and Graphs in Tensorflow

I am new to tensorflow and after going through some basics from different sources I am completely confused about the graphs and their execution.Here is a 6 line code :

 x = tf.constant([35, 40, 45], name='x')
 y = tf.Variable(x + 5, name='y')

 model = tf.global_variables_initializer()

 with tf.Session() as session:
    session.run(model)
    print(session.run(y))

1. Line 1 and 2 creates a constant and a variable , at this point a graph is created ?

2.Is the graph created when I run the 'model' through session i.e variable initialization? and at what point the graph is executed ?

3.When the graph is executed why do we need to run the variable i.e 'session.run(y)' to print its value ?

Edited : enter image description here

Here is a line by line graph representation , is it correct ? I know 2(a) is wrong that is why i created 2(b) graph . So this is what happens to graph when I run these statements ?

Upvotes: 1

Views: 405

Answers (1)

Rudresh Panchal
Rudresh Panchal

Reputation: 1000

So Tensorflow runs in two phases,

  1. Creation Phase(Or building phase): Here you define your Variables, Constants and Placeholders, and their relations.(define the mathematical operations on them)
  2. Execution phase: Till now, all your variables and the computations applied (like matmul or adddition etc) are merely defined. Not computed. They are computed in this phase.

So to answer your questions:

Q1: At this point, yes the schema of the graph has been created(or the graph has been built) but it has not been executed.

Q2: The graph is executed(That is the actual initialization is done) when you call the run function on the initializer

Q3: You need to call run on the initializer first because before you do it, as mentioned before, the graph schema has merely been defined. The actual allocations and computations have not been done. When the tensor session is started and the run function called, the graph is executed and during the process the initialization of your variables is done. Before that they are not accessible as they still haven't been initialized even though they have been defined.

The tensorflow getting started guide here offers a great explanation of the same.

Hope this helps!

Upvotes: 3

Related Questions