Skip to content

Add padSequences utility to tfjs-layers - #8738

Open
bruce619 wants to merge 1 commit into
tensorflow:masterfrom
bruce619:bruce619/feature/pad-sequences
Open

Add padSequences utility to tfjs-layers#8738
bruce619 wants to merge 1 commit into
tensorflow:masterfrom
bruce619:bruce619/feature/pad-sequences

Conversation

@bruce619

Copy link
Copy Markdown

Fixes #8737

Description

Adds a padSequences utility function to tfjs-layers, mirroring the
behavior of Python Keras's tf.keras.utils.pad_sequences. It pads or
truncates a list of numeric sequences to a uniform length and returns
the result as a Tensor2D.

Supports:

  • maxlen (auto-computed from the longest input sequence if omitted)
  • padding: 'pre' | 'post'
  • truncating: 'pre' | 'post'
  • custom value for padding (defaults to 0)
  • dtype: 'int32' | 'float32'

Exposed as padSequences from the tfjs-layers entrypoint.

Testing

Added unit tests in pad_sequences_test.ts covering default padding,
custom padding/truncating direction, custom pad value with float32
dtype, and empty input. All tests pass locally via:

yarn test --//:grep=padSequences

yarn lint also passes with no errors.

@google-cla

google-cla Bot commented Jul 27, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Add padSequences utility function for sequence preprocessing

1 participant