首页 > 解决方案 > 将 LSTM 与具有嵌入层的并行 MLP 网络连接起来

问题描述

我正在尝试使用非时间因变量的并行输入来构建 LSTM 模型。其中一些变量本质上是分类变量,我想通过嵌入层创建它们,然后稍后加入 LSTM。

请参阅下面我正在尝试的代码,但我收到“TypeError:unhashable type:'list'”的错误

以下是我目前编写的代码。

model = Model(inputs=[cat_els, ts_inputs], outputs=out_class)

我的 cat_els 定义为(3个张量的列表):

[<tf.Tensor 'flatten_10/Reshape:0' shape=(?, ?) dtype=float32>,
 <tf.Tensor 'flatten_11/Reshape:0' shape=(?, ?) dtype=float32>,
 <tf.Tensor 'flatten_12/Reshape:0' shape=(?, ?) dtype=float32>]

ts_inputs 定义为:

<tf.Tensor 'input_3:0' shape=(?, 6, 17) dtype=float32>

错误回溯

 File "<ipython-input-48-601f97a5b348>", line 31, in <module>
    model = Model(inputs=[cat_els, ts_inputs], outputs=out_class)

  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 93, in __init__
    self._init_graph_network(*args, **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 147, in _init_graph_network
    if len(set(self.inputs)) != len(self.inputs):

TypeError: unhashable type: 'list'

谁能告诉我我在这里可能做错了什么?

标签: pythonkerasdeep-learninglstm

解决方案


Keras 不支持作为输入层列表本身的输入,输入列表需要包含输入层,您可以通过连接输入来做到这一点:

model = Model(inputs=cat_els + [ts_inputs], outputs=out_class)

那么这个模型将有 4 个输入层。


推荐阅读