Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

332
Vistas
¿Hay alguna forma de usar keras.pad_sequences en javascript?
@keras_export('keras.preprocessing.sequence.pad_sequences') def pad_sequences(sequences, maxlen=None, dtype='int32', padding='pre', truncating='pre', value=0.): return sequence.pad_sequences( sequences, maxlen=maxlen, dtype=dtype, padding=padding, truncating=truncating, value=value)

Quiero transformar este código a javascript.

Funciona así:

 sequence = [[1], [2, 3], [4, 5, 6]] tf.keras.preprocessing.sequence.pad_sequences(sequence, maxlen=2)
 array = 0,1 2,3 5,6
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Puede truncar y rellenar sus secuencias con Javascript de esta manera:

 const sequence = [[1], [2, 3], [4, 5, 6]]; var new_sequence = sequence.map(function(e) { const max_length = 2; const row_length = e.length if (row_length > max_length){ // truncate return e.slice(row_length - max_length, row_length) } else if (row_length < max_length){ // pad return Array(max_length - row_length).fill(0).concat(e); } return e; }); console.log('Before truncating and paddig: ',sequence) console.log('After truncating and paddig: ', new_sequence) // "Before truncating and paddig: ", [[1], [2, 3], [4, 5, 6]] // "After truncating and paddig: ", [[0, 1], [2, 3], [5, 6]]

que es equivalente al siguiente código de Python con Tensorflow:

 import tensorflow as tf def truncate_and_pad(row): row_length = tf.shape(row)[0] if tf.greater(row_length, max_length): # truncate return row[row_length-max_length:] elif tf.less(row_length, max_length): # pad padding = tf.constant([[max_length-row_length.numpy(), 0]]) return tf.pad(row, padding, "CONSTANT") else: return row max_length = 2 sequence = tf.ragged.constant([[1], [2, 3], [4, 5, 6]]) Y = tf.map_fn(truncate_and_pad, sequence)

pero en realidad no necesita ninguna función elegante.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda