tensorflow - Combining conditionals and control dependencies -
i'm trying execute conditional piece of code in turn dependent on op executing first. simple versions of work, following:
x = tf.variable(0.) x_op = tf.assign(x, 1.) tf.control_dependencies([x_op]): true_fun = lambda: tf.assign_add(x, 3.) false_fun = lambda: tf.constant([]) pred = tf.constant(true) cond_op = control_flow_ops.cond(pred, true_fun, false_fun) where evaluating cond_op sets x 4.0 expected. more complex version doesn't work:
def rest(x): tf.gather(x, tf.range(1, tf.size(x))) x = tf.variable([0., 1.]) x_op = tf.assign(x, [0., 1., 2.], validate_shape=false) tf.control_dependencies([x_op]): true_fun = lambda: tf.assign(x, rest(x), validate_shape=false) false_fun = lambda: tf.constant([]) pred = tf.constant(true) cond_op = control_flow_ops.cond(pred, true_fun, false_fun) in particular x gets assigned [1.] instead of [1., 2.]. logic i'm going x first assigned [0., 1., 2.], , then gets trimmed [1., 2.]. incidentally appears have size of x changing, since if in initial x_op assignment x gets assigned [1., 2.] instead of [0., 1., 2.], evaluating cond_op results in x being assigned [2.], correct behavior. i.e. first gets updated [1., 2.], , trimmed [2.].
note with tf.control_dependencies applies operations created inside block. when call rest(x) inside block x referring still old x return value of tf.variable function, tensor holding initial value of variable. can pass new value calling rest(x_op) instead. here complete working snippet :
import tensorflow tf def rest(x): return tf.gather(x, tf.range(1, tf.size(x))) x = tf.variable([0., 1.]) x_op = tf.assign(x, [0., 1., 2.], validate_shape=false) tf.control_dependencies([x_op]): true_fun = lambda: tf.assign(x, rest(x_op), validate_shape=false) false_fun = lambda: tf.constant([]) pred = tf.constant(true) cond_op = tf.cond(pred, true_fun, false_fun) tf.session(""): x.initializer.run() print(cond_op.eval())
Comments
Post a Comment