我目前正在使用Keras模型,其中第一层是嵌入层。为了可视化每个词之间的关系和相似度,我需要一个函数来返回词汇表中每个元素的映射及其向量(例如“love”- [0.21, 0.56,...,0.65,0.10])。
有没有什么方法可以做到这一点?
我目前正在使用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]
word_to_index['__padding__'] = 0
。 - Vaibhavemebdding_layer
?它显示未定义。我在文档中找不到它。我认为这可能是一个拼写错误。 - Guilherme Parreira