Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
Cómo usar fn_map para asignar cada fila en una matriz C a su correspondiente en la matriz B

Como estoy trabajando con TensorFlow, me gustaría saber cómo mapear mis filas de un tensor C al índice de su fila correspondiente en la matriz B.

Aquí está el código que escribí:

 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)

Mi resultado esperado es obtener un tensor [2 4], donde 2 se refiere al índice del vector [0., 0., 3.] en las filas del tensor B, y 4 se refiere al índice del vector [2 .,5.,7.] en las filas del tensor B.

Tuve el siguiente 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

¿Cómo puedo resolver este problema usando la biblioteca TensorFlow? ¿Hay alguna alternativa al método fn_map en TensorFlow?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

No tiene que usar tf.map_fn . Tal vez intente algo como esto:

 import tensorflow as tf B = tf.constant([[ 0., 5., 2.], [ 0., 0., 0.], [ 0., 0., 3.],[1.,5.,6.], [2.,5.,7.]]) c = tf.constant([[0., 0., 3.], [2.,5.,7.]]) c_shape = tf.shape(c) b_shape = tf.shape(B) c = tf.reshape(tf.tile(c, [1, b_shape[0]]), [c_shape[0], b_shape[0], c_shape[1]]) z = tf.where(tf.reduce_all(tf.equal(B, c), -1)) z = tf.stack([z[i, 1] for i in tf.range(tf.shape(z)[0])], axis=0) print(z)
 tf.Tensor([2 4], shape=(2,), dtype=int64)

Utilizando:

 c = tf.constant([[0., 0., 3.], [2.,5.,7.],[2.,5.,7.],[2.,5.,7.],[2.,5.,7.],[2.,5.,7.],[2.,5.,7.],[2.,5.,7.]])

Usted obtiene:

 tf.Tensor([2 4 4 4 4 4 4 4], shape=(8,), dtype=int64)

Actualización 1 : si desea usar los índices en z para obtener los valores correspondientes en B , simplemente haga esto:

 z, _ = tf.unique(z) print(tf.gather(B, z))
 tf.Tensor( [[2. 5. 7.] [0. 0. 3.]], shape=(2, 3), dtype=float32)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!