简体   繁体   English

Keras功能API多输入LSTM

[英]Keras functional API multiple input LSTM

I am trying to build two inputs LSTM model using Keras functional API. 我正在尝试使用Keras功能API构建两个输入LSTM模型。

Here is the code I used: 这是我使用的代码:

inp_1 = 100
inp_2 = 100
out_1 = 10
N_FEATURES = 1


Input_1 = Input(shape=(inp_1, N_FEATURES), name='Input_1')
LSTM_1 = LSTM(name='LSTM_1', units=128)(Input_1)
Dense_1 = Dense(name='Dense_1', units=128)(LSTM_1)
Input_2 = Input(shape=(inp_2,), name='Input_2')
Dense_2 = Dense(name='Dense_2', units=128)(Input_2)
merge_2 = concatenate([Dense_1, Dense_2])

RepeatVector_1 = RepeatVector(out_1, name='RepeatVector_1')(merge_2)
LSTM_2 = LSTM(name='LSTM_2', units=128)(RepeatVector_1)
output = TimeDistributed(Dense(1,activation='linear'))(LSTM_2)

model = Model(inputs=[Input_1, Input_2], output=    output)

model.compile()

However, I got the following error that I do not understand: 但是,出现以下我不明白的错误:

assert len(input_shape) >= 3 AssertionError 断言len(input_shape)> = 3 AssertionError

for line: 对于行:

output = TimeDistributed(Dense(1,activation='linear'))(LSTM_2)

The layer already accepts a tensor of length 128. What am I missing here? 该层已经接受长度为128的张量。这里我缺少什么?

As the document says 正如文件所说

The input should be at least 3D , and the dimension of index one will be considered to be the temporal dimension. 输入应至少为3D ,索引一的维度将被视为时间维度。

TimeDistributed layer applies a layer to every temporal slice of an input. TimeDistributed层将一层应用于输入的每个时间片。 So you should set return_sequences=True to return the complete time series output in upper layer LSTM_2 . 因此,您应该设置return_sequences=True以返回上层LSTM_2的完整时间序列输出。

LSTM_2 = LSTM(name='LSTM_2', units=128,return_sequences=True)(RepeatVector_1)
# LSTM_2 output shape =(?, 10, 128)

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

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