如何从TFHub下载的预先训练好的word2vec模型中获取单词向量?

问题描述 投票:0回答:1

所以我使用的是TFHub的以下word2vec模型。

embed = hub.load("https://tfhub.dev/google/Wiki-words-250-with-normalization/2")

这个对象的类型是:

tensorflow.python.saved_model.load.Loader._recreate_base_user_object.<locals>._UserObject

虽然我可以用这个模型来嵌入文本列表 但我不清楚如何访问单词嵌入本身。

tensorflow deep-learning nlp word2vec
1个回答
0
投票

首先,我们来讨论一下什么是 embed 其实呢?据? 公文జజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజ embed 对象是基于以TensorFlow 2格式存储的Skipgram模型创建的TextEmbedding。

Skipgram模型只是一个前馈神经网络,它将TextEmbedding对象中的 一气呵成 词汇表征作为输入,并将它 计算 的词嵌入。所以,这些词嵌入不是存储在模型中,而是被计算出来的。

所以,如果你想要单独的词嵌入,那么你可以像这样一次传递一个词。

# word embedding of `apple`
>>> apple_embedding = embed(["apple"])
>>> apple_embedding.shape
TensorShape([1, 250])

>>> #concatenation of three different word embeddings
>>> group = embed(["apple", "banana", "carrot"])
>>> group.shape
TensorShape([3, 250])
© www.soinside.com 2019 - 2024. All rights reserved.