Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This example is going to be incorporated into a more detailed blog post, but here is a brief idea:
To do sentiment analysis in Tensorflex however, we first need to do some preprocessing and prepare the graph model (
.pb
) as done multiple times before in other examples. For that, in theexamples/rnn-lstm-example
directory there are two scripts:freeze.py
andcreate_input_data.py
. Prior to explaining the working of these scripts you first need to download the original saved models as well as the datasets:examples/rnn-lstm-example/model
folderwordsList.npy
andwordVectors.npy
. These will be used to encode our text data intoUTF-8
encoding for feeding our RNN as input.Now, for the Python two scripts:
freeze.py
andcreate_input_data.py
:freeze.py
: This is used to create ourpb
model from the Python saved checkpoints. Here we will use the downloaded Python checkpoints' model to create the.pb
graph. Just runningpython freeze.py
after putting the model files in the correct directory will do the trick. In the same./model/
folder, you will now see a file calledfrozen_model_lstm.pb
. This is the file which we will load into Tensorflex. In case for some reason you want to skip this step and just get the loaded graph here is a Dropbox linkcreate_input_data.py
: Even if we can load our model into Tensorflex, we also need some data to do inference on. For that, we will write our own example sentences and convert them (read encode) to a numeral (int32
) format that can be used by the network as input. For that, you can inspect the code in the script to get an understanding of what is happening. Basically, the neural network takes in an input of a24x250
int32
(matrix) tensor created from text which has been encoded asUTF-8
. Again, runningpython create_input_data.py
will give you twocsv
files (one indicating positive sentiment and the other a negative sentiment) which we will later load into Tensorflex. The two sentences converted are:- Negative sentiment sentence: That movie was terrible.
- Positive sentiment sentence: That movie was the best one I have ever seen.
Both of these get converted to two files
inputMatrixPositive.csv
andinputMatrixNegative.csv
(bycreate_input_data.py
) which we load into Tensorflex next.Inference in Tensorflex:
Now we do sentiment analysis in Tensorflex. A few things to note:
Placeholder_1
add
and is the eventual result of a matrix multiplication. Of this obtained result we only need the first row24x250
representing our sentence/review1x2
vector. If the value of the first column is higher than the second column, then the network indicates a positive sentiment otherwise a negative sentiment. All this can be observed in the original repository in a Jupyter notebook here:First we will try for positive sentiment:
We only need the first row, the rest do not indicate anything:
Thus we can clearly see that the RNN predicts a positive sentiment. For a negative sentiment, next:
Thus we can clearly see that in this case the RNN indicates negative sentiment! Our model works :D