web-dev-qa-db-fra.com

Comment obtenir des vecteurs Word à partir de Keras Embedding Layer

Je travaille actuellement avec un modèle Keras qui a une couche d'intégration comme première couche. Afin de visualiser les relations et la similitude des mots entre eux, j'ai besoin d'une fonction qui renvoie le mappage des mots et des vecteurs de chaque élément du vocabulaire (par exemple "amour" - [0,21, 0,56, ..., 0,65, 0,10] ).

Y a-t-il un moyen de le faire?

10
dermaschder

Vous pouvez obtenir les intégrations Word en utilisant la méthode get_weights() de la couche d'intégration (c'est-à-dire que les poids d'une couche d'intégration sont essentiellement les vecteurs d'intégration):

# if you have access to the embedding layer explicitly
embeddings = emebdding_layer.get_weights()[0]

# or access the embedding layer through the constructed model 
# first `0` refers to the position of embedding layer in the `model`
embeddings = model.layers[0].get_weights()[0]

# `embeddings` has a shape of (num_vocab, embedding_dim) 

# `Word_to_index` is a mapping (i.e. dict) from words to their index, e.g. `love`: 69
words_embeddings = {w:embeddings[idx] for w, idx in Word_to_index.items()}

# now you can use it like this for example
print(words_embeddings['love'])  # possible output: [0.21, 0.56, ..., 0.65, 0.10]
20
today