【问题标题】:LSTM for concaneted layers用于连接层的 LSTM
【发布时间】:2021-04-28 04:48:06
【问题描述】:

我正在尝试通过 LSTM 处理 5 层,但遇到了错误。 下面是代码。

yhdistelma=layers.concatenate([input1,input2, 
                              input3, input4, 
                              input5, input6, 
                              input7, input8, input9])

first_output  = layers.Dense(30,name='output_1')(yhdistelma)
second_output = layers.Dense(30,name='output_2')(yhdistelma)
third_output  = layers.Dense(30,name='output_3')(yhdistelma)
fourth_output = layers.Dense(30,name='output_4')(yhdistelma)
fifth_output  = layers.Dense(30,name='output_5')(yhdistelma)

hhs= layers.concatenate([first_output, second_output, 
                         third_output, fourth_output, 
                         fifth_output])

xxs=layers.LSTM(5)(hhs)

model.compile 给出的错误是

 Input 0 of layer lstm_2 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 150)

【问题讨论】:

    标签: tensorflow keras deep-learning lstm recurrent-neural-network


    【解决方案1】:

    LSTM 的输入形状应该是一个 3D 张量,例如[batch, timesteps, feature]。对于您的情况,您可以按如下方式扩展它的输入维度:

    from tensorflow.keras import layers
    
    yhdistelma    = layers.Input(20)
    first_output  = layers.Dense(30, name='output_1')(yhdistelma)
    second_output = layers.Dense(30, name='output_2')(yhdistelma)
    third_output  = layers.Dense(30, name='output_3')(yhdistelma)
    fourth_output = layers.Dense(30, name='output_4')(yhdistelma)
    fifth_output  = layers.Dense(30, name='output_5')(yhdistelma)
    hhs = layers.concatenate([first_output, second_output, 
                             third_output, fourth_output, 
                             fifth_output])
    xxs = layers.LSTM(5)(tf.expand_dims(hhs, axis=-1))
    
    
    print(hhs.shape)
    print(tf.expand_dims(hhs, axis=-1).shape)
    print(xxs.shape)
    (None, 150)
    (None, 150, 1)
    (None, 5)
    

    【讨论】:

    • 我们可以用 (None, 5, 30) 代替 (None, 150, 1) 吗?
    • 我想你误会了。 None, 150, 1 的形状来自hhs(None, 5)`来自LSTM。那是两个输出是不同的上下文。
    猜你喜欢
    • 1970-01-01
    • 2019-08-21
    • 2020-03-09
    • 2021-12-27
    • 2021-01-08
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多