Get startedGet started for free

Your first RNN model

In this exercise you will put in practice the Keras modules to build your first RNN model and use it to classify sentiment on movie reviews.

This first model has one recurrent layer with the vanilla RNN cell: SimpleRNN, and the output layer with two possible values: 0 representing negative sentiment and 1 representing positive sentiment.

You will use the IMDB dataset contained in keras.datasets. A model was already trained and its weights stored in the file model_weights.h5. You will build the model's architecture and use the pre-loaded variables x_test and y_test to check the its performance.

This exercise is part of the course

Recurrent Neural Networks (RNNs) for Language Modeling with Keras

View Course

Exercise instructions

  • Add the SimpleRNN cell with 128 units.
  • Add a Dense layer with one unit for sentiment classification.
  • Use the proper loss function for binary classification.
  • Evaluate the model on the pre-trained validation set: (x_test, y_test).

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Build model
model = Sequential()
model.add(____(units=____, input_shape=(None, 1)))
model.add(Dense(____, activation='sigmoid'))
model.compile(loss='____', 
              optimizer='adam',
              metrics=['accuracy'])

# Load pre-trained weights
model.load_weights('model_weights.h5')

# Method '.evaluate()' shows the loss and accuracy
loss, acc = model.evaluate(____, ____, verbose=0)
print("Loss: {0} \nAccuracy: {1}".format(loss, acc))
Edit and Run Code