【AI系列】从零开始学习大模型GPT (3)- Build a Large Language Model (From Scratch)

Build a Large Language Model

背景

第1章:理解大型语言模型

见前序文章【AI系列】从零开始学习大模型GPT (1)- Build a Large Language Model (From Scratch)

第2章:处理文本数据

见前序文章【AI系列】从零开始学习大模型GPT (1)- Build a Large Language Model (From Scratch)

第3章:编码Attention机制

见前序文章【AI系列】从零开始学习大模型GPT (1)- Build a Large Language Model (From Scratch)

第4章:从零实现GPT模型

基础架构

多头注意力模块

python 复制代码
import torch
import torch.nn as nn

class MultiHeadAttention(nn.Module):
    def __init__(self, d_model, num_heads):
        super().__init__()
        self.d_model = d_model
        self.num_heads = num_heads
        self.head_dim = d_model // num_heads
        
        self.wq = nn.Linear(d_model, d_model)
        self.wk = nn.Linear(d_model, d_model)
        self.wv = nn.Linear(d_model, d_model)
        self.dense = nn.Linear(d_model, d_model)

    def forward(self, q, k, v, mask=None):
        batch_size = q.size(0)
        
        # 线性投影并分头
        q = self.wq(q).view(batch_size, -1, self.num_heads, self.head_dim).transpose(1, 2)
        k = self.wk(k).view(batch_size, -1, self.num_heads, self.head_dim).transpose(1, 2)
        v = self.wv(v).view(batch_size, -1, self.num_heads, self.head_dim).transpose(1, 2)
        
        # 缩放点积注意力
        scores = torch.matmul(q, k.transpose(-2, -1)) / torch.sqrt(torch.tensor(self.head_dim, dtype=torch.float32))
        if mask is not None:
            scores += mask * -1e9
        attention = torch.softmax(scores, dim=-1)
        output = torch.matmul(attention, v)
        
        # 合并多头
        output = output.transpose(1, 2).contiguous().view(batch_size, -1, self.d_model)
        return self.dense(output)

前馈网络模块

python 复制代码
class FeedForward(nn.Module):
    def __init__(self, d_model, d_ff):
        super().__init__()
        self.linear1 = nn.Linear(d_model, d_ff)
        self.linear2 = nn.Linear(d_ff, d_model)
        self.relu = nn.ReLU()

    def forward(self, x):
        return self.linear2(self.relu(self.linear1(x)))

Transformer解码层

python 复制代码
class DecoderLayer(nn.Module):
    def __init__(self, d_model, num_heads, d_ff, dropout):
        super().__init__()
        self.mha = MultiHeadAttention(d_model, num_heads)
        self.ffn = FeedForward(d_model, d_ff)
        self.layernorm1 = nn.LayerNorm(d_model)
        self.layernorm2 = nn.LayerNorm(d_model)
        self.dropout = nn.Dropout(dropout)

    def forward(self, x, mask):
        attn_output = self.mha(x, x, x, mask)
        x = self.layernorm1(x + self.dropout(attn_output))
        ffn_output = self.ffn(x)
        return self.layernorm2(x + self.dropout(ffn_output))

位置编码

python 复制代码
class PositionalEncoding(nn.Module):
    def __init__(self, d_model, max_len=512):
        super().__init__()
        position = torch.arange(max_len).unsqueeze(1)
        div_term = torch.exp(torch.arange(0, d_model, 2) * (-math.log(10000.0) / d_model))
        pe = torch.zeros(max_len, d_model)
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        self.register_buffer('pe', pe)

    def forward(self, x):
        return x + self.pe[:x.size(1)]

以上代码展示了GPT核心组件的实现逻辑。实际完整实现还需包含:

  • 词嵌入层
  • 多层解码器堆叠
  • 自回归掩码生成
  • 输出投影层

完整开源实现可参考HuggingFace的transformers库或OpenAI官方发布的代码。

代码验证

第5章:在未标记数据上进行预训练

关注我,待更新

第6章:用于文本分类的微调

关注我,待更新

第7章:为指令执行进行微调

关注我,待更新

参考

代码汇总 github

相关推荐
dankokoko4 分钟前
一.函数与极限2
学习
颜酱5 分钟前
03 | 实现节点1 — 抽取关键词
前端·人工智能·后端
知识分享小能手9 分钟前
统计学学习教程,从入门到精通,分类数据分析 — 知识点详解(14)
学习·分类·数据分析
灵机一物31 分钟前
企业选型参考:2026 CXL 内存扩展方案六强测评与落地指南
服务器·网络·数据库·人工智能
Web极客码31 分钟前
如何用三段式确定性剪枝,为 LLM Agent 砍掉 35% 的 Token 成本?
服务器·人工智能·算法·机器学习
菜鸟‍42 分钟前
【论文学习】MICCAI 2025 || ARSeg:面向不完整文本提示的鲁棒医学图像指代表达分割
图像处理·学习
三克的油1 小时前
c++算法学习-4
学习
wtsolutions1 小时前
Sheet-to-Doc Skill: Let AI Assistants Generate Word Documents for You
人工智能·word
橘子星1 小时前
🧠 一文讲透 AI Workflow 与 Agent 的区别:别再把这俩概念搞混了!
人工智能