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

415
Views
¿Cuál es la alternativa de numpy.newaxis en tensorflow?

Hola, soy nuevo en tensorflow. Quiero implementar el siguiente código de Python en tensorflow.

 import numpy as np a = np.array([1,2,3,4,5,6,7,9,0]) print(a) ## [1 2 3 4 5 6 7 9 0] print(a.shape) ## (9,) b = a[:, np.newaxis] ### want to write this in tensorflow. print(b.shape) ## (9,1)
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

El comando correspondiente es tf.newaxis (o None , como en numpy). No tiene una entrada propia en la documentación de tensorflow, pero se menciona brevemente en la página de documentación de tf.stride_slice .

 x = tf.ones((10,10,10)) y = x[:, tf.newaxis] # or y = x [:, None] print(y.shape) # prints (10, 1, 10, 10)

Usar tf.expand_dims también está bien pero, como se indica en el enlace anterior,

Esas interfaces son mucho más amigables y muy recomendables.

over 4 years ago · Santiago Trujillo Report

0

Creo que sería tf.expand_dims -

 tf.expand_dims(a, 1) # Or tf.expand_dims(a, -1)

Básicamente, enumeramos el ID del eje donde se insertará este nuevo eje y los ejes/dimensiones posteriores se empujarán hacia atrás .

De los documentos vinculados, aquí hay algunos ejemplos de dimensiones en expansión:

 # 't' is a tensor of shape [2] shape(expand_dims(t, 0)) ==> [1, 2] shape(expand_dims(t, 1)) ==> [2, 1] shape(expand_dims(t, -1)) ==> [2, 1] # 't2' is a tensor of shape [2, 3, 5] shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5] shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5] shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]
over 4 years ago · Santiago Trujillo Report

0

a = a[..., tf.newaxis].astype("float32")

Esto también funciona

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!