How to do it...

Here is how you can proceed with sentiment prediction using LSTM:

  1. Load the required packages and movie reviews dataset:
load_packages=c("text2vec","tidytext","tensorflow") 
lapply(load_packages, require, character.only = TRUE) 
data("movie_review") 
  1. Extract the movie reviews and labels as a dataframe and matrix respectively. In movie reviews, add an additional attribute "Sno" denoting the review number. In the labels matrix, add an additional attribute related to negative flag.
reviews <- data.frame("Sno" = 1:nrow(movie_review), 
                         "text"=movie_review$review, 
                         stringsAsFactors=F) 
 
labels <- as.matrix(data.frame("Positive_flag" = movie_review$sentiment,"negative_flag" = (1                    movie_review$sentiment)))
  1. Extract all the unique words ...

Get R Deep Learning Cookbook 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.