keras 搭建網絡模型(lstm 和 convlstm)一些參數理解

1 所有網絡模型的輸入, input_shape 都不包含samples。訓練數據和測試數據的shape纔是(samples, input_shape)。

  eg: 有一批100張32*32的RGB圖片,若要處理這批圖片(分類,目標識別),搭建網絡模型的input_shape 應爲(3, 32, 32)(假設dataformat格式爲channels_first)

2 搭建網絡模型時既可選擇指定batch_input_shape,也可選擇指定input_shape。
batch_input_shape 和input_shape的關係是:batch_input_shape =(batch_size, input_shape)

3 keras 有Sequential 和Model兩種搭建網絡的方式,Sequential只能順序搭建無分支結構,Model則可以搭建多分支結構。

以Sequential 和Model兩種方式搭建同樣的網絡結構


from keras.layers import (
Input,
ConvLSTM2D,
)

from keras.models import Model
from keras.models import Sequential


def Seq():
    '''
     input_shape 爲(time_steps, map_height, map_width, channels)
     time_steps 就是將一個樣例分爲多少個時間點讀入,x1,x2...,xt,的t
     return_sequences爲True時每一個時間點都有輸出
     return_sequences爲False時,只有最後一個時間點有輸出
    '''
    seq = Sequential()
    seq.add(ConvLSTM2D(filters=30, kernel_size=(3, 3),
                       input_shape=(15, 40, 40, 3),
                       padding='same', return_sequences=True, data_format='channels_last'))

    seq.add(ConvLSTM2D(filters=50, kernel_size=(3, 3),
                       padding='same', return_sequences=True, data_format='channels_last'))

    seq.add(ConvLSTM2D(filters=60, kernel_size=(3, 3),
                       padding='same', return_sequences=True, data_format='channels_last'))

    seq.add(ConvLSTM2D(filters=70, kernel_size=(3, 3),
                       padding='same', return_sequences=False, data_format='channels_last'))

    seq.summary()

def main():
    Inputs=[]
    Outputs=[]
    input = Input(shape=(15, 40, 40, 3))
    Inputs.append(input)
    convlstm1= ConvLSTM2D(filters=30, kernel_size=(3,3),
                          padding='same', return_sequences=True, data_format='channels_last')(input)
    convlstm2 = ConvLSTM2D(filters=50, kernel_size=(3, 3),
                           padding='same', return_sequences=True, data_format='channels_last')(convlstm1)
    convlstm3 = ConvLSTM2D(filters=60, kernel_size=(3, 3),
                           padding='same', return_sequences=True, data_format='channels_last')(convlstm2)
    convlstm4 = ConvLSTM2D(filters=70, kernel_size=(3, 3),
                           padding='same', return_sequences=False, data_format='channels_last')(convlstm3)
    Outputs.append(convlstm4)
    model =Model(inputs=input, outputs=convlstm4)
    model.summary()

if __name__ == '__main__':
    Seq()
    main()

4 LSTM 、ConvLSTM和SimpleRNN等RNN layer input_shape格式應該加上time_steps,格式爲(time_steps, input_size)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章