How to Traverse a Tensor in TensorFlow

To traverse a tensor, we must use tf.map_fun function if the shape of tensor in unkown.

Here is an example:

import tensorflow as tf
import numpy as np

#data
def lookupTensor(y):
    #for i in range(y.shape[0]):
    print tf.shape(y)[0]
    eles = tf.map_fn(lambda x: x, y)
    return eles
    #for i in eles:
        #return i
x = np.random.random((10,10))
y_ = tf.placeholder(tf.float32,[None,10], name='input_y')

zz = lookupTensor(y_)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print sess.run(zz, feed_dict = {y_ : x})

The result is:

[[0.27342883 0.1530804  0.28495997 0.4466156  0.14734495 0.6563114
  0.8744778  0.43549702 0.5137387  0.02526552]
 [0.69358    0.74062335 0.5821915  0.22825919 0.05558707 0.56274813
  0.16048354 0.44796464 0.06519987 0.08140732]
 [0.402039   0.75282735 0.94024444 0.28801167 0.8442312  0.39745176
  0.39581087 0.8162748  0.7876757  0.45012134]
 [0.13744378 0.35864714 0.67010033 0.76092124 0.58320326 0.21305872
  0.9149774  0.9250241  0.68775773 0.25050306]
 [0.71072257 0.11768598 0.8920408  0.5774183  0.4512966  0.57520777
  0.59076536 0.42872414 0.4733914  0.01992757]
 [0.04814712 0.5177618  0.1741912  0.1826878  0.5587369  0.24354403
  0.8808466  0.02770878 0.29776815 0.7573681 ]
 [0.99160266 0.7231127  0.29390228 0.3750267  0.2738133  0.79001737
  0.8394186  0.18569376 0.59341645 0.81849146]
 [0.79299957 0.8459454  0.42224753 0.33818603 0.11004677 0.6402795
  0.5150225  0.27065045 0.2824011  0.55063593]
 [0.8139863  0.8166408  0.94273    0.9831243  0.3933435  0.37253633
  0.26316255 0.47187638 0.34482962 0.92831296]
 [0.72270626 0.06897362 0.60993916 0.22796552 0.21799998 0.22170398
  0.6416591  0.441043   0.5897468  0.4539335 ]]

,