Now let's compare that to the code that includes pretrained GloVe vectors encoded in a 2D matrix:
sequence_input = Input(shape=(sequence_length,), dtype='int32')
embedding_layer = Embedding(input_dim=vocab_size,
output_dim=embedding_dim,
weights=[embedding_matrix],
input_length=sequence_length,
trainable=False,
name="embedding")(sequence_input)
For the most part, this code looks equivalent. There are two key differences:
- We initialize the layer weights to be contained in the GloVe matrix that we assembled with weights=[embedding_matrix].
- We also set the layer to trainable=False. This will prevent us from updating our weights. You may wish to fine tune the weights in a similar way to how we fine tuned the CNN we built in Chapter 8, Transfer Learning with Pretrained CNNs, but most of the time that isn't necessary or helpful.