先贴一段模型代码代码
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
map_height, map_width, channels分别为输入图像的长、宽、高
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()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv_lst_m2d_1 (ConvLSTM2D) (None, 15, 40, 40, 30) 35760
_________________________________________________________________
conv_lst_m2d_2 (ConvLSTM2D) (None, 15, 40, 40, 50) 144200
_________________________________________________________________
conv_lst_m2d_3 (ConvLSTM2D) (None, 15, 40, 40, 60) 237840
_________________________________________________________________
conv_lst_m2d_4 (ConvLSTM2D) (None, 40, 40, 70) 327880
=================================================================
Total params: 745,680
Trainable params: 745,680
Non-trainable params: 0
_________________________________________________________________
Model: "model_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 15, 40, 40, 3) 0
_________________________________________________________________
conv_lst_m2d_5 (ConvLSTM2D) (None, 15, 40, 40, 30) 35760
_________________________________________________________________
conv_lst_m2d_6 (ConvLSTM2D) (None, 15, 40, 40, 50) 144200
_________________________________________________________________
conv_lst_m2d_7 (ConvLSTM2D) (None, 15, 40, 40, 60) 237840
_________________________________________________________________
conv_lst_m2d_8 (ConvLSTM2D) (None, 40, 40, 70) 327880
=================================================================
Total params: 745,680
Trainable params: 745,680
Non-trainable params: 0
_________________________________________________________________
如上图所示,在LSTM的计算流程中,只有图中所示的4个部分需要参数,取其中之一展开,最后参数量 × 4 \times4 ×4即可。
上图是第一层卷积的示意图,卷积核 f i l t e r filter filter个数是30。
由 8940 × 4 = 35760 8940\times4=35760 8940×4=35760,即得到模型的 s u m m a r y summary summary输出结果中的35760。
设置第一层的 r e t u r n s e q u e n c e s = T r u e return_sequences=True returnsequences=True,可以将第一层的输出作为第二层的convLSTM的输入,输入的形状为 40 × 40 × 30 40\times40\times30 40×40×30,卷积核 f i l t e r = 40 filter=40 filter=40
由 36050 × 4 = 144200 36050\times4=144200 36050×4=144200,即得到模型的 s u m m a r y summary summary输出结果中的144200。
后面的层的计算以此类推即可。