Dims
Dims

Reputation: 51019

How to control assignments order in Tensorflow?

I have implemented some tensor initializations as assigmnets in Tensowrflow graph. Simultaneously, these assignments should be called in correct order, because ones use results of anothers. If I expose assignment nodes to outside, user (myself too) should run them with session call

sess.run([assign1, assign2, ...])

in correct orded and can mistake. Can I join the sequence of assignments into single operation and expose only it run?

UPDATE

I wrote this code and found it prints eye? Why? It should print random inits of b, shouldn't it?

import tensorflow as tf

tf.reset_default_graph()

a = tf.eye(2, 2)
b = tf.get_variable("b", shape=(2,2))
c = tf.get_variable("c", shape=(2,2))

assign_c = tf.assign(c, b)
assign_b = tf.assign(b, a)

with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())

    sess.run([assign_c, assign_b])

    print(c.eval())

UPDATE 2

The code below shows I can't control assignmen order with group:

import tensorflow as tf

tf.reset_default_graph()

a = tf.eye(2, 2)
b = tf.get_variable("b", shape=(2,2), initializer=tf.zeros_initializer)
c = tf.get_variable("c", shape=(2,2))

assign_c = tf.assign(c, b)
assign_b = tf.assign(b, a)

incorrect_init = tf.group(assign_b, assign_c)
correct_init = tf.group(assign_c, assign_b)

with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())

    sess.run([incorrect_init])
    #sess.run([correct_init])

    print(c.eval())

Result is always

[[ 1.  0.]
 [ 0.  1.]]

regardless I call correct_init or incorrect_init. Why? How to force order?

Upvotes: 1

Views: 864

Answers (2)

Andrii
Andrii

Reputation: 1906

You can use tf.group to group operators. Tensorflow will resolve all dependencies for you if there are any.

UPDATE

c <- assign_c =>
c <- b =>
c <- assign_b =>
c <- a

even if you omit sess.run([assign_c, assign_b]) step.

UPDATE 2 If you need to keep execution order for 3 independent Tensors or Operators you may do it with control_dependencies, but ONLY if there are no any dependencies.

with tf.control_dependencies([first]):
    op1 = tf.no_op()
with tf.control_dependencies([op1, second]):
    op2 = tf.no_op()
with tf.control_dependencies([op2, third]):
    op3 = tf.no_op()

with tf.Session() as s:
    op3.aval()

Upvotes: 1

nessuno
nessuno

Reputation: 27042

The order of the variables in a single sess.run call doesn't mean anything.

sess.run([assign1, assign2, ...])

it equivalent to

sess.run([..., assign2, assign1, ...])

what really matters are the relationship among the nodes you're executing.

In your example, the output of c.eval() is eye, because:

  1. sess.run([assign_c, assign_b]) invokes assign_c and assign_b.
  2. assign_b depends on a.
  3. assign_c depends on b.
  4. thus c depends on b that depends on a

Thus: -> resolve a first.

Upvotes: 1

Related Questions