6

I don't know how to use tensorarray. Here's the code. What's the bug in that?

import tensorflow as tf

aI=tf.TensorArray(tf.int32, 2)
aO=tf.TensorArray(tf.int32, 2)
aI=aI.unpack([[1,2],[1,2]])
def body(i,aI,aO):
    aO.write(i, aI.read(i)+1)
    return (i+1, aI, aO)
cond=lambda i, *_ : i<2
_, _, aO=tf.while_loop(cond, body, [0,aI,aO])
r=aO.pack()
with tf.Session() as sess:
    res=sess.run(r)
    print('done!')

1 Answer 1

8

I solved. It seems that inside the body of while_loop we should reassign the old TensorArray aO with the returned value of aO.write():

def body(i,aI,aO):
    aO=aO.write(i, aI.read(i)+1)
    return (i+1, aI, aO)

Whole code:

import tensorflow as tf

aI=tf.TensorArray(tf.int32, 2)
aO=tf.TensorArray(tf.int32, 2)
aI=aI.unpack([[1,2],[1,2]])
def body(i,aI,aO):
    aO=aO.write(i, aI.read(i)+1)
    return (i+1, aI, aO)
cond=lambda i, *_ : i<2
_, _, aO=tf.while_loop(cond, body, [0,aI,aO])
r=aO.pack()
with tf.Session() as sess:
    res=sess.run(r)
    print('done!')
Sign up to request clarification or add additional context in comments.

1 Comment

Note that currently (TF 1.4/1.5) TensorArray methods changed: unpack() -> unstack() and pack() -> stack()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.