简体   繁体   English

使用 Keras 训练 CNN-LSTM 时卡在第一个 epoch

[英]Stuck in the first epoch when training the CNN-LSTM using Keras

I am using Keras to construct a CNN-LSTM model for tweet classification.我正在使用 Keras 构建用于推文分类的 CNN-LSTM 模型。 The model has two inputs and the task is a three-class classification.该模型有两个输入,任务是三类分类。 The code I use to construct the model is given below:我用来构建模型的代码如下:

def conv2d_lstm_with_author():

    # Get the input information - author & tweet
    author_repre_input = Input(shape=(100,), name='author_input')
    tweet_input = Input(shape=(13, 100, 1), name='tweet_input')

    # Create the convolutional layer and lstm layer
    conv2d = Conv2D(filters = 200, kernel_size = (2, 100), padding='same', activation='relu', 
                    use_bias=True, name='conv_1')(tweet_input)
    flat = Flatten(name='flatten_1')(conv2d)
    reshape_flat = Reshape((260000, 1), name='reshape_1')(flat)
    lstm = LSTM(100, return_state=False, activation='tanh', recurrent_activation='hard_sigmoid', name='lstm_1')(reshape_flat)
    concatenate_layer = concatenate([lstm, author_repre_input], axis=1, name='concat_1')
    dense_1 = Dense(10, activation='relu', name='dense_1')(concatenate_layer)
    output = Dense(3, activation='softmax', kernel_regularizer=regularizers.l2(0.01), name='output_dense')(dense_1)

    # Build the model
    model = Model(inputs=[author_repre_input, tweet_input], outputs=output)
    return model

model = conv2d_lstm_with_author()
model.summary()

optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])

The shape of my two inputs and label are:我的两个输入和标签的形状是:

author_repre_input: (40942, 100)
tweet_input: (40942, 13, 100, 1)
my label Train_Y: (40942, 3)

A snapshot of the model summary is:模型摘要的快照是:

在此处输入图片说明

When I use the following code to train the data:当我使用以下代码训练数据时:

model.fit([author_repre_input, tweet_input], [Train_Y], epochs=20, batch_size=32, validation_split=0.2, 
          shuffle=False, verbose=2)

The result keeps stucking in the first epoch and the log does not show anything useful, just:结果一直停留在第一个纪元,日志没有显示任何有用的信息,只是:

Epoch 1/20

I am wondering why this happens.我想知道为什么会发生这种情况。 The version of tensorflow and keras I am using is:我使用的 tensorflow 和 keras 版本是:

tensorflow - 1.14.0
keras - 2.2.0

Thank you very much for your time!非常感谢您的宝贵时间!


Update on Jan 20... 1 月 20 日更新...

I try to use Google Colab to train the model.我尝试使用 Google Colab 来训练模型。 I check the RAM when running the model.我在运行模型时检查 RAM。 The Colab allocate 25G RAM for me. Colab 为我分配了 25G RAM。 However, after several seconds of training, the session crashed due to ocupying all available RAM...但是,经过几秒钟的训练后,由于占用了所有可用 RAM,会话崩溃了......

在此处输入图片说明

I think there must be something wrong with the model part...Any suggestions and insights would be appreciated!我认为模型部分一定有问题......任何建议和见解将不胜感激!

Fortunately for you, you are not stuck.对你来说幸运的是,你没有被卡住。

The issue comes from the fact that in your model.fit , you specified the parameter verbose=2 .问题来自这样一个事实,即在您的model.fit ,您指定了参数verbose=2

This means that your code will only output messages at the end of an epoch, not informative ones during training progress.这意味着您的代码只会在一个 epoch 结束时输出消息,而不会在训练过程中输出信息。

To solve your problem and see training progress, set verbose=1 .要解决您的问题并查看训练进度,请设置verbose=1

I think I have found the answer...我想我已经找到了答案......

The problem is in the convolutional layer.问题出在卷积层。 The kernel size is too small, which causes the dimensionality of the output layer is too high.内核尺寸太小,导致输出层的维数太高。 To solve this problem, I change the kernel size from (2, 100) to (3, 100) .为了解决这个问题,我将内核大小从(2, 100)更改为(3, 100) Furthermore, I also add dropout to my model.此外,我还在我的模型中添加了 dropout。 The summary of the model I use now is given below:我现在使用的模型总结如下:

在此处输入图片说明

Now the model could run smoothly in the Google Colab.现在该模型可以在 Google Colab 中顺利运行。

Hence , I think if a similar problem occurs, please check the output dimension of each layer.因此,我认为如果出现类似问题,请检查每一层的输出维度。 The Keras API may stop in the training epochs if the model creates a very high dimensional output.如果模型创建了非常高维的输出,Keras API 可能会在训练阶段停止。

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

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