python - tensorflow map_fn TensorArray has inconsistent shapes -
i playing around map_fn function, , noticed outputs tensorarray, should mean capable of outputting "jagged" tensors (where tensors on inside have different first dimensions.
i tried see in action code:
import tensorflow tf import numpy np num_arrays = 1000 max_length = 1000 lengths = tf.placeholder(tf.int32) tarray = tf.map_fn(lambda x: tf.random_normal((x,),0,1),lengths,dtype=tf.float32) #should return tensorarray # starttensor = tf.random_normal(((tf.reduce_sum(lengths)),),0,1) # tarray = tf.tensorarray(tf.float32,num_arrays) # tarray = tarray.split(starttensor,lengths) # outarray = tarray.concat() tf.session() sess: outputarray,l = sess.run([tarray,lengths],feed_dict={lengths:np.random.randint(max_length,size=(num_arrays))}) print outputarray.shape, l
however got error:
"tensorarray has inconsistent shapes. index 0 has shape: [259] index 1 has shape: [773]"
this of course comes surprise me since under impression tensorarrays should able handle it. wrong?
while tf.map_fn()
use tf.tensorarray
objects internally, , tf.tensorarray
can hold objects of different size, program won't work as-is because tf.map_fn()
converts tf.tensorarray
result tf.tensor
stacking elements together, , operation fails.
you can implement tf.tensorarray
-based using lower-lever tf.while_loop()
op instead:
lengths = tf.placeholder(tf.int32) num_elems = tf.shape(lengths)[0] init_array = tf.tensorarray(tf.float32, size=num_elems) def loop_body(i, ta): return + 1, ta.write(i, tf.random_normal((lengths[i],), 0, 1)) _, result_array = tf.while_loop( lambda i, ta: < num_elems, loop_body, [0, init_array])
Comments
Post a Comment