我已经为下一个单词的预测构建了一个Keras模型,并且我试图使用前端的模型来根据文本字段的输入来预测下一个单词,我必须将下面的代码从Python转换为JavaScript,但是没有找到任何合适的选项。有办法解决这个问题吗?
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在Python中的以下预测:
H 114下一个词建议:从不H 215H 116下一个词建议:doH 217H 118下一个词建议:要<>代码>H 219<代码>H 120下一个单词建议:历史上<代码>H 221/代码><代码>H 122下一个词建议:<<>代码>H 223<代码H 124下一个词建议:<>代码H 225代码><>代码代码<>代码>下一个词建议:<>代码>H 223下一个词建议:<>代码>H 223下一个词建议:<>代码H 225下一个单词建议:<>代码>H 221/代码><代码>H 122下一个单词建议:<>代码>H 223<代码>下一个词建议:<>代码H 225代码<226>
发布于 2021-03-05 06:23:26
我刚用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);https://stackoverflow.com/questions/65980817
复制相似问题