我正在尝试为我的分层数据实现 Facebook ( Link ) 一篇论文中讨论的庞加莱嵌入。您可能会找到关于 Poincaré 嵌入的更容易理解的解释 here .
基于这篇论文,我找到了 Tensorflow 的一些实现 here和 here以及 tfa.layers.PoincareNormalize在 Tensorflow 插件中。后者甚至与上面提到的论文有链接,这让我相信这对我来说可能是一个很好的起点。但是,到目前为止,我没有成功实现 tfa.layers.PoincareNormalize 并且除了我链接的 API 页面上的一些通用信息之外,也找不到任何文档。
有谁知道应该如何实现这一层以提供论文中讨论的双曲线空间中的嵌入?我的出发点是使用标准嵌入层的实现,如下所示(它实际上是分类变量的实体嵌入)?
input = Input(shape=(1, ))
model = Embedding(input_dim=my_input_dim,
output_dim=embed_dim, name="my_feature")(input)
model = Reshape(target_shape=(embed_dim, ))(model)
model = Dense(1)(model)
model = Activation('sigmoid')(model)
由于输入不同,简单地用 tfa.layers.PoincareNormalize 替换嵌入层不起作用。我假设它可以放在嵌入层之后的某个地方,以便在反向传播步骤中,“值”在每次迭代时都被投影到双曲线空间中,但到目前为止也没有运气。
最佳答案
庞加莱嵌入
However, while complex symbolic datasets often exhibit a latent hierarchical structure, state-of-the-art methods typically learn embeddings in Euclidean vector spaces, which do not account for this property. For this purpose, we introduce a new approach for learning hierarchical representations of symbolic data by embedding them into hyperbolic space – or more precisely into an n-dimensional Poincaré ball.
Poincaré 嵌入允许您在非欧几里得空间中创建分层嵌入。与中心的矢量相比,庞加莱球外侧的矢量在层次结构中较低。
将欧几里得度量张量映射到黎曼度量张量的变换是一个开放的 d 维单位球。
在这个非欧式空间中 2 个向量之间的距离计算为
research paper for Poincaré embeddings写得很精彩,你也会在流行的库中找到一些很棒的实现。不用说,他们被低估了。
您可以使用的两个实现位于 -
tensorflow_addons.PoincareNormalize
gensim.models.poincare
Tensorflow 插件实现
根据文档,对于一维张量,
tfa.layers.PoincareNormalize
沿轴 = 0 计算以下输出。 (x * (1 - epsilon)) / ||x|| if ||x|| > 1 - epsilon
output =
x otherwise
对于更高维的张量,它沿 独立地对每个一维切片进行归一化。尺寸轴 .这种转换可以简单地应用于 n-dims 的嵌入。让我们为时间序列的每个元素创建一个 5dim 的嵌入。在这种情况下,维度轴=-1,从欧几里得空间映射到非欧几里得空间。
from tensorflow.keras import layers, Model, utils
import tensorflow_addons as tfa
X = np.random.random((100,10))
y = np.random.random((100,))
inp = layers.Input((10,))
x = layers.Embedding(500, 5)(inp)
x = tfa.layers.PoincareNormalize(axis=-1)(x) #<-------
x = layers.Flatten()(x)
out = layers.Dense(1)(x)
model = Model(inp, out)
model.compile(optimizer='adam', loss='binary_crossentropy')
utils.plot_model(model, show_shapes=True, show_layer_names=False)
model.fit(X, y, epochs=3)
Epoch 1/3
4/4 [==============================] - 0s 2ms/step - loss: 7.9455
Epoch 2/3
4/4 [==============================] - 0s 2ms/step - loss: 7.5753
Epoch 3/3
4/4 [==============================] - 0s 2ms/step - loss: 7.2429
<tensorflow.python.keras.callbacks.History at 0x7fbb14595310>
Gensim 实现另一个实现 Poincare 嵌入可以在 Gensim 中找到。它与您在使用 Gensim 的 Word2Vec 时使用的非常相似。
过程将是——
from gensim.models.poincare import PoincareModel
relations = [('kangaroo', 'marsupial'), ('kangaroo', 'mammal'), ('gib', 'cat'), ('cow', 'mammal'), ('cat','pet')]
model = PoincareModel(relations, size = 2, negative = 2) #Change size for higher dims
model.train(epochs=10)
print('kangroo vs marsupial:',model.kv.similarity('kangaroo','marsupial'))
print('gib vs mammal:', model.kv.similarity('gib','mammal'))
print('Embedding for Cat: ', model.kv['cat'])
kangroo vs marsupial: 0.9481239343527523
gib vs mammal: 0.5325816385250299
Embedding for Cat: [0.22193988 0.0776986 ]
有关训练和保存 Poincare 嵌入的更多详细信息,请访问 here .
关于tensorflow - 如何使用 tfa.layers.PoincareNormalize 实现庞加莱嵌入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64047435/