Converting Python Keras NLP Model to Tensorflowjs

前端 未结 1 985
醉梦人生
醉梦人生 2021-01-28 04:05

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:

<         


        
相关标签:
1条回答
  • 2021-01-28 04:27

    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.

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

    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.

    The Tokenizer holds a object called word_index.

    index = tokenizer.word_index
    

    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 )
    

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

    0 讨论(0)
提交回复
热议问题