Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!')
like image 252
mxmxlwlw Avatar asked Feb 09 '17 14:02

mxmxlwlw


1 Answers

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!')
like image 194
mxmxlwlw Avatar answered Oct 22 '22 07:10

mxmxlwlw