如何从 Keras 嵌入层获取词向量 [英] How to get word vectors from Keras Embedding Layer

查看:35
本文介绍了如何从 Keras 嵌入层获取词向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently working with a Keras model which has a embedding layer as first layer. In order to visualize the relationships and similarity of words between each other I need a function that returns the mapping of words and vectors of every element in the vocabulary (e.g. 'love' - [0.21, 0.56, ..., 0.65, 0.10]).

有什么办法吗?

推荐答案

可以通过embedding层的get_weights()方法获取word embeddings(即本质上是embedding层的权重是嵌入向量):

You can get the word embeddings by using the get_weights() method of the embedding layer (i.e. essentially the weights of an embedding layer are the embedding vectors):

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

这篇关于如何从 Keras 嵌入层获取词向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆