torch.nn.Sequential —— 把多个层串成一条流水线 torch.nn.Sequential —— 把多个层串成一条流水线一句话把多个层按顺序装进一个容器数据依次流经每个层省去手动写中间变量。基本用法importtorch.nnasnn三个层串起来modelnn.Sequential(nn.Linear(10,20),# 层1nn.ReLU(),# 层2nn.Linear(20,1),# 层3)xtorch.randn(5,10)# 输入 (5, 10)outmodel(x)# 自动依次经过 层1→层2→层3print(out.shape)# (5, 1)等价于手写的 forward用 Sequential简洁modelnn.Sequential(nn.Linear(10,20),nn.ReLU(),nn.Linear(20,1),)outmodel(x)等价于手写啰嗦但看得清classMyModel(nn.Module):def__init__(self):super().__init__()self.fc1nn.Linear(10,20)self.relunn.ReLU()self.fc2nn.Linear(20,1)defforward(self,x):xself.fc1(x)xself.relu(x)xself.fc2(x)returnx modelMyModel()outmodel(x)数据流动图输入 x (5, 10)│▼ nn.Linear(10, 20)(5, 20)│▼ nn.ReLU()(5, 20)│▼ nn.Linear(20, 1)(5, 1) ← 输出你在 gpt.py 里见过的用法Ch04 的 FeedForward 类classFeedForward(nn.Module):def__init__(self,cfg):super().__init__()self.layersnn.Sequential(nn.Linear(cfg[emb_dim],4*cfg[emb_dim]),# 扩大4倍GELU(),# 激活nn.Linear(4*cfg[emb_dim],cfg[emb_dim]),# 缩回原大小)defforward(self,x):returnself.layers(x)# 数据依次流经这三个层Ch04 的 GPTModel 类self.trf_blocksnn.Sequential(*[TransformerBlock(cfg)for_inrange(cfg[n_layers])])展开后 12 个 TransformerBlock 串起来数据依次经过 block1 → block2 → … → block12三种常用写法① 直接列出来modelnn.Sequential(nn.Linear(3,4),nn.ReLU(),nn.Linear(4,1))② 用列表 * 解包适合动态创建多个相同层layers[nn.Linear(3,4)for_inrange(5)]modelnn.Sequential(*layers)③ 用 OrderedDict 给每层命名方便调试fromcollectionsimportOrderedDict modelnn.Sequential(OrderedDict([(fc1,nn.Linear(3,4)),(relu,nn.ReLU()),(fc2,nn.Linear(4,1)),]))什么情况不用 Sequential当你需要分支、跳过、多次使用同一个层、或层之间需要复杂逻辑时就要手写 forward❌ 这种复杂结构 Sequential 做不了classTransformerBlock(nn.Module):defforward(self,x):shortcutx# 存一份xself.norm1(x)xself.att(x)xxshortcut# 残差连接要跳回去加returnx速记特性 说明作用 把多个层串成一条流水线输入 层的列表顺序执行输出 数据依次流经所有层后的结果优点 简洁、自动处理 forward局限 只能顺序执行不能分支/残差/复用

相关新闻

最新新闻

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现 【免费下载链接】serenity The Serenity Operating System 🐞 项目地址: https://gitcode.com/GitHub_Trending/se/serenity 导读 本文以 getopt(3) 手册 为核心&a…

2026/10/3 16:42:15
轻量服务器还是ECS?大促云服务器选购与避坑实战指南

轻量服务器还是ECS?大促云服务器选购与避坑实战指南

每年大促节点,群里永远有人在问同一个问题:“38元的轻量服务器到底怎么抢?为什么我每次点进去都是已售罄?68元直购和99元的ECS我到底选哪个?”作为一个常年帮团队和自己采购云服务器的老用户,我太清楚这种纠…

2026/10/3 16:42:30
为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南 【免费下载链接】agents Multi-harness agentic plugin marketplace for Claude Code, Codex, Cursor, OpenCode, GitHub Copilot, and Google Antigravity 项目地址:…

2026/10/3 16:42:22
PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署 【免费下载链接】PaddleOCR Turn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between i…

2026/10/4 7:45:19
Spring源码解析:构造器注入的类型转换与候选匹配机制

Spring源码解析:构造器注入的类型转换与候选匹配机制

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/3 16:42:24
openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由 【免费下载链接】openai-agents-python A lightweight, powerful framework for multi-agent workflows 项目地址: https://gitcode.com/GitHub_Trending/op/openai-agents-pyth…

2026/10/3 16:42:28

日新闻

周新闻

月新闻