将Python Keras NLP模型转换为Tensorflow.js [英] Converting Python Keras NLP Model to Tensorflowjs

查看:302
本文介绍了将Python Keras NLP模型转换为Tensorflow.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解有关Tensorflowjs的更多信息,但不幸的是,我一直无法将Keras NLP模型转换为Tensorflowjs.

I'm trying to learn more about Tensorflowjs, but sadly I'm stuck getting my Keras NLP Model converted to Tensorflowjs.

这就是我要转换的内容:

This is what I'm trying to convert:

from keras.models import load_model

from keras.preprocessing.sequence import pad_sequences

import pickle

list_classes = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]

model = load_model('Keras_Model/m.hdf5')
with open('Keras_Model/tokenizer.pkl', 'rb') as handler:
    tokenizer = pickle.load(handler)

list_sentences_train = ["I need help Stackoverflow"]

list_tokenized_train = tokenizer.texts_to_sequences(list_sentences_train)
maxlen = 200
X_t = pad_sequences(list_tokenized_train, maxlen=maxlen)


pred = model.predict(X_t)[0]

Tensorflowjs方面:

Tensorflowjs side:

import tf = require('@tensorflow/tfjs-node')

async function processModel(){
  const model = await tf.loadLayersModel('Server_Model/model.json');
}

如何使Tokenizer运行并做出正确的预测?

How I can get the Tokenizer running and make correct predictions?

推荐答案

实际上,我在Android上对文本进行分类时遇到了同样的问题.我已经准备好使用模型(tflite),但是如何像Keras在Python中那样标记这些语句.

Actually, I ran into the same problem while classifying text on Android. I had the model ( tflite ) ready to use, but how can I tokenize the sentences just as Keras did in Python.

我找到了一个简单的解决方案,已经在此处(对于Android)进行了讨论.

I found a simple solution which I have discussed here ( for Android ).

简单的想法是转换keras.preprocessing.text.Tokenizer 词汇表到JSON文件.可以使用以下任何一种方式解析此JSON文件: 包括JavaScript在内的编程语言.

The simple idea is to convert the keras.preprocessing.text.Tokenizer vocabulary to a JSON file. This JSON file could be parsed in any of the programming languages including JavaScript.

令牌生成器保存一个名为word_index的对象.

The Tokenizer holds a object called word_index.

index = tokenizer.word_index

word_index对象是一个字典,可以像这样转换为JSON,

The word_index object is a dict which can be converted to JSON like,

import json 
with open( 'word_dict.json' , 'w' ) as file:    
    json.dump( tokenizer.word_index , file )

JSON文件包含单词和索引对.您可以按链接中所述的JavaScript对其进行解析.

The JSON file contains pairs of words and indexes. You can parse it in JavaScript as mentioned in this link.

这篇关于将Python Keras NLP模型转换为Tensorflow.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆