[英]Reduce output dimensions on LSTM keras
下面是我的模型架構。 數據是一個時間序列,我只需要預測最后一個值,因此return_sequences=False
。
但這正是在這里造成問題的原因。 我已經能夠使用sequences=True
運行nnet,但這不是我需要做的。
我需要輸入大小(32,50,88) =(batch_size,timesteps,features)並獲得(32,88)的輸出大小 =(batch_size,labels)
特征和標簽具有相同的大小,但無關緊要。
此代碼中的錯誤是:
ValueError:檢查目標時出錯:預期density_1具有2維,但數組的形狀為(32,50,88)
這是在培訓階段發生的(意思是該架構有效)。
數據從生成器中以(32,50,88)的塊形式輸入,標簽也具有相同的大小。 由於我使用keras
,因此需要通過生成器創建批次。 我試圖添加一個(50,88),但根本不起作用。
我怎么有這種架構,獲得(32,50,88)的輸入,但僅獲得(32,88)作為輸出?
簡而言之,我需要timestep + 50預測...我認為..
def note_model():
visible = Input(shape=(50,88), batch_shape=(32,50,88))
hidden1 = Bidirectional(LSTM(200, stateful=False, return_sequences=False, kernel_regularizer=l1(10**(-4)), dropout=0.5))(visible)
#flat = Flatten()(hidden1)
output = Dense(88, activation='sigmoid')(hidden1)
model = Model(inputs=visible, outputs=output)
print(model.summary())
return model
def train_note_model(model):
checkpoint_path_notes = "1Layer-200units-loss=BCE-Model-{epoch:02d}-{val_acc:.2f}.hdf5"
model.compile(optimizer='SGD', loss='binary_crossentropy', metrics=['accuracy']) #mean_squared_error
monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=10, verbose=0, mode='min')
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.3, patience=10, min_lr=0.001)
checkpoint = ModelCheckpoint(checkpoint_path_notes,monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)
model.fit_generator(training_generator(), steps_per_epoch=2,
callbacks=[monitor, reduce_lr, checkpoint],
validation_data= validation_generator(), validation_steps= 2,
verbose=1, epochs=10, shuffle=True)
model_try = note_model()
train_note_model(model_try)
您的模型是正確的,問題出在檢查目標時 ,這意味着您的training_generator
返回了錯誤的目標形狀。
看一下print(next(training_generator()))
並確保它返回形狀為(32, 50, 88), (32, 88)
的元組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.