简体   繁体   中英

tf.keras.preprocessing.sequence.pad_sequences in JavaScript

How can we implement tf.keras.preprocessing.sequence.pad_sequences in TensorFlow.js?

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

The universal sentence encoder can be used to convert text into tensors

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 can later be used to padd the tensors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM