I've always relied on the Keras embedding layer for my NLP work. But for my latest project I want to use a custom embedding layer. I have gone through the steps to create a word2vec file but now what? Can someone provide an example of how I can replace the Keras embedding layer with my own layer when constructing a model?
2 Answers
First, make sure, you have the same vocabulary in your word2file and in your Keras code. If you Keras Tokenizer
, you can simply call fit_on_texts
with the text actually being the vocabulary.
Load your word embeddings as numpy matrix (just read all the numbers and then call np.array
).
When creating the Embedding
layer, call set_weights
and set the loaded matrix and set trainable
to False
.
You can find more at Keras blog but I am not sure how up to date it is because it is from 2016.
Before, I have implemented a downloaded embbeding using Keras' Embedding
package:
from keras.layers import Embedding
embedding_layer = Embedding(
num_words,
EMBEDDING_DIM,
weights=[embedding_matrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False
)