【问题标题】:TimeDistributed layer with multiple inputs具有多个输入的时间分布层
【发布时间】:2021-04-09 21:29:10
【问题描述】:

我在使用组合 CNN-LSTM 模型正确应用 TimeDistributed 以进行多任务学习时遇到问题。

这是我迄今为止尝试过的一部分:

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,形状为 (293,4),但我得到了这个 enter image description here,形状为 (293,1,4),它只预测最后一类

帮助,我将不胜感激!

【问题讨论】:

    标签: python time-series prediction


    【解决方案1】:

    问题解决了!如果遇到同样的问题,请参考这个链接:Combining CNN and bidirectional LSTM

    【讨论】:

      猜你喜欢
      • 2019-09-17
      • 2019-03-28
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多