Skip to content

Instantly share code, notes, and snippets.

@ikhlestov
Created September 11, 2017 20:29
Show Gist options
  • Save ikhlestov/46fecf638be11679c901e1a6b9abea7b to your computer and use it in GitHub Desktop.
Save ikhlestov/46fecf638be11679c901e1a6b9abea7b to your computer and use it in GitHub Desktop.
pytorch: tensorflow while loop
import tensorflow as tf
first_counter = tf.constant(0)
second_counter = tf.constant(10)
some_value = tf.Variable(15)
# condition should handle all args:
def cond(first_counter, second_counter, *args):
return first_counter < second_counter
def body(first_counter, second_counter, some_value):
first_counter = tf.add(first_counter, 2)
second_counter = tf.add(second_counter, 1)
return first_counter, second_counter, some_value
c1, c2, val = tf.while_loop(
cond, body, [first_counter, second_counter, some_value])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
counter_1_res, counter_2_res = sess.run([c1, c2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment