Skip to content

    RNN & LSTM

    AI · Advanced

    Definition

    RNNs process sequences step by step with a hidden state; LSTMs add gates to remember long-term information.

    Used for time series, speech and early NLP.

    Diagram

    x1 → h1x2 → h2x3 → h3

    Flowchart

    Read token tCombine with hidden stateUpdate state (gates in LSTM)Output and move to t+1

    Example: Simple running state

    h = 0
    for x in [1, 2, 3]:
        h = 0.5 * h + x
    print(h)

    Output: 4.25

    Complexity

    O(T) sequential steps

    Key points

    • LSTM gates: forget, input, output
    • Transformers have largely replaced RNNs

    Interview questions

    Why LSTM over RNN?

    Its gates reduce vanishing gradients and keep long-range memory.

    More AI topics