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

推荐订阅源

美团技术团队
IT之家
IT之家
博客园 - Franky
博客园_首页
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
量子位
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
M
MIT News - Artificial intelligence
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
Jina AI
Jina AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed

博客园 - 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的设计约定,确保矩阵乘法能正确匹配输入和输出的维度。