Isa-rentacs commented on a change in pull request #13680: [MXNET-1121] Example to demonstrate the inference workflow using RNN URL: https://github.com/apache/incubator-mxnet/pull/13680#discussion_r251306508
########## File path: cpp-package/example/inference/README.md ########## @@ -39,3 +39,72 @@ Alternatively, The script [unit_test_inception_inference.sh](<https://github.com ``` ./unit_test_inception_inference.sh ``` + +### [sentiment_analysis_rnn.cpp](<https://github.com/apache/incubator-mxnet/blob/master/cpp-package/example/inference/sentiment_analysis_rnn.cpp>) +This example demonstrates how you can load a pre-trained RNN model and use it to predict the sentiment expressed in the given line of the movie review with the MXNet C++ API. The example performs the following tasks +- Loads the pre-trained RNN model. +- Loads the dictionary file containing the word to index mapping. +- Converts the input string to vector of indices that's truncated or padded to match the input data length. +- Runs the forward pass and predicts the sentiment score between 0 to 1 where 1 represents positive sentiment. + +The example uses a pre-trained RNN model trained with a IMDB dataset. The RNN model was built by exercising the [GluonNLP Sentiment Analysis Tutorial](<http://gluon-nlp.mxnet.io/examples/sentiment_analysis/sentiment_analysis.html#>). The tutorial uses 'standard_lstm_lm_200' available in Gluon Model Zoo and fine tunes it for the IMDB dataset +The model consists of : +- Embedding Layer +- 2 LSTM Layers with hidden dimension size of 200 +- Average pooling layer +- Sigmoid output layer +The model was trained for 10 epochs to achieve 85% test accuracy. +The visual representation of the model is [here](<http://gluon-nlp.mxnet.io/examples/sentiment_analysis/sentiment_analysis.html#Sentiment-analysis-model-with-pre-trained-language-model-encoder>). + +The model files can be found here. +- [sentiment_analysis-symbol.json](< https://s3.amazonaws.com/mxnet-cpp/RNN_model/sentiment_analysis-symbol.json>) +- [sentiment_analysis-0010.params](< https://s3.amazonaws.com/mxnet-cpp/RNN_model/sentiment_analysis-0010.params>) +- [sentiment_token_to_idx.txt](<https://s3.amazonaws.com/mxnet-cpp/RNN_model/sentiment_token_to_idx.txt>) Each line of the dictionary file contains a word and a unique index for that word, separated by a space, with a total of 32787 words generated from the training dataset. +The example downloads the above files while running. + +The example's command line parameters are as shown below: + +``` +./sentiment_analysis_rnn --help +Usage: +sentiment_analysis_rnn +--input Input movie review line.e.g. "This movie is the best." The input is trimmed or padded with 0s to match the max_num_words. +[--max_num_words] The number of words in the sentence to be considered for sentiment analysis. Default is 10 Review comment: Is there any upper bound for ```max_num_words```? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services