2

I have created LSTM network using Keras for next word prediction based on the context of the previous words in a sentence. I have written the code in Python, but have to deploy it with existing code of C++. So how to translate this chunk of code to C++ as I am new to it and I have been using built-in functions in python for the same.

I have been trying to convert each and every line of python code to C++ but that is consuming a lot of time, also I am not even able to read hdf5 and pkl files in C++. Python Code :

    from pickle import load
    from keras.models import load_model
    from keras.preprocessing.sequence import pad_sequences


def generate_seq(model, tokenizer, seq_length, seed_text, no_next_words):
    result = []
    in_text = seed_text

    for _ in range(no_next_words):
        # encode the text as integer
        encoded = tokenizer.texts_to_sequences([in_text])[0]
        # convert sequences to a fixed length
        encoded = pad_sequences([encoded], maxlen=seq_length, truncating='pre')
        # predict probabilities for each word
        yhat = model.predict_classes(encoded, verbose=0)
        # map predicted word index to word
        out_word = ''
        for word, index in tokenizer.word_index.items():
            if index == yhat:
                out_word = word
                break
        # append to input
        in_text += ' ' + out_word
        result.append(out_word)
    return ' '.join(result)

# load the model
model = load_model('model_chat2_3.h5')

# load the tokenizer
tokenizer = load(open('tokenizer_chat2_3.pkl', 'rb'))

# generate new text
while(True):
  inp = input("Enter:")
  generated = generate_seq(model, tokenizer, 2,inp, 1)
  print(generated)

C++ code :

#include <iostream>
#include <fstream>
#include <typeinfo>
#include <string>
using namespace std;

int main(int argc, const char * argv[]) {
ifstream myReadFile,myReadFile2;

myReadFile.open("/Users/Apple/New word_prediction/word_prediction/data/colab models/chat2_2/tokenizer_chat2_2.pkl");
myReadFile2.open("/Users/Apple/New word_prediction/word_prediction/data/colab models/chat2_2/model_chat2_2.h5");
        char output[1200];
        if (myReadFile.is_open()) {

            while (!myReadFile.eof()) {
                myReadFile >> output;
                cout<<output<<endl;
    }
        }
        myReadFile.close();
        myReadFile2.close();
        return 0;

    }

The output of C++ comes out to be some encoded string.

I tried referring to the previous answers but all of them are built very specifically to their use-cases. For e.g : Convert Keras model to C++. Basically, I have built keras model using LSTM networks(hidden layers: LSTM) while in the above link, it's built for CNN network, with completely different architecture. If I follow the above link I will have to completely change the code. Also in the other similar repositories, they work for CNN for image processing, but in my use case, I have to predict next words based on the context of previous words, through LSTM network. This model has good accuracy on the console, but it has to be tested on the real-time keyboard of mobile applications.

So my question is that if there is a method or tool through which I can convert some of the code to C++ or at least how can I read the keras model built on LSTM network(model_chat2_3.h5) and tokenizer_chat2_3.pkl files into C++. Also if someone can guide me to directly deploy the keras model on the mobile application, to work as a keyboard. Again all existing repositories are made to deploy CNN based models on the mobile app to do image related things

Keras LSTM model to android This will also help but no one has answered it yet.

I am new to C++ so not able to write the complete code for it, as I have a time constraint. Any sort of help is appreciated!

0 Answers0