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

推荐订阅源

S
SegmentFault 最新的问题
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
Jina AI
Jina AI
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
V
V2EX
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
B
Blog
博客园 - 叶小钗
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
A
About on SuperTechFans
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog

博客园 - xiezhengcai

日志分层设计 Ai阅读代码的局限性 业务系统缓存加速的五种缓存设计 The CustomResourceDefinition "applications.apps.abc.com" is invalid: metadata.annotations: Too long: must have at most 262144 bytes vscode ssh开发无法读取go env配置 deepspeed ray + nccl + 张量并行 训练 ray集群 ray 分布式, 工作节点、reylet、 主节点、GCS ray分布式 nccl & mpi 跨节点通信 cudaMemsetParams 参数详细 cuda 中的内存拷贝 cuda 编程 cuda vllm 参数分类 张量计算流程图标注方法 torch.view、unsqueeze、reshape、transpose 和 permute PyTorch交叉熵损失函数详解
为什么nn.Linear 的weight 是 (out_features, in_features)
xiezhengcai · 2025-03-26 · via 博客园 - xiezhengcai

在PyTorch的nn.Linear中,权重矩阵的形状为(out_features, in_features)。这是因为线性变换的实现方式为:

具体来说:

  1. 当创建nn.Linear(10, 60)时,in_features=10out_features=60,因此权重的形状是(60, 10)
  2. 输入张量t的形状为(2, 5, 10),与转置后的权重a.weight.T(形状(10, 60))相乘时,实际计算为:
    [
    t \in \mathbb{R}^{2 \times 5 \times 10}, \quad a.weight^\top \in \mathbb{R}^{10 \times 60} \implies t \ @ \ a.weight^\top \in \mathbb{R}^{2 \times 5 \times 60}
    ]
    这与直接调用a(t)的结果一致。

因此,a.weight的shape是(60, 10),而非(10, 60),这是PyTorch的设计约定,确保矩阵乘法能正确匹配输入和输出的维度。