【问题标题】:Keras functional API multiple input LSTMKeras 函数式 API 多输入 LSTM
【发布时间】:2019-03-22 01:26:47
【问题描述】:

我正在尝试使用 Keras 功能 API 构建两个输入 LSTM 模型。

这是我使用的代码:

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()

但是,我收到了以下我不明白的错误:

assert len(input_shape) >= 3 AssertionError

换行:

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

该层已经接受长度为 128 的张量。我在这里缺少什么?

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    如文件所述

    输入应该是至少3D,索引一的维度将 被认为是时间维度。

    TimeDistributed layer 将一个层应用于输入的每个时间切片。所以你应该设置return_sequences=True在上层LSTM_2返回完整的时间序列输出。

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

    【讨论】:

      猜你喜欢
      • 2019-05-17
      • 2018-09-14
      • 2017-07-25
      • 1970-01-01
      • 2018-02-16
      • 2018-02-22
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多