# upgrade tensorflow to tensorflow 2
%tensorflow_version 2.x
# display matplotlib plots
%matplotlib inline
from tensorflow import test
from tensorflow import device
TensorFlow 2.x selected.
The IMDB dataset consists of movie reviews (x_train) that have been marked as positive or negative (y_train). See the Word Vectors Tutorial for more details on the IMDB dataset.
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing import sequence
(imdb_x_train, imdb_y_train), (imdb_x_test, imdb_y_test) = imdb.load_data()
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/imdb.npz 17465344/17464789 [==============================] - 0s 0us/step
For a standard keras model, every input has to be the same length, so we need to set some length after which we will cutoff the rest of the review. (We will also need to pad the shorter reviews with zeros to make them the same length).
cutoff = 500
imdb_x_train_padded = sequence.pad_sequences(imdb_x_train, maxlen=cutoff)
imdb_x_test_padded = sequence.pad_sequences(imdb_x_test, maxlen=cutoff)
# see https://stackoverflow.com/questions/42821330/restore-original-text-from-keras-s-imdb-dataset
imdb_index_offset = 3
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense
Unlike last time, when we used convolutional layers, we're going to use an LSTM, a special type of recurrent network.
Using recurrent networks means that rather than seeing these reviews as one input happening all at once, with the convolutional layers taking into account which words are next to each other, we are going to see them as a sequence of inputs, with one word occurring at each timestep.
imdb_lstm_model = Sequential()
imdb_lstm_model.add(Embedding(input_dim=len(imdb.get_word_index()) + imdb_index_offset,
output_dim=100,
input_length=cutoff))
# return_sequences tells the LSTM to output the full sequence, for use by the next LSTM layer. The final
# LSTM layer should return only the output sequence, for use in the Dense output layer
imdb_lstm_model.add(LSTM(units=32, return_sequences=True))
imdb_lstm_model.add(LSTM(units=32))
imdb_lstm_model.add(Dense(units=1, activation='sigmoid')) # because at the end, we want one yes/no answer
imdb_lstm_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['binary_accuracy'])
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/imdb_word_index.json 1646592/1641221 [==============================] - 0s 0us/step
# Train using GPU acceleration
# (see https://colab.research.google.com/notebooks/gpu.ipynb#scrollTo=Y04m-jvKRDsJ)
device_name = test.gpu_device_name()
if device_name != '/device:GPU:0':
print(
'\n\nThis error most likely means that this notebook is not '
'configured to use a GPU. Change this in Notebook Settings via the '
'command palette (cmd/ctrl-shift-P) or the Edit menu.\n\n')
raise SystemError('GPU device not found')
with device('/device:GPU:0'):
imdb_lstm_model.fit(imdb_x_train_padded, imdb_y_train, epochs=1, batch_size=64)
Train on 25000 samples 25000/25000 [==============================] - 64s 3ms/sample - loss: 0.3914 - binary_accuracy: 0.8204
with device('/device:GPU:0'):
imdb_lstm_scores = imdb_lstm_model.evaluate(imdb_x_test_padded, imdb_y_test)
print('loss: {} accuracy: {}'.format(*imdb_lstm_scores))
25000/25000 [==============================] - 15s 589us/sample - loss: 0.3151 - binary_accuracy: 0.8690 loss: 0.31508431223869326 accuracy: 0.8689600229263306
Experiment with different model configurations from the one above. Try other recurrent layers, different numbers of layers, change some of the defaults. See Keras Recurrent Layers
Keep notes on what you try and what results you get.
Set up your own RNN model for the Reuters Classification Problem
Take the model from exercise 1 (imdb_lstm_model) and modify it to classify the Reuters data.
Think about what you are trying to predict in this case, and how you will have to change your model to deal with this.
from tensorflow.keras.datasets import reuters
from tensorflow.keras.preprocessing import sequence
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense
(reuters_x_train, reuters_y_train), (reuters_x_test, reuters_y_test) = reuters.load_data()
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/reuters.npz 2113536/2110848 [==============================] - 0s 0us/step