Last active
December 4, 2020 13:48
-
-
Save Sirsirious/0b7e5d2e0b8f39eabbfaefeba6d1270a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
example_input = "I loved the way that the actors were cast, also, It is clear that they've put a huge effort in post-production." | |
#example_input = "Try your movie review here!" | |
# Steps explained: | |
# 1st: tokenize input. We cast it to an iterator to fake a generator. | |
input_iter = iter([example_input]) | |
input_tokens = data.tokenize(input_iter, vocab_file='en_8k.subword') | |
# 2nd: cast the results to a list and get the first value (the tokens, not the label or anything else) | |
tokenized_input = list(input_tokens)[0] | |
# 3rd: Add fake batch dimmension | |
tokenized_with_batch = tokenized_input[None, :] | |
# 4th: input it to the model and get the logprobs | |
sentiment_log_probs = sentiment_analysis_model(tokenized_with_batch) | |
# 5th: get the values to between 0 and 1 by exponentiating the logprobs. | |
norm_log_probs = numpy.exp(sentiment_log_probs) | |
# 6th: get if it's either true or false by checking the position of the greatest values at norm_log_probs dimmension 0 (that's what argmax does) | |
sentiment = numpy.argmax(norm_log_probs[0]) | |
print('Input review:\n"{}"\nThe sentiment is: {}'.format(example_input, "Positive" if sentiment else "Negative")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment