我想以这种方式实现有状态卷积LSTM:
# build CNN/LSTM and train it.
#
model = Sequential()
# build CNN/LSTM and train it.
model.add(TimeDistributed(Conv2D(16, (3, 3), padding='same'), input_shape=(210, 22, 26, 1)))
model.add(Activation('elu'))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(Dropout(0.2))
model.add(TimeDistributed(Conv2D(32, (3, 3), padding='same')))
model.add(Activation('elu'))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(Dropout(0.2))
model.add(TimeDistributed(Conv2D(64, (3, 3), padding='same')))
model.add(Activation('elu'))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(TimeDistributed(Flatten()))
model.add(Conv1D(16, 3, padding='same'))
model.add(Activation('elu'))
model.add(MaxPooling1D(pool_size=8))
model.add(Bidirectional(LSTM(64, batch_input_shape=(32, 26, 16), return_sequences=True, stateful=True)))
model.add(Activation('elu'))
model.add(Bidirectional(LSTM(128, return_sequences=False, stateful=True)))
model.add(Activation('elu'))
model.add(Dense(1, activation='sigmoid'))
adammgm = keras.optimizers.Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0001)
model.compile(loss='mean_squared_error', optimizer=adammgm, metrics=['accuracy'])
print(model.summary())
# saves the model weights after each epoch
# if the validation loss decreased
#
checkpointer = ModelCheckpoint(filepath= odir_a + "/temp_lstm_model_weights-{epoch:02d}.h5")
model.fit_generator(generate_reduced_dimension_arrays_from_file(ilist_a, edfroot_a, featroot_a, labroot_a, feat_ext_a, lab_ext_a, num_channels_a, feat_fdur_a, win_len_a, models_order, lstm_batch_size_a, NEDC_DEF_BATCH_FILE), steps_per_epoch=NEDC_DEF_STEPS_PER_EPOCH, epochs=lstm_epoch_size_a, callbacks=[checkpointer])
但是当我想实现这个结构时,我遇到了这个错误:
ValueError:如果RNN是有状态的,则需要知道其批量大小。指定输入张量的批量大小:
- 如果使用Sequential模型,请通过将batch_input_shape
参数传递给第一层来指定批量大小。
- 如果使用功能API,请通过将batch_shape
参数传递到输入图层来指定时间维度。
我真实的所有帖子,但我仍然不知道如何解决这个问题。
答案 0 :(得分:1)
问题解决了。解决方案是在CNN的第一层使用batch_input_shape而不是input_shape:
# build CNN/LSTM and train it.
#
model = Sequential()
# build CNN/LSTM and train it.
model.add(TimeDistributed(Conv2D(16, (3, 3), padding='same'), batch_input_shape=(32, 210, 22, 26, 1)))
model.add(Activation('elu'))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(Dropout(0.2))
model.add(TimeDistributed(Conv2D(32, (3, 3), padding='same')))
model.add(Activation('elu'))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(Dropout(0.2))
model.add(TimeDistributed(Conv2D(64, (3, 3), padding='same')))
model.add(Activation('elu'))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(TimeDistributed(Flatten()))
model.add(Conv1D(16, 3, padding='same'))
model.add(Activation('elu'))
model.add(MaxPooling1D(pool_size=8))
model.add(Bidirectional(LSTM(64, return_sequences=True, stateful=True)))
model.add(Activation('elu'))
model.add(Bidirectional(LSTM(128, return_sequences=False, stateful=True)))
model.add(Activation('elu'))
model.add(Dense(1, activation='sigmoid'))
adammgm = keras.optimizers.Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0001)
model.compile(loss='mean_squared_error', optimizer=adammgm, metrics=['accuracy'])
print(model.summary())