0
$\begingroup$

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?

$\endgroup$
1
  • 1
    $\begingroup$ lol'ed at the name of the post ) $\endgroup$
    – Kari
    Commented Jun 21, 2019 at 14:49

2 Answers 2

1
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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
        )
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.