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)

你可能感兴趣的:(keras)