惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
人人都是产品经理
人人都是产品经理
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
月光博客
月光博客
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Visual Studio Blog
博客园 - 叶小钗
博客园 - 司徒正美
美团技术团队
博客园_首页
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News

Louis C Deng's Blog

RoPE: Properties, Patterns, and Long-Context Behavior CS336 Assignment 1: Large Language Model Training and Inference CS231n Lecture Note: Generative Models CS231n Lecture Note: Self-Supervised Learning CS231n Lecture Note: Large Scale Distributed Training 自動微分 | DIY 實現自己的 PyTorch From RNNs to Transformers Uncovering Batch & Layer Normalization CS231n Lecture Note VI: CNN Architectures and Training CS231n Lecture Note V: Convolution Neural Networks Basics Demystifying Softmax Loss: A Step-by-Step Derivation for Linear Classifiers Backpropagation: A Vector Calculus Perspective CS231n Lecture Note IV: Neural Networks and Backpropagation CS231n Lecture Note III: Optimization CS231n Lecture Note II: Linear Classifiers CS231n Lecture Note I: Image Classification CSAPP Cache Lab II: Optimizing Matrix Transposition CSAPP Cache Lab I: Let's simulate a cache memory! CS188 Search Lecture Notes III CS188 Search Lecture Notes II How to Use TouchID for Sudo Commands on macOS CS188 Search Lecture Notes I RECAP2025: 留白 CSAPP Bomb Lab 解析 x64 暫存器速查表 CSAPP Data Lab 解析 矩陣的 Modified Gram Schmidt 方法 聊一聊位掩碼(Bit Mask) 整數溢位與未定義行為 快速排序 幾種劃分方法討論
CS231n Lecture Note VII: Recurrent Neural Networks
Louis C Deng · 2026-04-07 · via Louis C Deng's Blog

Recurrent Neural Networks (RNNs) are a class of neural networks designed to handle sequential data. Unlike standard feedforward networks, RNNs maintain an internal state (memory) that is updated as each element of a sequence is processed, allowing information to persist across time steps.

Sequence Architectures

RNNs are flexible and can be adapted to various input-output mapping structures depending on the task:

  • One-to-Many: A single input produces a sequence of outputs.
    • Example: Image Captioning (Input: Image →\rightarrow Output: Sequence of words).
  • Many-to-One: A sequence of inputs produces a single output.
    • Example: Action Prediction or Sentiment Analysis (Input: Sequence of video frames/words →\rightarrow Output: Label).
  • Many-to-Many: A sequence of inputs produces a sequence of outputs.
    • Example: Video Captioning (Input: Sequence of frames →\rightarrow Output: Sequence of words) or Video Classification (Frame-by-frame labeling).

The Recurrence Mechanism

The “key idea” behind an RNN is the use of a recurrence formula applied at every time step (tt). This allows the network to process a sequence of vectors xx by maintaining a hidden state hh.

A. Hidden State Update

The hidden state is updated by combining the previous state with the current input.

ht=fW(ht−1,xt)h_t = f_W(h_{t-1}, x_t)

Parameters:

  • hth_t: New state (current hidden state).
  • ht−1h_{t-1}: Old state (hidden state from the previous time step).
  • xtx_t: Input vector at the current time step.
  • fWf_W: A function (often a non-linear activation like tanhtanh or ReLUReLU) with trainable parameters WW.

Note: the same function and the same set of parameters are used at every time step.

B. Output Generation

After the hidden state is updated, the network can produce an output at that specific time step.

yt=fWhy(ht)y_t = f_{W_{hy}}(h_t)

Parameters:

  • yty_t: Output at time tt.
  • hth_t: New state (the updated hidden state).
  • fWhyf_{W_{hy}}: A separate function with its own trainable parameters WhyW_{hy} used to map the hidden state to the output space.

Backpropagation Through Time

Optimizing an RNN requires a specialized version of gradient descent known as Backpropagation Through Time (BPTT).

In its standard form, the model performs a complete forward pass through the entire sequence to calculate a global loss. During the subsequent backward pass, gradients are propagated from the final loss all the way back to the first time step.

While this captures long-range dependencies accurately, it is often computationally prohibitive for long sequences due to the massive memory requirements for storing intermediate states.

To mitigate these resource constraints, researchers often employ Truncated BPTT. This technique involves partitioning the sequence into smaller, manageable chunks.

The model performs a forward and backward pass on a specific chunk to update its weights before moving to the next. Crucially, while the hidden state is carried forward into the next chunk to maintain continuity, the gradient flow is “truncated” at the chunk boundary.

This approximation significantly reduces memory overhead and allows for the training of models on extended temporal data without sacrificing the benefits of sequential learning.

RNN Tradeoffs

RNNs can process input sequences of arbitrary length, and their model size remains constant regardless of input length since the same weights are shared across all time steps—ensuring temporal symmetry in how inputs are processed. In principle, they can leverage information from arbitrarily distant time steps.

However, in practice, recurrent computation tends to be slow due to its sequential nature, and capturing long-range dependencies remains challenging because relevant information from many steps back often becomes inaccessible or diluted over time.

Long Short Term Memory

Long Short Term Memory (LSTMs) are used to alleviate vanishing or exploding gradients when training long sequences by using a gated architecture that regulates the flow of information.

LSTM Mathematics

The operations of an LSTM cell at time step tt are defined by three primary equations:

1. The Gate Vector
LSTMs compute four internal vectors (gates and candidates) simultaneously by concatenating the previous hidden state ht−1h_{t-1} and the current input xtx_t:

(ifog)=(σσσtanh⁡)W(ht−1xt)\begin{pmatrix} i \\ f \\ o \\ g \end{pmatrix} = \begin{pmatrix} \sigma \\ \sigma \\ \sigma \\ \tanh \end{pmatrix} W \begin{pmatrix} h_{t-1} \\ x_t \end{pmatrix}

  • ii (Input Gate): Decides which new information to store in the cell state.
  • ff (Forget Gate): Decides which information to discard from the previous state.
  • oo (Output Gate): Decides which part of the cell state to output.
  • gg (Cell Candidate): Creates a vector of new candidate values (using tanh⁡\tanh) to be added to the state.

2. The Cell State (ctc_t)
This is the “long-term memory” of the network. It is updated via a linear combination of the old state and the new candidates:

ct=f⊙ct−1+i⊙gc_t = f \odot c_{t-1} + i \odot g

The use of the Hadamard product (⊙\odot, element-wise multiplication) allows the forget gate to “zero out” specific memories while the input gate adds new ones. This additive update is the secret to preventing vanishing gradients.

3. The Hidden State (hth_t)
The hidden state is the “working memory” passed to the next cell and the higher layers. It is a filtered version of the cell state:

ht=o⊙tanh⁡(ct)h_t = o \odot \tanh(c_t)