【问题标题】:What's the sequence length of a Keras Bidirectional layer?Keras 双向层的序列长度是多少?
【发布时间】:2020-02-25 13:18:26
【问题描述】:

如果我有:

        self.model.add(LSTM(lstm1_size, input_shape=(seq_length, feature_dim), return_sequences=True))
        self.model.add(BatchNormalization())
        self.model.add(Dropout(0.2))

然后我的seq_length 指定我想一次处理多少个数据片。如果重要的话,我的模型是一个序列到序列(相同大小)。

但如果我有:

        self.model.add(Bidirectional(LSTM(lstm1_size, input_shape=(seq_length, feature_dim), return_sequences=True)))
        self.model.add(BatchNormalization())
        self.model.add(Dropout(0.2))

那么这会使序列大小加倍吗?或者在每个时间步,它是否在该时间步之前和之后得到seq_length / 2

【问题讨论】:

    标签: python keras neural-network lstm bidirectional


    【解决方案1】:

    使用双向 LSTM 层对序列长度没有影响。 我用以下代码对此进行了测试:

    from keras.models import Sequential
    from keras.layers import Bidirectional,LSTM,BatchNormalization,Dropout,Input
    
    model = Sequential()
    lstm1_size = 50
    seq_length = 128
    feature_dim = 20
    model.add(Bidirectional(LSTM(lstm1_size, input_shape=(seq_length, feature_dim), return_sequences=True)))
    model.add(BatchNormalization())
    model.add(Dropout(0.2))
    
    batch_size = 32
    
    model.build(input_shape=(batch_size,seq_length, feature_dim))
    
    model.summary()
    

    这导致以下双向输出

    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    bidirectional_1 (Bidirection (32, 128, 100)            28400     
    _________________________________________________________________
    batch_normalization_1 (Batch (32, 128, 100)            400       
    _________________________________________________________________
    dropout_1 (Dropout)          (32, 128, 100)            0         
    =================================================================
    Total params: 28,800
    Trainable params: 28,600
    Non-trainable params: 200
    

    没有双向层:

    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    lstm_1 (LSTM)                (None, 128, 50)           14200     
    _________________________________________________________________
    batch_normalization_1 (Batch (None, 128, 50)           200       
    _________________________________________________________________
    dropout_1 (Dropout)          (None, 128, 50)           0         
    =================================================================
    Total params: 14,400
    Trainable params: 14,300
    Non-trainable params: 100
    _________________________________________________________________
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 2019-05-29
      • 2019-01-03
      • 1970-01-01
      • 2019-04-28
      相关资源
      最近更新 更多