Part 2: Understanding GRU models

You will now see how you can use Keras models to accept arbitrary sized batches of inputs. The ability to accept arbitrary sized batches is important for many reasons. For example, this allows you to define a single Keras model and experiment with different batch sizes during the model training stage, without having to change anything in the model.

For this exercise, keras and numpy (as np) have already been imported.

This exercise is part of the course

Machine Translation with Keras

View Course

Exercise instructions

  • Define an input layer that accepts an arbitrary sized batch of data having sequence length 3 and input size 4.
  • Define a GRU layer with 10 hidden units that consumes the previous input and produces an output.
  • Define a Model called model that takes the input layer as the input and produces the output of the GRU layer as the output. Remember that you can use the keras.models.Model(<argument>=<value>) syntax to define a model.
  • Predict the model output for both x1 and x2.

Hands-on interactive exercise

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

# Define an input layer
inp = keras.layers.____(____=(____))
# Define a GRU layer that takes in the input
gru_out = keras.layers.____(____)(____)
# Define a model that outputs the GRU output
____ = keras.models.____(inputs=____, outputs=____)

x1 = np.random.normal(size=(2,3,4))
x2 = np.random.normal(size=(5,3,4))

# Get the output of the model and print the result
y1 = ____.____(____)
y2 = ____.____(____)
print("shape (y1) = ", y1.shape, " shape (y2) = ", y2.shape)