Skip to content

Instantly share code, notes, and snippets.

@rahulremanan
Last active February 28, 2023 18:13
Show Gist options
  • Save rahulremanan/12ba7f20433071b8d28a6f7275d727e7 to your computer and use it in GitHub Desktop.
Save rahulremanan/12ba7f20433071b8d28a6f7275d727e7 to your computer and use it in GitHub Desktop.
Using Tensorflow while_loop to manipulate the named tuples
Pair = collections.namedtuple('Pair', 'j, k')
ijk_0 = (tf.constant(0), Pair(tf.constant(1), tf.constant(2)))
c = lambda i, p: i < 10
b = lambda i, p: (i + 1, Pair((p.j + p.k), (p.j - p.k)))
ijk_final = tf.while_loop(c, b, ijk_0)
print(ijk_final[-1].j.numpy(), ijk_final[-1].k.numpy())
print(ijk_final[-1][0].numpy(), ijk_final[-1][1].numpy())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment