Keras如何为每个InputNeuron构造一个共享的Embedding()层

2024-05-23 23:08:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在keras中创建一个深层神经网络,在输入层的每个元素被输入到更深层之前,使用相同的共享Embedding()-layer进行“编码”。在

每个输入都是一个定义对象类型的数字,网络应该学习一个嵌入,它封装了“这个对象是什么”的一些内部表示。在

因此,如果输入层有X维,而嵌入层有Y维,则第一个隐藏层应该由X*Y神经元(每个嵌入的输入神经元)组成。在

Here is a little image that should show the network architecture that I would like to create, where each input-element is encoded using a 3D-Embedding

我该怎么做?在


Tags: 对象网络layer元素类型编码thathere
1条回答
网友
1楼 · 发布于 2024-05-23 23:08:23
from keras.layers import Input, Embedding

first_input = Input(shape = (your_shape_tuple) )
second_input = Input(shape = (your_shape_tuple) )
...

embedding_layer = Embedding(embedding_size)

first_input_encoded = embedding_layer(first_input)
second_input_encoded = embedding_layer(second_input)
...

Rest of the model....

emnedding_层将具有共享权重。如果有很多输入,可以用层列表的形式来完成。在

如果要转换输入的张量,方法是:

^{pr2}$

这就是你要找的吗?在

相关问题 更多 >