Keras: 怎样使用LSTM

本文深入解析LSTM(长短期记忆网络)在Keras中的应用,详细解释了input_shape、input_dim和input_length等参数的意义及如何正确配置这些参数以实现序列预测任务。通过实例展示了如何搭建LSTM模型进行序列学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LSTM(10, input_length=shape[1], input_dim=shape[2],return_sequences=True)

model.add(LSTM(32, input_shape=(10, 64)))

这里的 input_length, input_dim , input_shape是什么意思?怎样使用LSTM?

通过help(LSTM)查看

class LSTM(RNN)
 |  Long Short-Term Memory layer - Hochreiter 1997.
 |  
 |  # Arguments
 |      units: Positive integer, dimensionality of the output space.
 |      activation: Activation function to use
 |          (see [activations](../activations.md)).
 |          Default: hyperbolic tangent (`tanh`).
 |          If you pass `None`, no activation is applied
 |          (ie. "linear" activation: `a(x) = x`).

在Arguments中并没有上面出现的input_length, input_dim, input_shape的描述

到keras的源码路径:

/usr/local/lib/python2.7/dist-packages/keras

 grep -nrw LSTM ./

./legacy/layers.py:315:    Use its children classes `LSTM`, `GRU` and `SimpleRNN` instead.
./legacy/layers.py:316:    All recurrent layers (`LSTM`, `GRU`, `SimpleRNN`) also
./legacy/layers.py:325:        model.add(LSTM(32, input_shape=(10, 64)))
./legacy/layers.py:329:        model.add(LSTM(16))
./legacy/layers.py:334:        model.add(LSTM(64, input_dim=64, input_length=10, return_sequences=True))
./legacy/layers.py:335:        model.add(LSTM(32, return_sequences=True))
./legacy/layers.py:336:        model.add(LSTM(10))
 

class LSTM(RNN)

基类RNN

class Recurrent(Layer):
    """Abstract base class for recurrent layers.

    Do not use in a model -- it's not a valid layer!
    Use its children classes `LSTM`, `GRU` and `SimpleRNN` instead.
    All recurrent layers (`LSTM`, `GRU`, `SimpleRNN`) also
    follow the specifications of this class and accept
    the keyword arguments listed below.

    # Example

    ```python
        # as the first layer in a Sequential model
        model = Sequential()
        model.add(LSTM(32, input_shape=(10, 64))) //input_shape
        # now model.output_shape == (None, 32) //output_shape
        # note: `None` is the batch dimension.
        # for subsequent layers, no need to specify the input size:
        model.add(LSTM(16))
        # to stack recurrent layers, you must use return_sequences=True
        # on any recurrent layer that feeds into another recurrent layer.
        # note that you only need to specify the input size on the first layer.
        model = Sequential()
        model.add(LSTM(64, input_dim=64, input_length=10, return_sequences=True))
        model.add(LSTM(32, return_sequences=True))
        model.add(LSTM(10))
    ```
    # Arguments
        return_sequences: Boolean. Whether to return the last output
            in the output sequence
, or the full sequence.

        input_dim: dimensionality of the input (integer).
            This argument (or alternatively, the keyword argument `input_shape`)
            is required when using this layer as the first layer in a model.

        input_length: Length of input sequences, to be specified
            when it is constant.
            This argument is required if you are going to connect
            `Flatten` then `Dense` layers upstream
            (without it, the shape of the dense outputs cannot be computed).
            Note that if the recurrent layer is not the first layer
            in your model, you would need to specify the input length
            at the level of the first layer
            (e.g. via the `input_shape` argument)


    # Input shapes
        3D tensor with shape `(batch_size, timesteps, input_dim)`,
        (Optional) 2D tensors with shape `(batch_size, output_dim)`.

    # Output shape
        - if `return_state`: a list of tensors. The first tensor is
            the output. The remaining tensors are the last states,
            each with shape `(batch_size, units)`.
        - if `return_sequences`: 3D tensor with shape
            `(batch_size, timesteps, units)`.
        - else, 2D tensor with shape `(batch_size, units)`.

class LSTM(RNN): units


    """Long Short-Term Memory layer - Hochreiter 1997.

    # Arguments
        units: Positive integer, dimensionality of the output space.

相关链接

https://towardsdatascience.com/understanding-lstm-and-its-quick-implementation-in-keras-for-sentiment-analysis-af410fd85b47

https://adventuresinmachinelearning.com/keras-lstm-tutorial/

https://medium.com/@daniel820710/%E5%88%A9%E7%94%A8keras%E5%BB%BA%E6%A7%8Blstm%E6%A8%A1%E5%9E%8B-%E4%BB%A5stock-prediction-%E7%82%BA%E4%BE%8B-1-67456e0a0b

[Keras] 利用Keras建構LSTM模型,以Stock Prediction 為例 (数据预处理的过程)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值