Since I am working with TensorFlow, I would like to know how to map my rows from a tensor C to the index of its corresponding row in matrix B.
Here is the code I wrote:
B= tf.constant([[ 0., 5., 2.],[ 0., 0., 0.], [ 0., 0., 3.],[1.,5.,6.],[2.,5.,7.]])
def embeding_to_index(a_vector):
return np.where(np.all(a_vector==B,axis=1))[0].tolist()[0]
c = tf.constant([[0., 0., 3.],[2.,5.,7.]])
arr = tf.map_fn(fn=embeding_to_index,elems=c)
My expected result is to get a tensor [2 4], where 2 refers to index of the vector [0., 0., 3.] in the rows of the tensor B, and 4 refers to the index of the vector [2.,5.,7.] in the rows of the tensor B.
I got the following error:
---------------------------------------------------------------------------
AxisError Traceback (most recent call last)
<ipython-input-8-de82c21bac35> in <module>
79
80 arr=tf.map_fn(fn=embeding_to_index, # input & output have different dtypes
---> 81 elems=c)
82 # arr = tf.vectorized_map(fn=embeding_to_index_,elems=U)
~\.conda\envs\test\lib\site-packages\tensorflow\python\ops\map_fn.py in map_fn(fn, elems, dtype, parallel_iterations, back_prop, swap_memory, infer_shape, name)
266 back_prop=back_prop,
267 swap_memory=swap_memory,
--> 268 maximum_iterations=n)
269 results_flat = [r.stack() for r in r_a]
270
...
AxisError: axis 1 is out of bounds for array of dimension 0
How can I solve this issue using the TensorFlow library? Is there any alternative to the fn_map method in TensorFlow?