sudsag
sudsag

Reputation: 1

Tensorflow graph building mechanism

When I tried to understand the underlying mechanism of the following tensorflow graph building code, I was confused about 3rd and 4th lines. I assumed that in side the +operator overloading , adder_node builds references to both a and b. However when the adder_node is executed on line 4, what would be the mechanism to distinguish a(a: 3) and b(b:3). Lets say, if the placeholders are filled with values and if the adder_node has references to both a,b why do we have to pass these parameters again.

a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b 
print(sess.run(adder_node, {a: 3, b: 4.5}))

Upvotes: 0

Views: 57

Answers (1)

Mihail Burduja
Mihail Burduja

Reputation: 3256

The a, b, and adder_node are nodes in a graph.

The adder_node knows to take the values inside a and b and to perform an operation on them. The graph looks like this:

Your graph

When you perform sess.run(adder_node) you're telling tensorflow to evaluate the value of adder_node (aka to perform all the dependent operations and perform the operation of adder_node).

In order to calculate adder_node tf will calculate the value of a and b, which are placeholders, so their operation is to take the values from the feed_dict. So, every time you need to compute adder_node you will have to provide values for the placeholders, so they can get computed.

{a: 3, b: 4.5} is not specifically passing parameters to adder_node but passing parameters to the graph.

You can have this code which passes a, b, and c as parameters to the graph, and adder_node add z an c:

a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
c = tf.placeholder(tf.float32)
z = a + b 
adder_node = z + c
print(sess.run(adder_node, {a: 3, b: 4.5, c: 1.5}))

Upvotes: 2

Related Questions