Sentiment analysis with an RNN

The following example shows the implementation of sentiment analysis using an RNN. It has fixed-length movie reviews encoded as integer values, which are then converted to word embedding (embedding vectors) passed to LSTM layers in a recurrent manner that pick the last prediction as the output sentiment:

import numpy as npimport tensorflow as tffrom string import punctuationfrom collections import Counter'''movie review dataset for sentiment analysis'''with open('data/reviews.txt', 'r') as f:    movieReviews = f.read()with open('data/labels.txt', 'r') as f:    labels = f.read()'''data cleansing - remove punctuations'''text = ''.join([c for c in movieReviews if c not in punctuation])movieReviews = text.split('\n'

Get Neural Network Programming with TensorFlow now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.