from tensorflow.keras.datasets import imdb
load_data
replaces any words outside the top 10,000 with a placeholder value (discussed shortly)number_of_words = 10000
NOTE: Following cell was added to work around a known issue with TensorFlow/Keras and NumPy at the time we created the slides—this issue is already fixed in a forthcoming version. See this cell's code on StackOverflow.
import numpy as np
# save np.load
np_load_old = np.load
# modify the default parameters of np.load
np.load = lambda *a,**k: np_load_old(*a, allow_pickle=True, **k)
(X_train, y_train), (X_test, y_test) = imdb.load_data(
num_words=number_of_words)
# This cell completes the workaround mentioned above
# restore np.load for future normal usage
np.load = np_load_old
X_train
and X_test
appear to be one-dimensionalX_train.shape
y_train.shape
X_test.shape
y_test.shape
y_train
and y_test
are one-dimensional arrays containing 1s and 0s, indicating whether each review is positive or negativeX_train
and X_test
are lists of integers, each representing one review’s contents%pprint # toggle pretty printing, so elements don't display vertically
X_train[123]
load_data
uses 2 for words with frequency rankings greater than num_words
word_to_index = imdb.get_word_index()
'great'
might appear in a positive movie review:word_to_index['great'] # 84th most frequent word
word_to_index
mapping, so we can look up words by frequency ratingindex_to_word = {index: word for (word, index) in word_to_index.items()}
[index_to_word[i] for i in range(1, 51)]
i - 3
accounts for the frequency ratings offsets in the encoded reviews i
values 0
–2
, get
returns '?'
; otherwise, get
returns the word with the key i - 3
in the index_to_word
dictionary' '.join([index_to_word.get(i - 3, '?') for i in X_train[123]])
y_train[123]
that this review is classified as positivey_train[123]
pad_sequences
function reshapes samples and returns a 2D arraywords_per_review = 200
from tensorflow.keras.preprocessing.sequence import pad_sequences
X_train = pad_sequences(X_train, maxlen=words_per_review)
X_train.shape
X_test
for evaluating the model laterX_test = pad_sequences(X_test, maxlen=words_per_review)
X_test.shape
fit
method via validation_data
argumenttrain_test_split
function from sklearn.model_selection import train_test_split
X_test, X_val, y_test, y_val = train_test_split(
X_test, y_test, random_state=11, test_size=0.20)
X_test
’s and X_val
’s shapes:X_test.shape
X_val.shape
Sequential
model and import the other layersfrom tensorflow.keras.models import Sequential
rnn = Sequential()
from tensorflow.keras.layers import Dense, LSTM, Embedding
Embedding
Layer (cont.)¶rnn.add(Embedding(input_dim=number_of_words, output_dim=128,
input_length=words_per_review))
input_dim=number_of_words
—Number of unique wordsoutput_dim=128
—Size of each word embeddinginput_length=words_per_review
—Number of words in each input samplernn.add(LSTM(units=128, dropout=0.2, recurrent_dropout=0.2))
units
—number of neurons in the layerdropout
—percentage of neurons to randomly disable when processing the layer’s input and outputDropout
layer that you can add to your models recurrent_dropout
—percentage of neurons to randomly disable when the layer’s output is fed back into the layer again to allow the network to learn from what it has seen previously1
for the units
argument'sigmoid
' activation function is preferred for binary classificationrnn.add(Dense(units=1, activation='sigmoid'))
binary_crossentropy
loss function:rnn.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
Embedding
layer’s output (128)rnn.summary()
rnn.fit(X_train, y_train, epochs=10, batch_size=32,
validation_data=(X_test, y_test))
evaluate
returns the loss and accuracy valuesresults = rnn.evaluate(X_test, y_test)
results
©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.