tf.keras.preprocessing.sequence.pad_sequences 在 JavaScript

tf.keras.preprocessing.sequence.pad_sequences in JavaScript

我们如何在 TensorFlow.js 中实施 tf.keras.preprocessing.sequence.pad_sequences?

encoded_text = tokenizer.texts_to_sequences([input_text])[0]
pad_encoded = pad_sequences([encoded_text], maxlen=seq_len, truncating='pre')

universal sentence encoder可用于将文本转换为张量

require('@tensorflow/tfjs');
const use = require('@tensorflow-models/universal-sentence-encoder');

use.load().then(model => {
  // Embed an array of sentences.
  const sentences = [
    'Hello.',
    'How are you?'
  ];
  model.embed(sentences).then(embeddings => {
    // `embeddings` is a 2D tensor consisting of the 512-dimensional embeddings for each sentence.
    // So in this example `embeddings` has the shape [2, 512].
    embeddings.print(true /* verbose */);
  });
});

tf.pad 以后可以用来填充张量