简体   繁体   English

减少LSTM keras的输出尺寸

[英]Reduce output dimensions on LSTM keras

Below is my architecture of the model. 下面是我的模型架构。 The data is a time series which I need to predict only the last value, hence return_sequences=False . 数据是一个时间序列,我只需要预测最后一个值,因此return_sequences=False

But this is exactly what creates the problem here. 但这正是在这里造成问题的原因。 I have been able to run the nnet using sequences=True , but it is not what I need to do. 我已经能够使用sequences=True运行nnet,但这不是我需要做的。

I need an input size (32,50,88) 我需要输入大小(32,50,88) =(batch_size,timesteps,features) and get output size of (32,88) =(batch_size,timesteps,features)并获得(32,88)的输出大小 =(batch_size,labels) . =(batch_size,labels)

Features and labels have the same size, but it is irrelevant. 特征和标签具有相同的大小,但无关紧要。

The error out of this code is: 此代码中的错误是:

ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (32, 50, 88) ValueError:检查目标时出错:预期density_1具有2维,但数组的形状为(32,50,88)

which is happening in the training phase (meaning, the architecture comes to be valid). 这是在培训阶段发生的(意思是该架构有效)。

The data comes in chunks of (32,50,88) from a generator, also the labels have the same size. 数据从生成器中以(32,50,88)的块形式输入,标签也具有相同的大小。 Since I use keras , I need to create the batches through the generator. 由于我使用keras ,因此需要通过生成器创建批次。 I have tried to add a single (50,88) but simply it doesn't work. 我试图添加一个(50,88),但根本不起作用。

How could I have this kind of architecture, get the input of (32,50,88) but only get (32,88) as output? 我怎么有这种架构,获得(32,50,88)的输入,但仅获得(32,88)作为输出?

In short, I need the timestep+50 prediction...I think.. 简而言之,我需要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)

Your model is correct, the issues is when checking the target which means that your training_generator is returning wrong target shapes. 您的模型是正确的,问题出在检查目标时 ,这意味着您的training_generator返回了错误的目标形状。

Have a look at print(next(training_generator())) and ensure that it returns a tuple with shapes (32, 50, 88), (32, 88) . 看一下print(next(training_generator()))并确保它返回形状为(32, 50, 88), (32, 88)的元组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM