Pytorch Added

This commit is contained in:
e2hang
2025-09-09 15:10:57 +08:00
parent a74884a555
commit a5fdeaf70e
20 changed files with 1307 additions and 2 deletions

464
Pytorch/nn/nn.Module.md Normal file
View File

@@ -0,0 +1,464 @@
`torch.nn.Module` 是 PyTorch 中构建神经网络的核心基类,所有的神经网络模型都继承自它。它提供了许多方法来管理模块、参数、子模块和前向传播等功能。以下基于 PyTorch 官方文档https://pytorch.org/docs/stable/generated/torch.nn.Module.html`torch.nn.Module` 的所有方法进行详细讲解,力求清晰、简洁且实用。
---
### 1. `__init__` 方法
- **描述**: 初始化方法,用于定义模块的结构(如层、子模块等)。
- **用法**: 在自定义模块时,重写 `__init__` 来定义子模块或参数。
- **示例**:
```python
import torch.nn as nn
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.fc1 = nn.Linear(10, 5)
```
- **说明**: 调用 `super().__init__()` 确保基类的初始化,子模块需注册到模块中(通过赋值给 `self`)。
---
### 2. `forward(*args, **kwargs)`
- **描述**: 定义模块的前向传播逻辑,子类必须实现此方法。
- **用法**: 输入数据通过此方法计算输出。
- **示例**:
```python
def forward(self, x):
x = self.fc1(x)
return x
```
- **说明**: 这是核心方法,所有输入数据的计算流程在此定义。调用模型实例(如 `model(x)`)时会自动调用 `forward`。
---
### 3. `__call__` 方法
- **描述**: 使模块实例可调用,内部调用 `forward` 方法并添加钩子hook功能。
- **用法**: 不需要显式重写,用户通过 `model(input)` 间接调用。
- **说明**: 通常不需要直接操作,但了解它是 `model(input)` 的实现基础。
---
### 4. `add_module(name: str, module: Module) -> None`
- **描述**: 向模块添加一个子模块,并以指定名称注册。
- **用法**: 动态添加子模块。
- **示例**:
```python
model = nn.Module()
model.add_module("fc1", nn.Linear(10, 5))
```
- **说明**: 等价于 `self.name = module`,但更灵活,适合动态构建模型。
---
### 5. `apply(fn: Callable[['Module'], None]) -> T`
- **描述**: 递归地将函数 `fn` 应用到模块及其所有子模块。
- **用法**: 用于初始化参数或修改模块属性。
- **示例**:
```python
def init_weights(m):
if isinstance(m, nn.Linear):
nn.init.xavier_uniform_(m.weight)
model.apply(init_weights)
```
- **说明**: 常用于自定义权重初始化。
---
### 6. `buffers(recurse: bool = True) -> Iterator[Tensor]`
- **描述**: 返回模块中所有缓冲区(如 `running_mean` 或 `running_var`)的迭代器。
- **用法**: 获取模块的非可训练参数(如 BatchNorm 的统计数据)。
- **示例**:
```python
for buf in model.buffers():
print(buf)
```
- **说明**: 如果 `recurse=True`,包括子模块的缓冲区。
---
### 7. `children() -> Iterator[Module]`
- **描述**: 返回直接子模块的迭代器。
- **用法**: 遍历模型的直接子模块。
- **示例**:
```python
for child in model.children():
print(child)
```
- **说明**: 仅返回直接子模块,不递归到更深层。
---
### 8. `cpu() -> T`
- **描述**: 将模块的所有参数和缓冲区移动到 CPU。
- **用法**: 用于设备切换。
- **示例**:
```python
model.cpu()
```
- **说明**: 确保模型和数据在同一设备上运行。
---
### 9. `cuda(device: Optional[Union[int, torch.device]] = None) -> T`
- **描述**: 将模块的所有参数和缓冲区移动到指定的 GPU 设备。
- **用法**: 指定 GPU 设备(如 `cuda:0`)。
- **示例**:
```python
model.cuda(0) # 移动到 GPU 0
```
- **说明**: 如果不指定 `device`,使用当前默认 GPU。
---
### 10. `double() -> T`
- **描述**: 将模块的参数和缓冲区转换为 `torch.float64`(双精度浮点)。
- **用法**: 提高数值精度。
- **示例**:
```python
model.double()
```
- **说明**: 通常在需要高精度计算时使用,但会增加内存占用。
---
### 11. `eval() -> T`
- **描述**: 将模块设置为评估模式。
- **用法**: 关闭训练特有的行为(如 Dropout 和 BatchNorm 的更新)。
- **示例**:
```python
model.eval()
```
- **说明**: 与 `train(False)` 等效,用于推理阶段。
---
### 12. `float() -> T`
- **描述**: 将模块的参数和缓冲区转换为 `torch.float32`(单精度浮点)。
- **用法**: 默认浮点类型,适合大多数深度学习任务。
- **示例**:
```python
model.float()
```
- **说明**: 比 `double()` 更节省内存。
---
### 13. `get_buffer(target: str) -> Tensor`
- **描述**: 返回指定名称的缓冲区。
- **用法**: 访问特定缓冲区(如 BatchNorm 的 `running_mean`)。
- **示例**:
```python
running_mean = model.get_buffer("bn.running_mean")
```
- **说明**: 如果缓冲区不存在,会抛出 `KeyError`。
---
### 14. `get_parameter(target: str) -> Parameter`
- **描述**: 返回指定名称的参数。
- **用法**: 访问特定参数(如 `weight` 或 `bias`)。
- **示例**:
```python
weight = model.get_parameter("fc1.weight")
```
- **说明**: 参数必须是注册的参数,否则抛出 `KeyError`。
---
### 15. `get_submodule(target: str) -> Module`
- **描述**: 返回指定路径的子模块。
- **用法**: 访问嵌套子模块。
- **示例**:
```python
submodule = model.get_submodule("block1.conv1")
```
- **说明**: 使用点号(如 `block1.conv1`)访问嵌套结构。
---
### 16. `half() -> T`
- **描述**: 将模块的参数和缓冲区转换为 `torch.float16`(半精度浮点)。
- **用法**: 用于加速计算和节省 GPU 内存。
- **示例**:
```python
model.half()
```
- **说明**: 需确保硬件支持半精度运算。
---
### 17. `load_state_dict(state_dict: Mapping[str, Any], strict: bool = True, assign: bool = False) -> _IncompatibleKeys`
- **描述**: 从 `state_dict` 加载参数和缓冲区。
- **用法**: 用于加载预训练模型或检查点。
- **示例**:
```python
model.load_state_dict(torch.load("model.pth"))
```
- **说明**:
- `strict=True`:要求 `state_dict` 的键完全匹配。
- `assign=True`:直接赋值而非复制(实验性功能)。
- 返回 `_IncompatibleKeys`,指示缺失或多余的键。
---
### 18. `modules() -> Iterator[Module]`
- **描述**: 返回模块及其所有子模块(递归)的迭代器。
- **用法**: 遍历整个模块树。
- **示例**:
```python
for module in model.modules():
print(module)
```
- **说明**: 比 `children()` 更深入,包含所有层级子模块。
---
### 19. `named_buffers(recurse: bool = True, prefix: str = '', remove_duplicate: bool = True) -> Iterator[Tuple[str, Tensor]]`
- **描述**: 返回模块中所有缓冲区的名称和值的迭代器。
- **用法**: 获取缓冲区的名称和值。
- **示例**:
```python
for name, buf in model.named_buffers():
print(name, buf)
```
- **说明**: 如果 `recurse=True`,包括子模块的缓冲区。
---
### 20. `named_children() -> Iterator[Tuple[str, Module]]`
- **描述**: 返回直接子模块的名称和模块的迭代器。
- **用法**: 遍历直接子模块及其名称。
- **示例**:
```python
for name, child in model.named_children():
print(name, child)
```
- **说明**: 仅返回直接子模块,不递归。
---
### 21. `named_modules(memo: Optional[Set[Module]] = None, prefix: str = '', remove_duplicate: bool = True) -> Iterator[Tuple[str, Module]]`
- **描述**: 返回模块及其所有子模块(递归)的名称和模块的迭代器。
- **用法**: 遍历整个模块树及其名称。
- **示例**:
```python
for name, module in model.named_modules():
print(name, module)
```
- **说明**: 比 `named_children()` 更深入,包含所有层级。
---
### 22. `named_parameters(recurse: bool = True, prefix: str = '', remove_duplicate: bool = True) -> Iterator[Tuple[str, Parameter]]`
- **描述**: 返回模块中所有参数的名称和值的迭代器。
- **用法**: 访问模型的参数(如权重和偏置)。
- **示例**:
```python
for name, param in model.named_parameters():
print(name, param.shape)
```
- **说明**: 如果 `recurse=True`,包括子模块的参数。
---
### 23. `parameters(recurse: bool = True) -> Iterator[Parameter]`
- **描述**: 返回模块中所有参数的迭代器。
- **用法**: 用于优化器配置。
- **示例**:
```python
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
```
- **说明**: 如果 `recurse=True`,包括子模块的参数。
---
### 24. `register_buffer(name: str, tensor: Optional[Tensor], persistent: bool = True) -> None`
- **描述**: 注册一个缓冲区(如非可训练的张量)。
- **用法**: 用于存储不需要梯度的张量(如 BatchNorm 的 `running_mean`)。
- **示例**:
```python
self.register_buffer("running_mean", torch.zeros(10))
```
- **说明**: `persistent=True` 表示缓冲区会保存到 `state_dict`。
---
### 25. `register_forward_hook(hook: Callable[..., None], *, prepend: bool = False, with_kwargs: bool = False) -> RemovableHandle`
- **描述**: 注册一个前向传播钩子函数,在模块前向传播时调用。
- **用法**: 用于监控或修改前向传播的输入/输出。
- **示例**:
```python
def hook(module, input, output):
print(output)
handle = model.register_forward_hook(hook)
```
- **说明**: 返回 `RemovableHandle`,可通过 `handle.remove()` 移除钩子。
---
### 26. `register_forward_pre_hook(hook: Callable[..., None], *, prepend: bool = False, with_kwargs: bool = False) -> RemovableHandle`
- **描述**: 注册一个前向传播前的钩子函数,在 `forward` 调用前触发。
- **用法**: 用于修改输入或调试。
- **示例**:
```python
def pre_hook(module, input):
print(input)
handle = model.register_forward_pre_hook(pre_hook)
```
- **说明**: 类似 `register_forward_hook`,但在 `forward` 之前运行。
---
### 27. `register_full_backward_hook(hook: Callable[..., None], prepend: bool = False) -> RemovableHandle`
- **描述**: 注册一个反向传播钩子函数,在梯度计算时调用。
- **用法**: 用于监控或修改梯度。
- **示例**:
```python
def backward_hook(module, grad_input, grad_output):
print(grad_output)
handle = model.register_full_backward_hook(backward_hook)
```
- **说明**: 在 PyTorch 2.0+ 中推荐使用,替代旧的 `register_backward_hook`。
---
### 28. `register_parameter(name: str, param: Optional[Parameter]) -> None`
- **描述**: 注册一个参数到模块。
- **用法**: 动态添加可训练参数。
- **示例**:
```python
self.register_parameter("weight", nn.Parameter(torch.randn(10, 5)))
```
- **说明**: 参数会自动加入 `parameters()` 和 `state_dict`。
---
### 29. `requires_grad_(requires_grad: bool = True) -> T`
- **描述**: 设置模块所有参数的 `requires_grad` 属性。
- **用法**: 冻结或解冻参数。
- **示例**:
```python
model.requires_grad_(False) # 冻结参数
```
- **说明**: 常用于冻结预训练模型的部分层。
---
### 30. `share_memory() -> T`
- **描述**: 将模块的参数和缓冲区移动到共享内存。
- **用法**: 用于多进程数据共享。
- **示例**:
```python
model.share_memory()
```
- **说明**: 主要用于 `torch.multiprocessing` 场景。
---
### 31. `state_dict(*args, destination: Optional[Dict[str, Tensor]] = None, prefix: str = '', keep_vars: bool = False) -> Dict[str, Tensor]`
- **描述**: 返回模块的参数和缓冲区的状态字典。
- **用法**: 保存模型状态。
- **示例**:
```python
torch.save(model.state_dict(), "model.pth")
```
- **说明**: `keep_vars=True` 时保留 `Tensor` 的 `Variable` 特性(较少使用)。
---
### 32. `to(*args, **kwargs) -> T`
- **描述**: 将模块的参数和缓冲区移动到指定设备或数据类型。
- **用法**: 灵活的设备/类型转换。
- **示例**:
```python
model.to(device="cuda", dtype=torch.float16)
```
- **说明**: 支持多种参数形式(如 `to(device)`, `to(dtype)`)。
---
### 33. `to_empty(*, device: Union[str, torch.device], recurse: bool = True) -> T`
- **描述**: 将模块的参数和缓冲区移动到指定设备,但不初始化内容。
- **用法**: 用于初始化空模型。
- **示例**:
```python
model.to_empty(device="cuda")
```
- **说明**: 较少使用,适合特殊场景。
---
### 34. `train(mode: bool = True) -> T`
- **描述**: 设置模块的训练模式。
- **用法**: 启用/禁用训练特有的行为(如 Dropout、BatchNorm
- **示例**:
```python
model.train() # 训练模式
model.train(False) # 等同于 eval()
```
- **说明**: 与 `eval()` 相对,用于切换训练/评估模式。
---
### 35. `type(dst_type: Union[str, torch.dtype]) -> T`
- **描述**: 将模块的参数和缓冲区转换为指定数据类型。
- **用法**: 类似 `float()`, `double()` 等,但更通用。
- **示例**:
```python
model.type(torch.float16)
```
- **说明**: 支持字符串(如 `"torch.float32"`)或 `torch.dtype`。
---
### 36. `xpu(device: Optional[Union[int, torch.device]] = None) -> T`
- **描述**: 将模块移动到 XPU 设备Intel GPU
- **用法**: 用于支持 XPU 的设备。
- **示例**:
```python
model.xpu(0)
```
- **说明**: 仅在支持 XPU 的环境中有效。
---
### 37. `zero_grad(set_to_none: bool = False) -> None`
- **描述**: 将模块所有参数的梯度清零。
- **用法**: 在优化步骤前调用。
- **示例**:
```python
model.zero_grad()
```
- **说明**: 如果 `set_to_none=True`,梯度设为 `None` 而非 0更节省内存
---
### 注意事项
- **模块管理**: 使用 `children()`, `modules()`, `named_parameters()` 等方法可以方便地管理复杂模型的结构和参数。
- **设备与类型**: 确保模型和输入数据在同一设备和数据类型(`to()`, `cuda()`, `float()` 等)。
- **钩子函数**: 钩子(如 `register_forward_hook`)是调试和动态修改模型的强大工具。
- **状态保存**: `state_dict()` 和 `load_state_dict()` 是保存和加载模型的关键方法。
### 实践建议
你可以尝试以下代码来探索 `torch.nn.Module` 的功能:
```python
import torch
import torch.nn as nn
class MyModel(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(10, 5)
self.register_buffer("running_mean", torch.zeros(5))
def forward(self, x):
return self.fc1(x)
model = MyModel()
print(list(model.named_parameters())) # 查看参数
print(list(model.named_buffers())) # 查看缓冲区
model.to("cuda") # 移动到 GPU
model.eval() # 设置评估模式
torch.save(model.state_dict(), "model.pth") # 保存模型
```
如果你想深入某个方法(如钩子函数或 `state_dict`)或需要具体代码示例,请告诉我,我可以进一步展开!

140
Pytorch/nn/nn.Sequential.md Normal file
View File

@@ -0,0 +1,140 @@
### torch.nn.Sequential 类的概述
根据 PyTorch 官网https://pytorch.org/docs/stable/generated/torch.nn.Sequential.html的最新文档`torch.nn.Sequential` 是一个顺序容器Sequential container它允许你将多个神经网络模块`nn.Module`)按顺序堆叠起来,形成一个简单的前向传播链。模块会按照构造函数中传入的顺序依次执行前向传播:输入数据传递给第一个模块,其输出作为下一个模块的输入,以此类推,最后返回最后一个模块的输出。
这个类继承自 `nn.Module`,因此它继承了 `nn.Module` 的所有通用方法和属性(如 `forward()``parameters()``train()` 等)。但官网针对 `Sequential` 特有的方法和属性进行了详细描述。下面我将逐一讲解所有特有的方法和属性,包括名称、描述、参数、返回值以及示例(基于官网内容)。注意,`Sequential` 的核心是其 `_modules` 属性(一个有序字典,用于存储子模块),其他方法大多围绕它展开。
#### 1. 构造函数 `__init__`
- **描述**:初始化 `Sequential` 容器。你可以直接传入多个 `nn.Module` 对象,或者传入一个 `OrderedDict`(键为字符串,值为模块)。这定义了模块的执行顺序。
- **参数**
- `*args`:可变数量的 `nn.Module` 对象(例如,`nn.Linear(10, 20), nn.ReLU()`)。
- `arg`(可选):一个 `collections.OrderedDict`,其中键是模块的名称(字符串),值是 `nn.Module` 对象。
- **返回值**:无(返回 `Sequential` 实例本身)。
- **注意**
-`nn.ModuleList` 的区别:`ModuleList` 只是一个模块列表,不自动连接层;`Sequential` 会自动将输出连接到下一个输入,形成级联。
- 支持动态添加模块,但初始化时定义的顺序固定。
- **示例**
```python
import torch.nn as nn
from collections import OrderedDict
# 直接传入模块
model = nn.Sequential(
nn.Conv2d(1, 20, 5),
nn.ReLU(),
nn.Conv2d(20, 64, 5),
nn.ReLU()
)
# 使用 OrderedDict便于命名模块
model = nn.Sequential(
OrderedDict([
("conv1", nn.Conv2d(1, 20, 5)),
("relu1", nn.ReLU()),
("conv2", nn.Conv2d(20, 64, 5)),
("relu2", nn.ReLU()),
])
)
```
当你打印 `model` 时,它会显示所有子模块的结构。
#### 2. 属性:`_modules`
- **描述**:这是一个私有属性(但在文档中被提及),是一个 `collections.OrderedDict`,存储所有子模块。键是模块的名称(字符串或整数索引),值是 `nn.Module` 对象。这是 `Sequential` 内部维护模块顺序的核心数据结构。
- **参数**:无(它是只读的,但可以通过方法如 `append` 等间接修改)。
- **返回值**`OrderedDict` 对象。
- **注意**
- 你不应该直接修改它(例如,不要用 `model._modules['new'] = some_module`),而应使用提供的公共方法(如 `append`、`insert`)来添加或修改模块,以确保正确性。
- 在前向传播中,`_forward_impl` 方法会遍历这个字典,按顺序调用每个模块的 `forward`。
- **示例**
```python
model = nn.Sequential(nn.Linear(1, 2))
print(model._modules) # 输出: OrderedDict([('0', Linear(...))])
```
这有助于调试,但实际使用中很少直接访问。
#### 3. 方法:`append(module)`
- **描述**:将一个给定的模块追加到 `Sequential` 容器的末尾。这会动态扩展模型,而无需重新初始化。
- **参数**
- `module`:要追加的 `nn.Module` 对象(必需)。
- **返回值**`Self`,即 `Sequential` 实例本身(支持链式调用)。
- **注意**
- 追加后,模块会自动获得一个新的索引名称(如从 '0' 开始递增)。
- 这是一个便捷方法,适合在训练过程中动态添加层。
- 源代码位置:`torch/nn/modules/container.py` 中的 Sequential 类。
- **示例**
```python
import torch.nn as nn
n = nn.Sequential(nn.Linear(1, 2), nn.Linear(2, 3))
n.append(nn.Linear(3, 4))
print(n)
# 输出:
# Sequential(
# (0): Linear(in_features=1, out_features=2, bias=True)
# (1): Linear(in_features=2, out_features=3, bias=True)
# (2): Linear(in_features=3, out_features=4, bias=True)
# )
```
#### 4. 方法:`extend(sequential)`
- **描述**:将另一个 `Sequential` 容器中的所有层扩展(追加)到当前容器的末尾。这相当于合并两个顺序模型。
- **参数**
- `sequential`:另一个 `Sequential` 实例,其中的层将被追加(必需)。
- **返回值**`Self`,即 `Sequential` 实例本身。
- **注意**
- 也可以使用 `+` 操作符实现类似效果,例如 `n + other`,它会返回一个新的 `Sequential` 而非修改原对象。
- 如果 `other` 不是 `Sequential`,可能会引发错误;确保类型匹配。
- 源代码位置:同上。
- **示例**
```python
import torch.nn as nn
n = nn.Sequential(nn.Linear(1, 2), nn.Linear(2, 3))
other = nn.Sequential(nn.Linear(3, 4), nn.Linear(4, 5))
n.extend(other) # 或者 n = n + other
print(n)
# 输出:
# Sequential(
# (0): Linear(in_features=1, out_features=2, bias=True)
# (1): Linear(in_features=2, out_features=3, bias=True)
# (2): Linear(in_features=3, out_features=4, bias=True)
# (3): Linear(in_features=4, out_features=5, bias=True)
# )
```
#### 5. 方法:`insert(index, module)`
- **描述**:在指定的索引位置插入一个模块到 `Sequential` 容器中。现有模块会根据插入位置向后移位。
- **参数**
- `index`:整数,指定插入位置(从 0 开始;如果超出范围,会追加到末尾)。
- `module`:要插入的 `nn.Module` 对象(必需)。
- **返回值**`Self`,即 `Sequential` 实例本身。
- **注意**
- 如果 `index` 等于当前模块数量,它相当于 `append`。
- 插入后,模块名称会自动调整(例如,插入到索引 1会将原 1 变为 2
- 源代码位置:同上。
- **示例**
```python
import torch.nn as nn
n = nn.Sequential(nn.Linear(1, 2), nn.Linear(2, 3))
n.insert(1, nn.ReLU()) # 在索引 1 插入 ReLU
print(n)
# 输出:
# Sequential(
# (0): Linear(in_features=1, out_features=2, bias=True)
# (1): ReLU()
# (2): Linear(in_features=2, out_features=3, bias=True)
# )
```
### 附加说明
- **继承的方法**:除了以上特有方法,`Sequential` 还继承了 `nn.Module` 的所有方法,例如:
- `forward(input)`:核心前向传播方法,自动遍历所有子模块。
- `parameters()` / `named_parameters()`:获取模型参数。
- `zero_grad()`、`step()`:用于优化器交互。
- `to(device)`:移动到指定设备(如 GPU
这些在官网的 `nn.Module` 文档中详细描述,这里不赘述。
- **使用建议**`Sequential` 适合构建简单的线性网络(如 MLP 或 CNN 的基本堆叠)。对于复杂结构(如残差连接),推荐使用自定义 `nn.Module` 子类。
- **版本信息**:以上基于 PyTorch 2.4+ 文档(官网持续更新,当前日期 2025-09-09 时为最新)。如果需要代码验证,建议在实际环境中测试。
如果您需要某个方法的更详细代码示例、数学推导或其他 PyTorch 相关问题,请随时补充!

216
Pytorch/nn/readme.md Normal file
View File

@@ -0,0 +1,216 @@
好嘞 😃,那我给你系统、详细地讲一下 **`torch.nn`** ——它基本上是 PyTorch 搭建神经网络的核心模块。
---
# 🔹 1. `torch.nn` 是什么?
* `torch.nn` = **neural network神经网络** 的缩写。
* 作用:提供了构建神经网络的各种**模块**layers、**容器**、**损失函数**、**工具函数**。
* 核心思想:把网络看成一组 **模块Module** 的组合,每个模块都可以包含参数(权重、偏置),也可以嵌套其他模块。
---
# 🔹 2. 核心类:`nn.Module`
所有神经网络的基础类,几乎所有东西(层、模型、损失函数)都继承自它。
### 常见用法
```python
import torch
import torch.nn as nn
class MyModel(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(10, 20) # 输入10维 -> 输出20维
self.fc2 = nn.Linear(20, 1) # 输入20维 -> 输出1维
def forward(self, x): # 定义前向传播
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
model = MyModel()
```
👉 所有模型都需要定义 `__init__``forward` 方法。
---
# 🔹 3. 常用层Layers
`torch.nn` 提供了很多常用的层,常见的分类:
### 1全连接层Linear
```python
nn.Linear(in_features, out_features)
```
* 就是矩阵乘法 + bias
* 常用于 MLP
---
### 2卷积层CNN
```python
nn.Conv1d, nn.Conv2d, nn.Conv3d
```
* 卷积神经网络的核心
* 用于提取空间/时序特征
---
### 3循环神经网络RNN / LSTM / GRU
```python
nn.RNN, nn.LSTM, nn.GRU
```
* 用于处理序列数据(文本、时间序列)
---
### 4归一化层
```python
nn.BatchNorm1d, nn.BatchNorm2d, nn.LayerNorm
```
* 让训练更稳定,加速收敛
---
### 5正则化层
```python
nn.Dropout(p=0.5)
```
* 随机“丢弃”神经元,防止过拟合
---
# 🔹 4. 常用容器Containers
用于把多个层组合在一起。
### 1Sequential
```python
model = nn.Sequential(
nn.Linear(10, 20),
nn.ReLU(),
nn.Linear(20, 1)
)
```
* 顺序堆叠层,适合简单模型。
### 2ModuleList / ModuleDict
```python
self.layers = nn.ModuleList([nn.Linear(10, 20), nn.Linear(20, 30)])
self.dict = nn.ModuleDict({
"fc1": nn.Linear(10, 20),
"fc2": nn.Linear(20, 1)
})
```
* 更灵活,可以动态组合模块。
---
# 🔹 5. 常用损失函数Loss functions
`torch.nn` 提供了很多常用的 loss
* **回归**
```python
nn.MSELoss() # 均方误差
nn.L1Loss() # 绝对值误差
```
* **分类**
```python
nn.CrossEntropyLoss() # 多分类交叉熵
nn.BCELoss() # 二分类交叉熵
nn.NLLLoss() # 负对数似然
```
* **其他**
```python
nn.SmoothL1Loss() # Huber loss
```
---
# 🔹 6. 激活函数
激活函数也在 `torch.nn` 里,分为 **类** 和 **函数式** 两种:
* 类形式(模块)
```python
nn.ReLU(), nn.Sigmoid(), nn.Tanh()
```
* 函数形式(函数接口)
```python
torch.relu(x), torch.sigmoid(x), torch.tanh(x)
```
👉 一般在 `nn.Sequential` 里用类形式,在 `forward` 里用函数形式。
---
# 🔹 7. 使用流程(套路)
一个典型训练流程:
```python
import torch
import torch.nn as nn
import torch.optim as optim
# 1. 定义模型
model = nn.Sequential(
nn.Linear(10, 20),
nn.ReLU(),
nn.Linear(20, 1)
)
# 2. 定义损失函数
criterion = nn.MSELoss()
# 3. 定义优化器
optimizer = optim.SGD(model.parameters(), lr=0.01)
# 4. 前向传播 + 反向传播 + 更新
x = torch.randn(5, 10) # 输入 (batch=5, features=10)
y = torch.randn(5, 1) # 目标
output = model(x) # 前向
loss = criterion(output, y) # 计算损失
optimizer.zero_grad() # 梯度清零
loss.backward() # 反向传播
optimizer.step() # 参数更新
```
---
# 🔹 8. 记忆小技巧
* **所有层、损失函数、网络结构都是 `nn.Module` 的子类**
* **训练流程三步走forward → loss → backward**
* **组合模块时,先想清楚输入输出 shape**
---
要不要我帮你整理一个 **torch.nn 思维导图(层 → 容器 → 损失函数 → 激活函数 → 用法套路)**,这样你一张图就能记住?