Get startedGet started for free

Creating the text generation model

In this exercise, you will define a text generation model using Keras.

The variables n_vocab containing the vocabulary size and input_shape containing the shape of the data used for training are already loaded in the environment. Also, the weights of a pre-trained model is available on file model_weights.h5. The model was trained with 40 epochs on the training data. Recap that to train a model in Keras, you just use the method .fit() on the training data (X, y), and the parameter epochs. For example:

model.fit(X_train, y_train, epochs=40)

This exercise is part of the course

Recurrent Neural Networks (RNNs) for Language Modeling with Keras

View Course

Exercise instructions

  • Add one LSTM layer returning the sequences.
  • Add one LSTM layer not returning the sequences.
  • Add the output layer with n_vocab units.
  • Display the model summary.

Hands-on interactive exercise

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

# Instantiate the model
model = Sequential(name="LSTM model")

# Add two LSTM layers
model.add(____(64, input_shape=input_shape, dropout=0.15, recurrent_dropout=0.15, return_sequences=____, name="Input_layer"))
model.add(____(64, dropout=0.15, recurrent_dropout=0.15, return_sequences=____, name="LSTM_hidden"))

# Add the output layer
model.add(Dense(____, activation='softmax', name="Output_layer"))

# Compile and load weights
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.load_weights('model_weights.h5')
# Summary
model.____
Edit and Run Code