简体   繁体   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 suitable option. 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合适的选择。 Is there any way to work around this?有没有办法解决这个问题?

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)

I am getting the following predictions for I in Python :Python中得到以下 I 的预测:

  • Next word suggestion: have下一个词建议:有
  • Next word suggestion: am下一个词建议:am
  • Next word suggestion: know下一个词建议:知道
  • Next word suggestion: think下一个词建议:认为
  • Next word suggestion: never下一个词建议:从不
  • Next word suggestion: do下一个词建议:做
  • Next word suggestion: want下一个词建议:想要
  • Next word suggestion: ever下一个词建议:曾经
  • Next word suggestion: will下一个词建议:将
  • Next word suggestion: see下一个词建议:见

I just wrote an alternative in node.js, maybe it can help you我刚刚在 node.js 中写了一个替代方案,也许它可以帮助你

Repo: https://github.com/Shadowhusky/node_tokenizer回购: https://github.com/Shadowhusky/node_tokenizer

You can install it with你可以安装它

npm install --save tf_node_tokenizer

Example:例子:

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