具有多个输入的 TimeDistributed 层

TimeDistributed layer with multiple inputs

我在将 TimeDistributed 与 CNN-LSTM 组合模型正确应用于多任务学习时遇到问题。

这是我目前尝试过的部分内容:

def create_model(X_trainn, onehot_encoded_train, X_vall, onehot_encoded_vall):
input_shape = (X_trainn.shape[1], X_trainn.shape[2], X_trainn.shape[3])

model.add(Conv2D(filters=(3), kernel_size=(ks1_first, ks1_second), input_shape=input_shape, 
                    padding='same', kernel_initializer='TruncatedNormal'
                    ))
model.add(LeakyReLU())
model.add(Dropout(0.025))

model.add(TimeDistributed(Flatten()))

model.add(LSTM(64, return_sequences=True, bias_regularizer=l1_l2(l1=0.03, l2=0.05))) 
model.add(Dropout(0.2))
model.add(Dense(64))

return model

model = create_model(X_trainn,  onehot_encoded_train, X_vall, onehot_encoded_val)

# 0.05 0.9 0 True
sgd = SGD(lr=0.5, momentum=0.9, decay=0, nesterov=True) # sgd in general yields better results, but needs a lot of tweeking and is slower
adam = Adam(lr=lr)
nadam = Nadam(lr=lr)

# compile & fit
model.compile(optimizer='nadam', loss = ['mse'], metrics=['mse'])

early_stopping_monitor = callbacks.EarlyStopping(monitor ="val_loss",  
                                        mode ="min", patience = 20,  
                                        restore_best_weights = True) 
early_stopping_monitor = EarlyStopping(patience=5000)

filepath="models\CNN.best.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')

epoch_size = 56
start_time = time.time()
model.fit(X_trainn, onehot_encoded_train, epochs=epochs, batch_size=bs, validation_split=0.2,
         verbose=1, callbacks=[early_stopping_monitor, checkpoint])
print("--- %s seconds ---" % (time.time() - start_time))

print(model.summary())

我应该将其作为输出矩阵 enter image description here, with shape (293,4), except i got this enter image description here,形状为 (293,1,4),仅预测最后一个 class

求助,不胜感激!

问题已解决!如果你遇到同样的问题,请参考这个link: