Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from keras.layers.embeddings import Embedding
- def pretrained_embedding_layer(word_to_vec_map, word_to_index):
- """
- Creates a Keras Embedding() layer and loads in pre-trained GloVe 50-dimensional vectors.
- Arguments:
- word_to_vec_map -- dictionary mapping words to their GloVe vector representation.
- word_to_index -- dictionary mapping from words to their indices in the vocabulary (400,001 words)
- Returns:
- embedding_layer -- pretrained layer Keras instance
- """
- vocab_len = len(word_to_index) + 1 # adding 1 to fit Keras embedding (requirement)
- emb_dim = word_to_vec_map["cucumber"].shape[0] # define dimensionality of your GloVe word vectors (= 50)
- # Initialize the embedding matrix as a numpy array of zeros of shape (vocab_len, dimensions of word vectors = emb_dim)
- emb_matrix = np.zeros((vocab_len, emb_dim))
- # Set each row "index" of the embedding matrix to be the word vector representation of the "index"th word of the vocabulary
- for word, index in word_to_index.items():
- emb_matrix[index, :] = word_to_vec_map[word]
- # Define Keras embedding layer with the correct output/input sizes, make it trainable. Use Embedding(...). Make sure to set trainable=False.
- embedding_layer = Embedding(vocab_len, emb_dim, trainable=False)
- # Build the embedding layer, it is required before setting the weights of the embedding layer. Do not modify the "None".
- embedding_layer.build((None,))
- # Set the weights of the embedding layer to the embedding matrix. Your layer is now pretrained.
- embedding_layer.set_weights([emb_matrix])
- return embedding_layer
Add Comment
Please, Sign In to add comment