为什么CNN-LSTM比LSTM快?

时间:2019-11-18 08:27:24

标签: python keras time-series conv-neural-network lstm

我对加速的原因感到困惑。训练和预测速度方面的收益都是巨大的,超过50倍。

这是我创建LSTM模型的方式:

def create_model(learning_rate, num_LSTM_layers,
                 num_LSTM_nodes, dropout_rate):


    #CREATE THE LSTM NEURAL NETWORK
    model = Sequential()
    if num_LSTM_layers > 1:
        model.add(LSTM(num_LSTM_nodes, return_sequences=True ))
    if num_LSTM_layers == 1:
        model.add(LSTM(num_LSTM_nodes, return_sequences=False))
    model.add(Activation('relu'))
    model.add(Dropout(dropout_rate))

    if num_LSTM_layers > 1:
        for i in range(num_LSTM_layers-1):
            if i+1 == num_LSTM_layers-1:
                model.add(layers.LSTM(num_LSTM_nodes, return_sequences=False))
            else:
                model.add(layers.LSTM(num_LSTM_nodes, return_sequences=True))
            model.add(Activation('relu'))
            model.add(Dropout(dropout_rate))

    model.add(Dense(1))
    model.add(Activation('linear'))


    # Use the Adam method for training the network.
    # We want to find the best learning-rate for the Adam method.
    optimizer = Adam(lr=learning_rate)

    # In Keras we need to compile the model so it can be trained.
    model.compile(loss='mean_squared_error', optimizer=optimizer)

    return model

这就是我创建CNN-LSTM模型的方式:

def create_model_TD(learning_rate, num_conv_layers, num_LSTM_layers,
                 num_LSTM_nodes, dropout_rate, filter_size, kernel_height, pool_size):

    #CREATE THE LSTM NEURAL NETWORK
    model = Sequential()
    model.add(TimeDistributed(Conv1D(input_shape=(None, X_train.shape[2], X_train.shape[3]) , 
                                     filters= int(filter_size), kernel_size= int(kernel_height), activation='relu', padding='causal')))
    if num_conv_layers == 2:
        model.add(TimeDistributed(Conv1D(filters=int(filter_size), kernel_size= int(kernel_height), activation='relu', padding='causal')))
    model.add(TimeDistributed(MaxPooling1D(pool_size=int(pool_size))))
    model.add(TimeDistributed(Flatten()))
    if num_LSTM_layers > 1:
        model.add(LSTM(num_LSTM_nodes, return_sequences=True))
    if num_LSTM_layers == 1:
        model.add(LSTM(num_LSTM_nodes, return_sequences=False))
    model.add(Activation('relu'))
    model.add(Dropout(dropout_rate))

    if num_LSTM_layers > 1:
        for i in range(num_LSTM_layers-1):
            if i+1 == num_LSTM_layers-1:
                model.add(LSTM(num_LSTM_nodes, return_sequences=False))
            else:
                model.add(LSTM(num_LSTM_nodes, return_sequences=True))
            model.add(Activation('relu'))
            model.add(Dropout(dropout_rate))

    model.add(Dense(1))
    model.add(Activation('linear'))


    # Use the Adam method for training the network.
    # We want to find the best learning-rate for the Adam method.
    optimizer = Adam(lr=learning_rate)

    # In Keras we need to compile the model so it can be trained.
    model.compile(loss='mean_squared_error', optimizer=optimizer)

    return model

但是,当我查看可训练参数的数量时,CNN-LSTM似乎比经典LSTM拥有更多的参数。有人知道原因吗?谢谢您的帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

通过集中于关键特征,CNN导致了各种不同的复杂性降低。卷积层的使用导致张量的尺寸减小。另外,使用池化导致进一步减少。最后但并非最不重要的一点是ReLu层降低了复杂性。因为训练时间减少了

在cnn中更深入的初学者外观

https://www.semanticscholar.org/paper/Introduction-to-Convolutional-Neural-Networks-Wu/450ca19932fcef1ca6d0442cbf52fec38fb9d1e5