TensorArray TensorArray_1_0: Failed to read from TensorArray index 0 because it has not been written to

I do not know how to use tensorrare. Here is the code. What is the mistake of this?

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!')
+4
source share
1 answer

I decided. It seems that inside bodyof while_loopwe should reassign the old TensorArray aOreturn value aO.write():

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

All 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!')
+4
source

Source: https://habr.com/ru/post/1669276/


All Articles