当前位置: 首页 > news >正文

《d2l Chapter4 多层感知机基础内容》

Chapter 4 : 多层感知机(MLP)


先看看这个原理简单的嵌套式的“双层”的线性回归

\[\mathbf{X} \in \mathbb{R}^{n \times d} \]

表示 \(n\) 个样品且每个样品有 \(d\) 个输入特征的小批量,\(\mathbf{H}\) 为隐藏层变量。
\(\mathbf{W}^{(1)} \in \mathbb{R}^{d \times h}\)\(\mathbf{b}^{(1)} \in \mathbb{R}^{1 \times h}\)\(\mathbf{W}^{(2)} \in \mathbb{R}^{h \times q}\)\(\mathbf{b}^{(2)} \in \mathbb{R}^{1 \times q}\)
有:

\[\begin{split}\begin{aligned}\mathbf{H} & = \mathbf{X} \mathbf{W}^{(1)} + \mathbf{b}^{(1)}, \\\mathbf{O} & = \mathbf{H}\mathbf{W}^{(2)} + \mathbf{b}^{(2)}. \end{aligned}\end{split}\]

然而显然,这并没有什么用处,因为:

\[\mathbf{O} = (\mathbf{X} \mathbf{W}^{(1)} + \mathbf{b}^{(1)})\mathbf{W}^{(2)} + \mathbf{b}^{(2)} = \mathbf{X} \mathbf{W}^{(1)}\mathbf{W}^{(2)} + \mathbf{b}^{(1)} \mathbf{W}^{(2)} + \mathbf{b}^{(2)} = \mathbf{X} \mathbf{W} + \mathbf{b} \]

完全可以被一个 \(\mathbf{X}\) 完美替代,还不会添加性能开销。

为了发挥多层的真正实力用处,我们在仿射变换之后对每个隐藏单元应用非线性的激活函数 \(\sigma\)
我们让

\[\begin{split}\begin{aligned}\mathbf{H} & = \sigma(\mathbf{X} \mathbf{W}^{(1)} + \mathbf{b}^{(1)}), \\\mathbf{O} & = \mathbf{H}\mathbf{W}^{(2)} + \mathbf{b}^{(2)}.\\ \end{aligned}\end{split} \]

激活函数的输出(例如\(\sigma(\cdot)\))被称为活性值
一般来说,有了激活函数,就不可能再将我们的多层感知机退化成线性模型

常见激活函数

\(ReLU\) 函数:

  • \(\operatorname{ReLU}(x) = \max(x, 0)\)
  • \(x=0\) 时认为导数为0
  • 还有变式:\(\operatorname{pReLU}(x) = \max(0, x) + \alpha \min(0, x)\)

\(sigmoid\) 函数:

  • 将输入变换为区间 \((0, 1)\) 上的输出$$\operatorname{sigmoid}(x) = \frac{1}{1 + \exp(-x)}$$
  • 导数如下$$\frac{d}{dx} \operatorname{sigmoid}(x) = \frac{\exp(-x)}{(1 + \exp(-x))^2} = \operatorname{sigmoid}(x)\left(1-\operatorname{sigmoid}(x)\right)$$

\(tanh\) 函数:

  • 双曲正切函数压缩至 \((-1,1)\)

\[\operatorname{tanh}(x) = \frac{1 - \exp(-2x)}{1 + \exp(-2x)}\]

  • 此外,存在关系式:$$\operatorname{tanh}(x) + 1 = 2 \operatorname{sigmoid}(2x)$$

简单模型(调用d2l函数)

import torch
from torch import nn
from d2l import torch as d2l
net = nn.Sequential(nn.Flatten(),nn.Linear(784, 256),nn.ReLU(),nn.Linear(256, 10))def init_weights(m):if type(m) == nn.Linear:nn.init.normal_(m.weight, std=0.01)net.apply(init_weights);
batch_size, lr, num_epochs = 256, 0.1, 10
loss = nn.CrossEntropyLoss(reduction='none')
trainer = torch.optim.SGD(net.parameters(), lr=lr)train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)
http://www.rkmt.cn/news/58203.html

相关文章:

  • 实验3_CPP
  • longchain4j 学习系列(4)-mcp调用
  • Java 学习路线可按「基础→进阶→实战→架构」四阶段推进
  • 读 《d2l:Chapter3. 线性神经网络 》随笔
  • iPadOS16有什么新功能
  • ipad linux
  • 从上下文工程到组织知识管理重构——HRBP在人机协同时代的新使命 - 教程
  • 深入解析:蓝色星球如何打造能与企业共同进化的灵活系统
  • 内网渗透过程的多种代理搭建方法
  • 男女生排列 女生的一定千万别忘了
  • Claude code 使用技巧
  • xyd 2025 NOIP 模拟赛
  • 7段第二课:贪心
  • dropMimeData
  • Terrorform-自动化创建EKS集群
  • Django 学习路线图 - 教程
  • 积极想到二维数组的递推
  • [人工智能-大模型-55]:模型层技能 - AI的算法、数据结构中算法、逻辑处理的算法异同
  • Terrorform-自动化配置AWS EC2
  • Monit-基于非容器服务自恢复程序实践
  • 人工智能之编程进阶 Python高级:第十章 知识点总结
  • 这篇题为《手指沾满白河水:AI元人文的批判与建构》的论文
  • 《沉默的审查:高度原创性理论在预印本平台中的识别困境与范式危机——以“AI元人文”投稿为例》
  • 人工智能之编程进阶 Python高级:第九章 爬虫类模块
  • iOS虚拟现实开发如何降低成本
  • ios是linux
  • ios基于linux还是unix
  • ubuntu22.04 源更新报错 —— all.deb 403 Forbidden [IP: 101.6.15.130 80
  • 完整教程:【Linux入门】常用工具:yum、vim
  • 2025宠物饮水机水泵品牌TOP5推荐,水暖毯水泵、加湿器水泵、冷风扇水泵等微型水泵厂商品质性价比选择指南