簡體   English   中英

TensorFlow.js 中是否提供 texts_to_sequences、pad_sequences 或者是否有其他替代方案?

[英]Is texts_to_sequences, pad_sequences available in TensorFlow.js or is there any other alternative for this?

I have build a Keras model for next word prediction and I am trying to use my model in front-end for predicting next word based on input from the text field, I have to convert the following code from Python to JavaScript but did not find any合適的選擇。 有沒有辦法解決這個問題?

from keras.preprocessing.sequence import pad_sequences
input_text = input().strip().lower()

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

for i in (model.predict(pad_encoded)[0]).argsort()[-10:][::-1]:
  pred_word = tokenizer.index_word[i]
  print("Next word suggestion:",pred_word)

Python中得到以下 I 的預測:

  • 下一個詞建議:有
  • 下一個詞建議:am
  • 下一個詞建議:知道
  • 下一個詞建議:認為
  • 下一個詞建議:從不
  • 下一個詞建議:做
  • 下一個詞建議:想要
  • 下一個詞建議:曾經
  • 下一個詞建議:將
  • 下一個詞建議:見

我剛剛在 node.js 中寫了一個替代方案,也許它可以幫助你

回購: https://github.com/Shadowhusky/node_tokenizer

你可以安裝它

npm install --save tf_node_tokenizer

例子:

const { Tokenizer } = require("tf_node_tokenizer");
const tokenizer = new Tokenizer({ num_words: 5, oov_token = "<unk>", });

const text = [
  "<start> Cake and frosting all over a face and hands tells a happy story.  <end>",
  "<start> A baby is feeding himself with his hands and is smeared with food. <end>",
  "<start> A baby eating pink dessert in a highchair <end>"
];

tokenizer.fitOnTexts(text);
tokenizer.texts_to_sequences(text);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM