【问题标题】:Shape of the LSTM layers in multilayer LSTM model多层 LSTM 模型中 LSTM 层的形状
【发布时间】:2020-08-16 10:45:25
【问题描述】:
    model = tf.keras.Sequential([tf.keras.layers.Embedding(tokenizer.vocab_size, 64),tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64,return_sequences=True))     
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
    ])

第二层有 64 个隐藏单元,由于 return_sequences=True,它也会输出 64 个序列。但是如何将其馈送到 32 个隐藏单元的 LSTM。不会导致形状不匹配错误吗?

【问题讨论】:

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


    【解决方案1】:

    实际上不会,不会导致它。首先第二层的输出形状不是64,而是128。这是因为您正在使用Bidirectional 层,它将通过向前和向后传递连接起来,因此您的输出将是(None, None, 64+64=128)。可以参考link

    RNN 数据的形状如下:(Batch_size, time_steps, number_of_features)。这意味着当您尝试使用不同的神经元连接两层时,特征会根据神经元的数量增加或减少。您可以关注特定链接以获取更多信息details

    对于您的特定代码,这就是模型摘要的样子。因此,简而言之,它们不会不匹配。

    Model: "sequential_1"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    embedding (Embedding)        (None, None, 64)          32000     
    _________________________________________________________________
    bidirectional (Bidirectional (None, None, 128)         66048     
    _________________________________________________________________
    bidirectional_1 (Bidirection (None, 64)                41216     
    _________________________________________________________________
    dense_2 (Dense)              (None, 64)                4160      
    _________________________________________________________________
    dense_3 (Dense)              (None, 1)                 65        
    =================================================================
    Total params: 143,489
    Trainable params: 143,489
    Non-trainable params: 0
    _________________________________________________________________
    

    【讨论】:

    • “这意味着当你尝试用不同的神经元连接两层时,特征会根据神经元的数量增加或减少”我没有得到这个说法
    • 哦,好的,知道了。所以基本上如果我在输入数据中有 10 个特征,但我设置为 8 个神经元。它会忽略剩下的 2 个特征吗?
    猜你喜欢
    • 2018-02-27
    • 1970-01-01
    • 2018-11-27
    • 2020-08-31
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    相关资源
    最近更新 更多