How to use pre-trained embedding vectors to initialize an embedding layer in Keras?

Published on Aug. 22, 2023, 12:19 p.m.

To use pre-trained embedding vectors to initialize an embedding layer in Keras, you can load the pre-trained embedding vectors into a numpy array and use that array to set the weights of the Keras Embedding layer.

Here’s an example of how to load pre-trained GloVe word embeddings and use them to initialize an embedding layer in Keras:

import numpy as np
from keras.models import Sequential
from keras.layers import Embedding, Flatten, Dense

embedding_dim = 100
max_words = 10000
maxlen = 100

# load pre-trained GloVe embeddings
embeddings_index = {}
with open('glove.6B.100d.txt') as f:
    for line in f:
        word, coefs = line.split(maxsplit=1)
        coefs = np.fromstring(coefs, 'f', sep=' ')
        embeddings_index[word] = coefs

# prepare GloVe embedding matrix
embedding_matrix = np.zeros((max_words, embedding_dim))
for word, i in word_index.items():
    if i < max_words:
        embedding_vector = embeddings_index.get(word)
        if embedding_vector is not None:
            embedding_matrix[i] = embedding_vector

# define Keras model
model = Sequential()
model.add(Embedding(max_words, embedding_dim, input_length=maxlen))
model.layers[0].set_weights([embedding_matrix])
model.add(Flatten())
model.add(Dense(32, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# compile and fit the model
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
model.summary()

In this example, we first load the pre-trained GloVe word embeddings from a text file and store them in a dictionary (embeddings_index). We then create a numpy array (embedding_matrix) with shape (max_words, embedding_dim) to hold the GloVe embeddings for the max_words most common words in our dataset. We iterate over the words in our dataset (word_index) and for each word that is in the GloVe embeddings dictionary, we copy its embedding vector into the corresponding row of embedding_matrix.

We define a Keras model with an Embedding layer and one or more Dense layers for classification. We set the weights of the Embedding layer to be the GloVe embedding matrix that we created earlier.

We compile and fit the model using binary crossentropy loss for binary classification.