简体   繁体   English

Keras顺序模型的多个嵌入层

[英]Multiple Embedding layers for Keras Sequential model

I am using Keras (tensorflow backend) and am wondering how to add multiple Embedding layers into a Keras Sequential model. 我正在使用Keras(tensorflow后端),想知道如何在Keras顺序模型中添加多个嵌入层。

More specifically, I have several columns in my dataset which have categorical values and I have considered using one-hot encoding but have determined that the number of categorical items is in the hundreds leading to a large and far too sparse set of columns. 更具体地说,我的数据集中有几列具有分类值,并且我考虑过使用单热编码,但是确定分类项的数量在数百个之内,导致列的设置过于庞大且过于稀疏。 Upon looking for solutions I have found that Keras' Embedding layer appears to solve the problem very elegantly. 在寻找解决方案时,我发现Keras的Embedding层似乎非常优雅地解决了该问题。 However, most of the examples (and Keras documentation) illustrate a very simple situation with one Embedding layer. 但是,大多数示例(和Keras文档)都说明了一个具有一个嵌入层的非常简单的情况。

Unfortunately, I do not know how to integrate the multiple Embedding layers as input into a single model. 不幸的是,我不知道如何将多个嵌入层作为输入集成到单个模型中。

My code looks like this, but it does not work, and I am guessing that the multiple Embedding layers act sequentially (first Embedding layer is input the the second and so on) rather than be a multiple input sources to the model: 我的代码看起来像这样,但是它不起作用,并且我猜想多个Embedding层按顺序起作用(第一个Embedding层输入第二个,依此类推),而不是模型的多个输入源:

model = Sequential()
model.add(Embedding(500, 64, input_length=10))  # categorical col 1
model.add(Embedding(100, 64, input_length=10))  # categorical col 2
model.add(Embedding(500, 64, input_length=10))  # categorical col 3
model.add(Flatten... 
model.add(Dense...

My question is how would I establish a Keras Sequential model such that I would be able to use the three Embedding layers shown above. 我的问题是如何建立Keras序列模型,以便能够使用上面显示的三个Embedding层。 What specifically goes in between the first and last layers: 具体在第一层和最后一层之间是什么:

model = Sequential()
#
# What goes here?
#
model.add(Dense...

Am I on the right track, or is my approach incorrect and I need to establish the model in a different manner? 我是在正确的道路上,还是我的方法不正确,我需要以其他方式建立模型吗? Any suggestions/examples are appreciated! 任何建议/示例,不胜感激!

This can be done easily if you switch to the functional API , first have a read. 如果您切换到功能性API ,那么先阅读一下即可轻松完成。 Then you can build a model with multiple inputs that represent different columns: 然后,您可以使用代表不同列的多个输入来构建模型:

col1, col2, col3 = Input(shape=(10,)), Input(shape=(10,)), ...
col1_embeded = Embedding(500, 64)(col1)
col2_embedded = Embedding(100, 64)(col2)
# ...

The gist of this the layers are callable objects that build the computation graph. 这些层的要点是构建计算图的可调用对象。 You can also for example share embedding layers between columns by simply using the same Embedding layer. 例如,您还可以通过简单地使用同一嵌入层在列之间共享嵌入层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM