如何使用顺序类获取tensorflow.keras中两个嵌入层的点积并设置嵌入层的权重?

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

我正在尝试使用

tensorflow.keras
构建一个模型,其中我得到两个具有预定义权重的嵌入层的点积(我将在编译模型时对其进行优化)。我在推理期间的目标是使用
embedding_layer_1
作为查找表,以便根据指定的索引获取
embedding_layer_1
的权重,这就是我保留
trainable=True
的原因。

weights_matrix
的形状是
(288, 3569)
,我喜欢能够获得
embedding_layer_1
(形状是
(288, 3569)
)和
embedding_layer_2
(形状是
(3569, 288)
)的点积。所以基本上
embedding_layer_2
embedding_layer_1
的转置。

这是网络:

import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Flatten, Embedding, Dot, Flatten

input_dim=288
output_dim=3569

model_1 = Sequential()
embedding_layer_1 = Embedding(input_dim=input_dim, output_dim=output_dim, name='embedding_layer_1', dtype='float64', trainable=True,  input_length=1)
embedding_layer_1.build((None,))
model_1.add(embedding_layer_1)
model_1.layers[0].set_weights([weights_matrix])
model_1.add(Flatten())

model_2 = Sequential()
embedding_layer_2 = Embedding(input_dim=input_dim, output_dim=output_dim, name='embedding_layer_2', dtype='float64', trainable=True,  input_length=1)
embedding_layer_2.build((None,))
model_2.add(embedding_layer_2)
model_2.layers[0].set_weights([role_skill_matrix])
model_2.add(Flatten())

dot_product = Dot(axes=-1)([model_1.output, model_2.output])

model = Sequential([model_1, model_2, dot_product])
model.summary()

我收到以下错误。我在上述网络上尝试了各种更改,例如转置

embedding_layer_1
并尝试获取
embedding_layer_1
的点积及其转置,以及其他变体,但没有任何效果。

任何帮助将不胜感激。

我正在使用

tensorflow==2.8.0
keras==2.8.0

    model = Sequential([model_1, model_2, dot_product])
  File "/Users/ayalaallon/opt/anaconda3/envs/ml-pipeline/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py", line 629, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/Users/ayalaallon/opt/anaconda3/envs/ml-pipeline/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/Users/ayalaallon/opt/anaconda3/envs/ml-pipeline/lib/python3.8/site-packages/keras/engine/sequential.py", line 178, in add
    raise TypeError('The added layer must be an instance of class Layer. '
TypeError: The added layer must be an instance of class Layer. Received: layer=KerasTensor(type_spec=TensorSpec(shape=(None, 1), dtype=tf.float32, name=None), name='dot/Squeeze:0', description="created by layer 'dot'") of type <class 'keras.engine.keras_tensor.KerasTensor'>.

Process finished with exit code 1
python tensorflow deep-learning tf.keras dot-product
1个回答
0
投票

我不确定您是否可以在

Dot
模型中使用像
Sequential
图层这样的图层。棘手的部分是
Sequential
模型中的层是按顺序堆叠的。在这种类型的模型中,一个层应该如何具有多个输入,我不知道。
幸运的是,您可以只使用
functional
API 来构建模型,并且您自己开始在
Dot
层构建中使用函数式语法。

import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.layers import Flatten, Embedding, Dot, Flatten

input_dim=288
output_dim=3569

in1 = tf.keras.layers.Input(input_dim)
embedding_layer_1 = Embedding(input_dim=input_dim, output_dim=output_dim, name='embedding_layer_1', 
                              dtype='float64', trainable=True,  input_length=1)(in1)
flat1 = Flatten()(embedding_layer_1)

in2 = tf.keras.layers.Input(input_dim)
embedding_layer_2 = Embedding(input_dim=input_dim, output_dim=output_dim, name='embedding_layer_2',
                              dtype='float64', trainable=True,  input_length=1)(in2)
flat2 = Flatten()(embedding_layer_2)

dot_product = Dot(axes=-1)([flat1, flat2])

model = Model(inputs=[in1, in2], outputs=[dot_product])
model.summary()

显然,模型需要

Input
层作为第一层,没有它们,
Embedding
层就会出现错误。它使用以下代码运行,没有错误:

import numpy as np
model.compile()

x = np.random.rand(100, 288)
model([x, x])
© www.soinside.com 2019 - 2024. All rights reserved.