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!')
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!')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With