Manel Mili
Manel Mili

Reputation: 21

TimeDistributed layer with multiple inputs

I have an issue on applying TimeDistributed correctly with a combined CNN-LSTM model for a multi-task learning.

Here is a part of what I have tried so far:

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())

I'm supposed to have this as an output matrix enter image description here, with shape (293,4), except i got this enter image description here, with shape (293,1,4), that predict only the last class

help, I would be very grateful!

Upvotes: 1

Views: 52

Answers (1)

Manel Mili
Manel Mili

Reputation: 21

Problem solved ! If you encounter the same problem, please refer to this link: Combining CNN and bidirectional LSTM

Upvotes: 1

Related Questions