keras cnn_lstm input layer not accepting 1-D input

谁说我不能喝 提交于 2019-12-05 20:22:23

There is no need to add those TimeDistributed wrappers. Currently, before adding the LSTM layer, your model looks like this (I have assumed repeat_length=5 and stride=1):

Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_2 (Conv1D)            (None, 3000, 75)          450       
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 600, 75)           0         
_________________________________________________________________
flatten_2 (Flatten)          (None, 45000)             0         
_________________________________________________________________
dense_4 (Dense)              (None, 1)                 45001     
=================================================================
Total params: 45,451
Trainable params: 45,451
Non-trainable params: 0
_________________________________________________________________

So if you want to add a LSTM layer, you can put it right after the MaxPooling1D layer like model.add(LSTM(16, activation='relu')) and just remove the Flatten layer. Now the model looks like this:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_4 (Conv1D)            (None, 3000, 75)          450       
_________________________________________________________________
max_pooling1d_3 (MaxPooling1 (None, 600, 75)           0         
_________________________________________________________________
lstm_1 (LSTM)                (None, 16)                5888      
_________________________________________________________________
dense_5 (Dense)              (None, 1)                 17        
=================================================================
Total params: 6,355
Trainable params: 6,355
Non-trainable params: 0
_________________________________________________________________

If you want you can pass the return_sequences=True argument to the LSTM layer and keep the Flatten layer. But only do such a thing after you have tried the first approach and you have gotten poor results, since adding return_sequences=True may not be necessary at all and it only increases your model size and decreases model performance.


As a side note: why did you change the loss function to sparse_categorical_crossentropy in the second model? There is no need to do that since binary_crossentropy would work fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!