harman
harman

Reputation: 343

Dynamic change in input to a tensor at run time in tensorflow

Is it possible to change the input to a tensor dynamically during run time in tensorflow based on a flag which is a placeholder?

Eg:

check = tf.placeholder(tf.bool, name='check')

if <`check` condition == `True`>:
    output = node_1
else:
    output = node_2

Upvotes: 0

Views: 622

Answers (1)

ash
ash

Reputation: 6751

You could use the TensorFlow control flow operations like tf.cond. For example:

x = tf.placeholder(tf.float32)
check = tf.placeholder(tf.bool, name='check')
output = tf.cond(check, lambda: tf.add(x, 2), lambda: tf.add(x, 3))

with tf.Session() as sess:
  print(sess.run(output, feed_dict={check:True, x:3}))
  print(sess.run(output, feed_dict={check:False, x:3}))

will print:

5 (3+2 when check is True), and 6 (3+3 when check is False) Hope that helps.

Upvotes: 1

Related Questions