繁体   English   中英

如何从 Keras 嵌入层获取词向量

[英]How to get word vectors from Keras Embedding Layer

我目前正在使用具有嵌入层作为第一层的 Keras 模型。 为了可视化单词之间的关系和相似性,我需要一个函数来返回词汇表中每个元素的单词和向量的映射(例如,'love' - [0.21, 0.56, ..., 0.65, 0.10] )。

有什么办法吗?

您可以使用嵌入层的get_weights()方法获取词嵌入(即嵌入层的权重本质上是嵌入向量):

# 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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM