服装电子商务网站建设与实现,绍兴seo淄博公司,石家庄营销推广网站,国税政务公开网站建设Pytorch 复习总结#xff0c;仅供笔者使用#xff0c;参考教材#xff1a;
《动手学深度学习》Stanford University: Practical Machine Learning
本文主要内容为#xff1a;Pytorch 深度学习计算。
本文先介绍了深度学习中自定义层和块的方法#xff0c;然后介绍了一些…Pytorch 复习总结仅供笔者使用参考教材
《动手学深度学习》Stanford University: Practical Machine Learning
本文主要内容为Pytorch 深度学习计算。
本文先介绍了深度学习中自定义层和块的方法然后介绍了一些有关参数的方法。 Pytorch 语法汇总 Pytorch 张量的常见运算、线性代数、高等数学、概率论 部分 见 Pytorch 复习总结1Pytorch 线性神经网络 部分 见 Pytorch 复习总结2Pytorch 多层感知机 部分 见 Pytorch 复习总结3Pytorch 深度学习计算 部分 见 Pytorch 复习总结4Pytorch 卷积神经网络 部分 见 Pytorch 复习总结5Pytorch 现代卷积神经网络 部分 见 Pytorch 复习总结6 目录 一. 自定义块1. 顺序块2. 自定义前向传播3. 嵌套块 二. 自定义层1. 无参数层2. 有参数层 三. 参数管理1. 参数访问2. 参数初始化3. 延后初始化 四. 文件读写1. 加载和保存张量2. 加载和保存模型参数 层是神经网络的基本组成单元如全连接层、卷积层、池化层等。块是由层组成的更大的功能单元用于构建复杂的神经网络结构。块可以是一系列相互关联的层形成一个功能完整的单元也可以是一组层的重复模式用于实现重复的结构。下图就是多个层组合成块形成的更大模型
在实际应用中经常会需要自定义层和块。
一. 自定义块
1. 顺序块
nn.Sequential 本质上就是一个顺序块通过在块中实例化层来创建神经网络。 nn.Module 是 PyTorch 中用于构建神经网络模型的基类nn.Sequential 和各种层都是继承自 Modulenn.Sequential 维护一个由多个层组成的有序列表列表中的每个层连接在一起将每个层的输出作为下一个层的输入。
如果想要自定义一个顺序块必须要定义以下两个关键函数
构造函数将每个层按顺序逐个加入列表前向传播函数将每一层按顺序传递给下一层
import torch
from torch import nnclass MySequential(nn.Module):def __init__(self, *args):super().__init__()for idx, module in enumerate(args):self._modules[str(idx)] moduledef forward(self, X):# self._modules的类型是OrderedDictfor block in self._modules.values():X block(X)return Xnet MySequential(nn.Linear(20, 256),nn.ReLU(),nn.Linear(256, 10)
)X torch.rand(2, 20)
output net(X)上述示例代码中定义 net 时会自动调用 __init__(self, *args) 函数实例化 MySequential 对象调用 net(X) 相当于 net.__call__(X)会自动调用模型类中定义的 forward() 函数进行前向传播每一层的传播本质上就是调用 block(X) 的过程。
2. 自定义前向传播
nn.Sequential 类将前向传播过程封装成函数用户可以自由使用但没法修改传播细节。如果想要自定义前向传播过程中的细节就需要自定义顺序块及 forward 函数而不能仅仅依赖预定义的框架。
例如需要一个计算函数 f ( x , w ) c ⋅ w T x f(\bold x,\bold w)c \cdot \bold w ^T \bold x f(x,w)c⋅wTx 的层并且在传播过程中引入控制流。其中 x \bold x x 是输入 w \bold w w 是参数 c c c 是优化过程中不需要更新的指定常量。为此定义 FixedHiddenMLP 类如下
import torch
from torch import nn
from torch.nn import functional as Fclass FixedHiddenMLP(nn.Module):def __init__(self):super().__init__()self.rand_weight torch.rand((20, 20), requires_gradFalse) # 优化过程中不需要更新的指定常量self.linear nn.Linear(20, 20)def forward(self, X):X self.linear(X)X F.relu(torch.mm(X, self.rand_weight) 1)X self.linear(X) # 两个全连接层共享参数while X.abs().sum() 1: # 控制流X / 2return X3. 嵌套块
多个层可以组合成块多个块还可以嵌套形成更大的模型
import torch
from torch import nn
from torch.nn import functional as Fclass FixedHiddenMLP(nn.Module):def __init__(self):super().__init__()self.rand_weight torch.rand((20, 20), requires_gradFalse) # 优化过程中不需要更新的指定常量self.linear nn.Linear(20, 20)def forward(self, X):X self.linear(X)X F.relu(torch.mm(X, self.rand_weight) 1)X self.linear(X) # 两个全连接层共享参数while X.abs().sum() 1: # 控制流X / 2return X.sum()class NestMLP(nn.Module):def __init__(self):super().__init__()self.net nn.Sequential(nn.Linear(20, 64), nn.ReLU(),nn.Linear(64, 32), nn.ReLU())self.linear nn.Linear(32, 16)def forward(self, X):return self.linear(self.net(X))net nn.Sequential(NestMLP(), nn.Linear(16, 20), FixedHiddenMLP()
)X torch.rand(2, 20)
output net(X)二. 自定义层
和自定义块一样自定义层也需要实现构造函数和前向传播函数。
1. 无参数层
import torch
from torch import nnclass CenteredLayer(nn.Module):def __init__(self):super().__init__()def forward(self, X):return X - X.mean()net nn.Sequential(nn.Linear(8, 128), CenteredLayer())
X torch.rand(4, 8)
output net(X)
print(output.mean()) # tensor(0., grad_fnMeanBackward0)2. 有参数层
import torch
from torch import nn
import torch.nn.functional as Fclass MyLinear(nn.Module):def __init__(self, in_units, out_units):super().__init__()self.weight nn.Parameter(torch.randn(in_units, out_units))self.bias nn.Parameter(torch.randn(out_units,))def forward(self, X):linear torch.matmul(X, self.weight.data) self.bias.datareturn F.relu(linear)net nn.Sequential(MyLinear(64, 8), MyLinear(8, 1)
)
X torch.rand(2, 64)
output net(X)
print(output) # tensor([[11.9497], [13.9729]])三. 参数管理
在实验过程中有时需要提取参数以便检查或在其他环境中复用。本节将介绍参数的访问方法和参数的初始化。
1. 参数访问
net.state_dict() / net[i].state_dict()返回模型或某一层参数的状态字典net[i].weight.data / net[i].bias.data返回某一层的权重 / 偏置参数net[i].weight.grad返回某一层的权重参数的梯度属性。只有调用了 backward() 方法后才能访问到梯度值否则为 None
import torch
from torch import nnnet nn.Sequential(nn.Linear(4, 8), nn.ReLU(), nn.Linear(8, 1))
X torch.rand(size(2, 4))
output net(X)print(net.state_dict())OrderedDict([(0.weight, tensor([[ 0.2178, -0.3286, 0.4875, -0.0347],[-0.0415, 0.0009, -0.2038, -0.1813],[-0.2766, -0.4759, -0.3134, -0.2782],[ 0.4854, 0.0606, 0.1070, 0.0650],[-0.3908, 0.2412, -0.1348, 0.3921],[-0.3044, -0.0331, -0.1213, -0.1690],[-0.3875, -0.0117, 0.3195, -0.1748],[ 0.1840, -0.3502, 0.4253, 0.2789]])), (0.bias, tensor([-0.2327, -0.0745, 0.4923, -0.1018, 0.0685, 0.4423, -0.2979, 0.1109])), (2.weight, tensor([[ 0.1006, 0.2959, -0.1316, -0.2015, 0.2446, -0.0158, 0.2217, -0.2780]])), (2.bias, tensor([0.2362]))])print(net[2].state_dict())OrderedDict([(weight, tensor([[ 0.1006, 0.2959, -0.1316, -0.2015, 0.2446, -0.0158, 0.2217, -0.2780]])), (bias, tensor([0.2362]))])print(net[2].bias)Parameter containing:
tensor([0.2362], requires_gradTrue)print(net[2].bias.data)tensor([0.2362])如果想一次性访问所有参数可以使用 for 循环递归遍历
import torch
from torch import nnnet nn.Sequential(nn.Linear(4, 8), nn.ReLU(), nn.Linear(8, 1))
X torch.rand(size(2, 4))
output net(X)print(*[(name, param.shape) for name, param in net[0].named_parameters()])(weight, torch.Size([8, 4])) (bias, torch.Size([8]))print(*[(name, param.shape) for name, param in net.named_parameters()])(0.weight, torch.Size([8, 4])) (0.bias, torch.Size([8])) (2.weight, torch.Size([1, 8])) (2.bias, torch.Size([1]))如果网络是由多个块相互嵌套的可以按块索引后再访问参数
import torch
from torch import nndef block1():return nn.Sequential(nn.Linear(4, 8), nn.ReLU(),nn.Linear(8, 4), nn.ReLU())def block2():net nn.Sequential()for i in range(4):net.add_module(fblock {i}, block1())return netnet nn.Sequential(block2(), nn.Linear(4, 1))
X torch.rand(size(2, 4))
output net(X)print(net)Sequential((0): Sequential((block 0): Sequential((0): Linear(in_features4, out_features8, biasTrue)(1): ReLU()(2): Linear(in_features8, out_features4, biasTrue)(3): ReLU())(block 1): Sequential((0): Linear(in_features4, out_features8, biasTrue)(1): ReLU()(2): Linear(in_features8, out_features4, biasTrue)(3): ReLU())(block 2): Sequential((0): Linear(in_features4, out_features8, biasTrue)(1): ReLU()(2): Linear(in_features8, out_features4, biasTrue)(3): ReLU())(block 3): Sequential((0): Linear(in_features4, out_features8, biasTrue)(1): ReLU()(2): Linear(in_features8, out_features4, biasTrue)(3): ReLU()))(1): Linear(in_features4, out_features1, biasTrue)
)print(net[0][1][0].bias.data)tensor([-0.0083, 0.2490, 0.1794, 0.1927, 0.1797, 0.1156, 0.4409, 0.1320])2. 参数初始化
PyTorch 的 nn.init 模块提供了多种初始化方法
nn.init.constant_(layer.weight, c)将权重参数初始化为指定的常量值nn.init.zeros_(layer.weight)将权重参数初始化为 0nn.init.ones_(layer.weight)将权重参数初始化为 1nn.init.uniform_(layer.weight, a, b)将权重参数按均匀分布初始化nn.init.xavier_uniform_(layer.weight)nn.init.normal_(layer.weight, mean, std)将权重参数按正态分布初始化nn.init.orthogonal_(layer.weight)将权重参数初始化为正交矩阵nn.init.sparse_(layer.weight, sparsity, std)将权重参数初始化为稀疏矩阵
初始化时可以直接 net.apply(init_method) 初始化整个网络也可以 net[i].apply(init_method) 初始化某一层
import torch
from torch import nndef init_normal(m):if type(m) nn.Linear:nn.init.normal_(m.weight, mean0, std0.01)nn.init.zeros_(m.bias)def init_constant(m):if type(m) nn.Linear:nn.init.constant_(m.weight, 1)nn.init.zeros_(m.bias)net nn.Sequential(nn.Linear(4, 8), nn.ReLU(), nn.Linear(8, 1))
X torch.rand(size(2, 4))
output net(X)# net.apply(init_normal)
net[0].apply(init_normal)
net[2].apply(init_constant)3. 延后初始化
有些情况下无法提前判断网络的输入维度。为了代码能够继续运行需要使用延后初始化即直到数据第一次通过模型传递时框架才会动态地推断出每个层的大小。由于 PyTorch 的延后初始化功能还处于开发阶段API 和功能随时可能变化下面只给出简单示例
import torch
from torch import nnnet nn.Sequential(nn.LazyLinear(256), nn.ReLU(), nn.LazyLinear(10))print(net)Sequential((0): LazyLinear(in_features0, out_features256, biasTrue)(1): ReLU()(2): LazyLinear(in_features0, out_features10, biasTrue)
)
X torch.rand(2, 20)
net(X)
print(net)Sequential((0): Linear(in_features20, out_features256, biasTrue)(1): ReLU()(2): Linear(in_features256, out_features10, biasTrue)
)四. 文件读写
可以使用 load 和 save 函数读写张量和模型参数。
1. 加载和保存张量
2. 加载和保存模型参数