ding.torch_utils¶
损失¶
请参考 ding/torch_utils/loss 获取更多详细信息。
对比损失¶
- class ding.torch_utils.loss.ContrastiveLoss(x_size: int | SequenceType, y_size: int | SequenceType, heads: SequenceType = [1, 1], encode_shape: int = 64, loss_type: str = 'infoNCE', temperature: float = 1.0)[源代码]¶
- Overview:
对比学习损失的类。目前仅支持InfoNCE损失。代码参考:https://github.com/rdevon/DIM。论文参考:https://arxiv.org/abs/1808.06670。
- Interfaces:
__init__,forward.
- __init__(x_size: int | SequenceType, y_size: int | SequenceType, heads: SequenceType = [1, 1], encode_shape: int = 64, loss_type: str = 'infoNCE', temperature: float = 1.0) None[源代码]¶
- Overview:
使用给定的参数初始化ContrastiveLoss对象。
- Arguments:
x_size (
Union[int, SequenceType]): x的输入形状,支持观测形状和编码形状。y_size (
Union[int, SequenceType]): y的输入形状,支持观测形状和编码形状。heads (
SequenceType): 一个包含2个整数的列表,heads[0]用于x,head[1]用于y。用于多头、全局-局部、局部-局部MI最大化过程。encoder_shape (
Union[int, SequenceType]): 编码器隐藏状态的维度。loss_type: 目前仅支持InfoNCE损失。
温度:用于调整
log_softmax的参数。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _create_encoder(obs_size: int | SequenceType, heads: int) Module[源代码]¶
- Overview:
为输入观测创建编码器。
- Arguments:
obs_size (
Union[int, SequenceType]): x的输入形状,支持obs形状和编码形状。如果obs_size是一个整数,意味着obs是一个一维向量。如果obs_size是一个列表,例如[1, 16, 16],意味着obs是一个形状为[1, 16, 16]的三维图像。heads (
int): 头的数量。
- Returns:
编码器 (
nn.Module): 编码器模块。
- Examples:
>>> obs_size = 16 or >>> obs_size = [1, 16, 16] >>> heads = 1 >>> encoder = self._create_encoder(obs_size, heads)
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor, y: Tensor) Tensor[source]¶
- Overview:
计算基于噪声对比估计的损失,也称为infoNCE。
- Arguments:
x (
torch.Tensor): 输入x,支持原始观测值和编码。y (
torch.Tensor): 输入 y,支持原始观测值和编码。
- Returns:
损失 (
torch.Tensor): 计算得到的损失值。- Examples:
>>> x_dim = [3, 16] >>> encode_shape = 16 >>> x = np.random.normal(0, 1, size=x_dim) >>> y = x ** 2 + 0.01 * np.random.normal(0, 1, size=x_dim) >>> estimator = ContrastiveLoss(dims, dims, encode_shape=encode_shape) >>> loss = estimator.forward(x, y)
- Examples:
>>> x_dim = [3, 1, 16, 16] >>> encode_shape = 16 >>> x = np.random.normal(0, 1, size=x_dim) >>> y = x ** 2 + 0.01 * np.random.normal(0, 1, size=x_dim) >>> estimator = ContrastiveLoss(dims, dims, encode_shape=encode_shape) >>> loss = estimator.forward(x, y)
- training: bool¶
标签平滑交叉熵损失¶
- class ding.torch_utils.loss.LabelSmoothCELoss(ratio: float)[源代码]¶
- Overview:
标签平滑交叉熵损失。
- Interfaces:
__init__,forward.
- __init__(ratio: float) None[source]¶
- Overview:
使用给定的参数初始化LabelSmoothCELoss对象。
- Arguments:
ratio (
float): 标签平滑的比例(值在0-1之间)。如果比例较大,标签平滑的程度也较大。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(logits: Tensor, labels: LongTensor) Tensor[source]¶
- Overview:
计算标签平滑交叉熵损失。
- Arguments:
logits (
torch.Tensor): 预测的logits。标签 (
torch.LongTensor): 真实值。
- Returns:
损失 (
torch.Tensor): 计算出的损失。
- training: bool¶
SoftFocalLoss¶
- class ding.torch_utils.loss.SoftFocalLoss(gamma: int = 2, weight: Any | None = None, size_average: bool = True, reduce: bool | None = None)[来源]¶
- Overview:
软焦点损失。
- Interfaces:
__init__,forward.
- __init__(gamma: int = 2, weight: Any | None = None, size_average: bool = True, reduce: bool | None = None) None[来源]¶
- Overview:
使用给定的参数初始化SoftFocalLoss对象。
- Arguments:
gamma (
int): 对困难样本的关注程度。较小的gamma会导致更多关注简单样本,而较大的gamma会导致更多关注困难样本。权重 (
Any): 每个类别的损失权重。size_average (
bool): 默认情况下,损失是在批次中的每个损失元素上平均的。 注意,对于某些损失,每个样本有多个元素。如果字段size_average被 设置为False,则损失将在每个小批次中求和。当reduce为False时,此设置将被忽略。reduce (
Optional[bool]): 默认情况下,损失会根据size_average在每个小批量中对观测值进行平均或求和。当reduce为False时,返回每个批次元素的损失,并忽略size_average。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(inputs: Tensor, targets: LongTensor) Tensor[来源]¶
- Overview:
计算软焦点损失。
- Arguments:
logits (
torch.Tensor): 预测的logits。标签 (
torch.LongTensor): 真实值。
- Returns:
损失 (
torch.Tensor): 计算出的损失。
- training: bool¶
构建CE标准¶
MultiLogitsLoss¶
- class ding.torch_utils.loss.MultiLogitsLoss(criterion: str | None = None, smooth_ratio: float = 0.1)[source]¶
- Overview:
用于在linklink上进行监督学习的基础类,包括基本流程。
- Interfaces:
__init__,forward.
- __init__(criterion: str | None = None, smooth_ratio: float = 0.1) None[源代码]¶
- Overview:
初始化方法,默认使用交叉熵作为标准。
- Arguments:
criterion (
str): 标准类型,支持 [‘cross_entropy’, ‘label_smooth_ce’]。smooth_ratio (
float): 用于标签平滑的平滑比例。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- static _get_distance_matrix(lx: ndarray, ly: ndarray, mat: ndarray, M: int) ndarray[source]¶
- Overview:
获取距离矩阵。
- Arguments:
lx (
np.ndarray): lx.ly (
np.ndarray): ly.mat (
np.ndarray): 矩阵。M (
int): M.
- _get_metric_matrix(logits: Tensor, labels: LongTensor) Tensor[来源]¶
- Overview:
计算度量矩阵。
- Arguments:
logits (
torch.Tensor): 预测的logits。标签 (
torch.LongTensor): 真实值。
- Returns:
metric (
torch.Tensor): 计算得到的度量矩阵。
- _is_full_backward_hook: bool | None¶
- _label_process(logits: Tensor, labels: LongTensor) LongTensor[来源]¶
- Overview:
根据标准处理标签。
- Arguments:
logits (
torch.Tensor): 预测的logits。标签 (
torch.LongTensor): 真实值。
- Returns:
ret (
torch.LongTensor): 处理后的标签。
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _match(matrix: Tensor)[source]¶
- Overview:
匹配度量矩阵。
- Arguments:
矩阵 (
torch.Tensor): 度量矩阵。
- Returns:
索引 (
np.ndarray): 匹配的索引。
- _modules: Dict[str, Module | None]¶
- _nll_loss(nlls: Tensor, labels: LongTensor) Tensor[来源]¶
- Overview:
计算负对数似然损失。
- Arguments:
nlls (
torch.Tensor): 负对数似然损失。标签 (
torch.LongTensor): 真实值。
- Returns:
ret (
torch.Tensor): 计算得到的损失。
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(logits: Tensor, labels: LongTensor) Tensor[源代码]¶
- Overview:
计算多个logits的损失。
- Arguments:
logits (
torch.Tensor): 预测的logits,其形状必须为二维,如 (B, N)。标签 (
torch.LongTensor): 真实值。
- Returns:
损失 (
torch.Tensor): 计算出的损失。
- training: bool¶
network.activation¶
请参考 ding/torch_utils/network/activation 获取更多详细信息。
Lambda¶
- class ding.torch_utils.network.activation.Lambda(f: Callable)[source]¶
- Overview:
用于构建自定义层的自定义lambda模块。
- Interfaces:
__init__,forward.
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- training: bool¶
GLU¶
- class ding.torch_utils.network.activation.GLU(input_dim: int, output_dim: int, context_dim: int, input_type: str = 'fc')[source]¶
- Overview:
门控线性单元(GLU),一种特定类型的激活函数,首次在 [使用门控卷积网络进行语言建模](https://arxiv.org/pdf/1612.08083.pdf)中提出。
- Interfaces:
__init__,forward.
- __init__(input_dim: int, output_dim: int, context_dim: int, input_type: str = 'fc') None[源代码]¶
- Overview:
初始化GLU模块。
- Arguments:
input_dim (
int): 输入张量的维度。output_dim (
int): 输出张量的维度。context_dim (
int): 上下文张量的维度。input_type (
str): 输入的类型,目前支持 [‘fc’, ‘conv2d’]
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor, context: Tensor) Tensor[源代码]¶
- Overview:
计算输入张量的GLU变换。
- Arguments:
x (
torch.Tensor): 输入张量。上下文 (
torch.Tensor): 上下文张量。
- Returns:
x (
torch.Tensor): GLU变换后的输出张量。
- training: bool¶
Swish¶
- class ding.torch_utils.network.activation.Swish[源代码]¶
- Overview:
Swish激活函数,这是一种平滑、非单调的激活函数。更多详情,请参考[搜索激活函数](https://arxiv.org/pdf/1710.05941.pdf)。
- Interfaces:
__init__,forward.
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor) Tensor[源代码]¶
- Overview:
计算输入张量的Swish变换。
- Arguments:
x (
torch.Tensor): 输入张量。
- Returns:
x (
torch.Tensor): Swish 变换后的输出张量。
- training: bool¶
GELU¶
- class ding.torch_utils.network.activation.GELU[源代码]¶
- Overview:
高斯误差线性单元(GELU)激活函数,广泛用于GPT、BERT等NLP模型中。 更多详情,请参考原始论文:https://arxiv.org/pdf/1606.08415.pdf。
- Interfaces:
__init__,forward.
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor) Tensor[来源]¶
- Overview:
计算输入张量的GELU变换。
- Arguments:
x (
torch.Tensor): 输入张量。
- Returns:
x (
torch.Tensor): GELU变换后的输出张量。
- training: bool¶
build_activation¶
- ding.torch_utils.network.activation.build_activation(activation: str, inplace: bool | None = None) Module[源代码]¶
- Overview:
根据给定的类型构建并返回激活模块。
- Arguments:
activation (
str): 激活模块的类型,目前支持 [‘relu’, ‘glu’, ‘prelu’, ‘swish’, ‘gelu’, ‘tanh’, ‘sigmoid’, ‘softplus’, ‘elu’, ‘square’, ‘identity’]。inplace (Optional[
bool): 在激活中就地执行操作,默认为 None。
- Returns:
act_func (
nn.module): 对应的激活模块。
网络扩散¶
请参考 ding/torch_utils/network/diffusion 获取更多详细信息。
提取¶
余弦beta调度¶
应用条件¶
DiffusionConv1d¶
- class ding.torch_utils.network.diffusion.DiffusionConv1d(in_channels: int, out_channels: int, kernel_size: int, padding: int, activation: Module | None = None, n_groups: int = 8)[源代码]¶
- Overview:
用于扩散模型的带有激活和归一化的Conv1d。
- Interfaces:
__init__,forward
- __init__(in_channels: int, out_channels: int, kernel_size: int, padding: int, activation: Module | None = None, n_groups: int = 8) None[源代码]¶
- Overview:
创建一个带有激活和归一化的一维卷积层。这个Conv1d具有GroupNorm。 并且在计算归一化时需要添加一维。
- Arguments:
in_channels (
int): 输入张量中的通道数out_channels (
int): 输出张量中的通道数kernel_size (
int): 卷积核的大小填充 (
int): 添加到输入两侧的零填充activation (
nn.Module): 可选的激活函数
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(inputs) Tensor[来源]¶
- Overview:
计算输入的一维卷积。
- Arguments:
输入 (
torch.Tensor): 输入张量
- Return:
输出 (
torch.Tensor): 输出张量
- training: bool¶
正弦位置嵌入¶
- class ding.torch_utils.network.diffusion.SinusoidalPosEmb(dim: int)[源代码]¶
- Overview:
用于计算正弦位置嵌入的类
- Interfaces:
__init__,forward
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x) Tensor[源代码]¶
- Overview:
计算正弦位置嵌入
- Arguments:
x (
torch.Tensor): 输入张量
- Return:
emb (
torch.Tensor): 输出张量
- training: bool¶
残差¶
- class ding.torch_utils.network.diffusion.Residual(fn)[源代码]¶
- Overview:
基本残差块
- Interfaces:
__init__,forward
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- training: bool¶
层归一化¶
- class ding.torch_utils.network.diffusion.LayerNorm(dim, eps=1e-05)[来源]¶
- Overview:
LayerNorm,计算维度 = 1,因为时间输入 x [batch, dim, horizon]
- Interfaces:
__init__,forward
- __init__(dim, eps=1e-05) None[源代码]¶
- Overview:
LayerNorm 类的初始化
- Arguments:
dim (
int): 输入的维度eps (
float): LayerNorm 的 eps
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- training: bool¶
预归一化¶
- class ding.torch_utils.network.diffusion.PreNorm(dim, fn)[来源]¶
- Overview:
PreNorm,计算维度 = 1,因为时间输入 x [batch, dim, horizon]
- Interfaces:
__init__,forward
- __init__(dim, fn) None[source]¶
- Overview:
PreNorm 类的初始化
- Arguments:
dim (
int): 输入的维度fn (
nn.Module): 残差块的函数
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- training: bool¶
线性注意力¶
- class ding.torch_utils.network.diffusion.LinearAttention(dim, heads=4, dim_head=32)[源代码]¶
- Overview:
线性注意力头
- Interfaces:
__init__,forward
- __init__(dim, heads=4, dim_head=32) None[源代码]¶
- Overview:
LinearAttention 类的初始化
- Arguments:
dim (
int): 输入的维度heads (
int): 注意力头数dim_head (
int): 头的维度
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- training: bool¶
残差时间块¶
- class ding.torch_utils.network.diffusion.ResidualTemporalBlock(in_channels: int, out_channels: int, embed_dim: int, kernel_size: int = 5, mish: bool = True)[source]¶
- Overview:
时间残差块
- Interfaces:
__init__,forward
- __init__(in_channels: int, out_channels: int, embed_dim: int, kernel_size: int = 5, mish: bool = True) None[源代码]¶
- Overview:
ResidualTemporalBlock 类的初始化
- Arguments:
in_channels (:obj:’int’): 输入通道的维度
out_channels (:obj:’int’): 输出通道的维度
embed_dim (:obj:’int’): 嵌入层的维度
kernel_size (:obj:’int’): conv1d的kernel_size
mish (:obj:’bool’): 是否使用mish作为激活函数
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- training: bool¶
DiffusionUNet1d¶
- class ding.torch_utils.network.diffusion.DiffusionUNet1d(transition_dim: int, dim: int = 32, dim_mults: SequenceType = [1, 2, 4, 8], returns_condition: bool = False, condition_dropout: float = 0.1, calc_energy: bool = False, kernel_size: int = 5, attention: bool = False)[source]¶
- Overview:
用于一维向量数据的扩散unet
- Interfaces:
__init__,forward,get_pred
- __init__(transition_dim: int, dim: int = 32, dim_mults: SequenceType = [1, 2, 4, 8], returns_condition: bool = False, condition_dropout: float = 0.1, calc_energy: bool = False, kernel_size: int = 5, attention: bool = False) None[source]¶
- Overview:
DiffusionUNet1d 类的初始化
- Arguments:
transition_dim (:obj:’int’): 转换的维度,它是 obs_dim + action_dim
dim (:obj:’int’): 层的维度
dim_mults (:obj:’SequenceType’): 维度的倍数
returns_condition (:obj:’bool’): 是否使用返回作为条件
condition_dropout (:obj:’float’): 返回条件的dropout
calc_energy (:obj:’bool’): 是否使用calc_energy
kernel_size (:obj:’int’): conv1d的kernel_size
attention (:obj:’bool’): 是否使用注意力
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x, cond, time, returns=None, use_dropout: bool = True, force_dropout: bool = False)[源代码]¶
- Overview:
计算扩散unet前向传播
- Arguments:
x (:obj:’tensor’): 噪声轨迹
cond (:obj:’tuple’): [ (时间, 状态), … ] 状态是环境的初始状态,时间 = 0
时间 (:obj:’int’): 扩散步骤的时间步长
返回 (:obj:’tensor’): 轨迹的条件返回,返回是正常返回
use_dropout (:obj:’bool’): 是否使用返回条件掩码
force_dropout (:obj:’bool’): 是否使用返回条件
- get_pred(x, cond, time, returns: bool | None = None, use_dropout: bool = True, force_dropout: bool = False)[source]¶
- Overview:
计算扩散unet前向传播
- Arguments:
x (:obj:’tensor’): 噪声轨迹
cond (:obj:’tuple’): [ (时间, 状态), … ] 状态是环境的初始状态,时间 = 0
time (:obj:’int’): 扩散步骤的时间步长
返回 (:obj:’tensor’): 轨迹的条件返回,返回是正常返回
use_dropout (:obj:’bool’): 是否使用返回条件掩码
force_dropout (:obj:’bool’): 是否使用返回条件
- training: bool¶
时间值¶
- class ding.torch_utils.network.diffusion.TemporalValue(horizon: int, transition_dim: int, dim: int = 32, time_dim: int | None = None, out_dim: int = 1, kernel_size: int = 5, dim_mults: SequenceType = [1, 2, 4, 8])[source]¶
- Overview:
用于价值函数的时间网络
- Interfaces:
__init__,forward
- __init__(horizon: int, transition_dim: int, dim: int = 32, time_dim: int | None = None, out_dim: int = 1, kernel_size: int = 5, dim_mults: SequenceType = [1, 2, 4, 8]) None[source]¶
- Overview:
TemporalValue 类的初始化
- Arguments:
horizon (:obj:’int’): 轨迹的视野
transition_dim (:obj:’int’): 转换的维度,它是 obs_dim + action_dim
dim (:obj:’int’): 层的维度
time_dim (:obj:’int’): 时间的维度
out_dim (:obj:’int’): 输出的维度
kernel_size (:obj:’int’): conv1d的kernel_size
dim_mults (:obj:’SequenceType’): 维度的倍数
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x, cond, time, *args)[source]¶
- Overview:
计算时间值向前
- Arguments:
x (:obj:’tensor’): 噪声轨迹
cond (:obj:’tuple’): [ (时间, 状态), … ] 状态是环境的初始状态,时间 = 0
time (:obj:’int’): 扩散步骤的时间步长
- training: bool¶
network.dreamer¶
请参考 ding/torch_utils/network/dreamer 了解更多详情。
Conv2dSame¶
- class ding.torch_utils.network.dreamer.Conv2dSame(in_channels: int, out_channels: int, kernel_size: int | Tuple[int, int], stride: int | Tuple[int, int] = 1, padding: str | int | Tuple[int, int] = 0, dilation: int | Tuple[int, int] = 1, groups: int = 1, bias: bool = True, padding_mode: str = 'zeros', device=None, dtype=None)[source]¶
- Overview:
用于dreamerv3的Conv2dSame网络。
- Interfaces:
__init__,forward
- _reversed_padding_repeated_twice: List[int]¶
- bias: Tensor | None¶
- calc_same_pad(i, k, s, d)[来源]¶
- Overview:
计算相同的填充大小。
- Arguments:
i (
int): 输入大小。k (
int): 核大小。s (
int): 步幅大小。d (
int): 扩张大小。
- dilation: Tuple[int, ...]¶
- groups: int¶
- in_channels: int¶
- kernel_size: Tuple[int, ...]¶
- out_channels: int¶
- output_padding: Tuple[int, ...]¶
- padding: str | Tuple[int, ...]¶
- padding_mode: str¶
- stride: Tuple[int, ...]¶
- transposed: bool¶
- weight: Tensor¶
DreamerLayerNorm¶
- class ding.torch_utils.network.dreamer.DreamerLayerNorm(ch, eps=0.001)[来源]¶
- Overview:
DreamerLayerNorm 网络用于 dreamerv3。
- Interfaces:
__init__,forward
- __init__(ch, eps=0.001)[source]¶
- Overview:
初始化 DreamerLayerNorm 类。
- Arguments:
ch (
int): 输入通道。eps (
float): 埃普西隆。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- training: bool¶
DenseHead¶
- class ding.torch_utils.network.dreamer.DenseHead(inp_dim, shape, layer_num, units, act='SiLU', norm='LN', dist='normal', std=1.0, outscale=1.0, device='cpu')[源代码]¶
- Overview:
DenseHead 网络用于 dreamerv3 的值头、奖励头和折扣头。
- Interfaces:
__init__,forward
- __init__(inp_dim, shape, layer_num, units, act='SiLU', norm='LN', dist='normal', std=1.0, outscale=1.0, device='cpu')[来源]¶
- Overview:
初始化 DenseHead 类。
- Arguments:
inp_dim (
int): 输入维度。形状 (
tuple): 输出形状。层数 (
int): 层数。单位数 (
int): 单位的数量。act (
str): 激活函数。norm (
str): 归一化函数。dist (
str): 分布函数。std (
float): 标准差。outscale (
float): 输出比例。设备 (
str): 设备。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- training: bool¶
操作头¶
- class ding.torch_utils.network.dreamer.ActionHead(inp_dim, size, layers, units, act=<class 'torch.nn.modules.activation.ELU'>, norm=<class 'torch.nn.modules.normalization.LayerNorm'>, dist='trunc_normal', init_std=0.0, min_std=0.1, max_std=1.0, temp=0.1, outscale=1.0, unimix_ratio=0.01)[source]¶
- Overview:
Dreamerv3动作头的ActionHead网络。
- Interfaces:
__init__,forward
- __init__(inp_dim, size, layers, units, act=<class 'torch.nn.modules.activation.ELU'>, norm=<class 'torch.nn.modules.normalization.LayerNorm'>, dist='trunc_normal', init_std=0.0, min_std=0.1, max_std=1.0, temp=0.1, outscale=1.0, unimix_ratio=0.01)[source]¶
- Overview:
初始化 ActionHead 类。
- Arguments:
inp_dim (
int): 输入维度。大小 (
int): 输出大小。层数 (
int): 层数。单位数 (
int): 单位的数量。act (
str): 激活函数。norm (
str): 归一化函数。dist (
str): 分布函数。init_std (
float): 初始标准差。min_std (
float): 最小标准差。max_std (
float): 最大标准差。温度 (
float): 温度。outscale (
float): 输出比例。unimix_ratio (
float): Unimix 比例。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- training: bool¶
SampleDist¶
OneHotDist¶
- class ding.torch_utils.network.dreamer.OneHotDist(logits=None, probs=None, unimix_ratio=0.0)[source]¶
- Overview:
一种用于dreamerv3的onehot分布。
- Interfaces:
__init__,mode,sample
TwoHotDistSymlog¶
- class ding.torch_utils.network.dreamer.TwoHotDistSymlog(logits=None, low=-20.0, high=20.0, device='cpu')[source]¶
- Overview:
一种用于dreamerv3的twohotsymlog Dist。
- Interfaces:
__init__,mode,mean,log_prob,log_prob_target
SymlogDist¶
- class ding.torch_utils.network.dreamer.SymlogDist(mode, dist='mse', aggregation='sum', tol=1e-08, dim_to_reduce=[-1, -2, -3])[来源]¶
- Overview:
一种用于dreamerv3的Symlog分布。
- Interfaces:
__init__,entropy,mode,mean,log_prob
连续分布¶
伯努利¶
network.gtrxl¶
请参考 ding/torch_utils/network/gtrxl 了解更多详情。
位置嵌入¶
- class ding.torch_utils.network.gtrxl.PositionalEmbedding(embedding_dim: int)[来源]¶
- Overview:
PositionalEmbedding 模块实现了在原始 Transformer 模型中使用的位置嵌入。
- Interfaces:
__init__,forward
注意
此实现改编自 https://github.com/kimiyoung/transformer-xl/blob/ master/pytorch/mem_transformer.py
- __init__(embedding_dim: int)[来源]¶
- Overview:
初始化PositionalEmbedding模块。
- Arguments:
embedding_dim: (
int): 嵌入的维度。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(pos_seq: Tensor) Tensor[source]¶
- Overview:
计算给定位置序列的位置嵌入。
- Arguments:
pos_seq (
torch.Tensor): 位置序列,通常是一个1D整数张量,形式为 [seq_len-1, seq_len-2, …, 1, 0],
- Returns:
pos_embedding (
torch.Tensor): 计算得到的位置嵌入。张量的形状为 (seq_len, 1, embedding_dim)。
- training: bool¶
GRUGatingUnit¶
- class ding.torch_utils.network.gtrxl.GRUGatingUnit(input_dim: int, bg: float = 2.0)[source]¶
- Overview:
GRUGatingUnit 模块实现了 GTrXL 模型中使用的 GRU 门控机制。
- Interfaces:
__init__,forward
- __init__(input_dim: int, bg: float = 2.0)[源代码]¶
- Overview:
初始化GRUGatingUnit模块。
- Arguments:
input_dim (
int): 输入的维度。bg (
bg): 门偏置。通过设置 bg > 0,我们可以显式地将门控机制初始化为接近恒等映射。这可以极大地提高学习速度和稳定性,因为它将代理初始化为接近马尔可夫策略(在开始时忽略注意力)。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor, y: Tensor)[来源]¶
- Overview:
使用GRU门控机制计算输出值。
- Arguments:
x: (
torch.Tensor): 第一个输入张量。y: (
torch.Tensor): 第二个输入张量。x 和 y 应该具有相同的形状,并且它们的最后一个维度应该与 input_dim 匹配。
- Returns:
g: (
torch.Tensor): GRU门控机制的输出。g的形状与x和y的形状相匹配。
- training: bool¶
内存¶
- class ding.torch_utils.network.gtrxl.Memory(memory_len: int = 20, batch_size: int = 64, embedding_dim: int = 256, layer_num: int = 3, memory: Tensor | None = None)[来源]¶
- Overview:
一个类,用于存储用于向Transformer添加内存的上下文。
- Interfaces:
__init__,init,update,get,to
注意
详情请参考Transformer-XL:https://arxiv.org/abs/1901.02860
- __init__(memory_len: int = 20, batch_size: int = 64, embedding_dim: int = 256, layer_num: int = 3, memory: Tensor | None = None) None[source]¶
- Overview:
初始化内存模块。
- Arguments:
memory_len (
int): 记忆的维度,即使用多少过去的观测值作为记忆。batch_size (
int): 每个批次的维度。embedding_dim (
int): 嵌入的维度,即嵌入后单个观测值的维度。层数 (
int): transformer层的数量。内存 (
Optional[torch.Tensor]): 初始内存。默认为 None。
- get()[source]¶
- Overview:
获取当前内存。
- Returns:
memory: (
Optional[torch.Tensor]): 当前的内存,形状为 (layer_num, memory_len, bs, embedding_dim)。
- init(memory: Tensor | None = None)[source]¶
- Overview:
使用输入的张量列表初始化内存,或根据其维度自动创建内存。
- Arguments:
memory (
Optional[torch.Tensor]): 输入的记忆张量,形状为 (layer_num, memory_len, bs, embedding_dim)。其形状为 (layer_num, memory_len, bs, embedding_dim),其中 memory_len 是记忆的长度,bs 是批量大小,embedding_dim 是嵌入的维度。
- update(hidden_state: List[Tensor])[source]¶
- Overview:
根据一系列隐藏状态更新内存。 单层示例:(memory_len=3, hidden_size_len=2, bs=3)
m00 m01 m02 h00 h01 h02 m20 m21 m22
- m = m10 m11 m12 h = h10 h11 h12 => new_m = h00 h01 h02
m20 m21 m22 h10 h11 h12
- Arguments:
hidden_state: (
List[torch.Tensor]): 用于更新内存的隐藏状态。列表中的每个张量的形状为 (cur_seq, bs, embedding_dim),其中 cur_seq 是序列的长度。
- Returns:
memory: (
Optional[torch.Tensor]): 更新后的内存,形状为 (layer_num, memory_len, bs, embedding_dim)。
AttentionXL¶
- class ding.torch_utils.network.gtrxl.AttentionXL(input_dim: int, head_dim: int, head_num: int, dropout: Module)[来源]¶
- Overview:
TransformerXL模型中使用的注意力机制的实现。
- Interfaces:
__init__,forward
- __init__(input_dim: int, head_dim: int, head_num: int, dropout: Module) None[source]¶
- Overview:
初始化AttentionXL模块。
- Arguments:
input_dim (
int): 输入特征的维度。head_dim (
int): 每个注意力头的维度。head_num (
int): 注意力头的数量。dropout (
nn.Module): 使用的dropout层
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _rel_shift(x: Tensor, zero_upper: bool = False) Tensor[source]¶
- Overview:
对注意力分数矩阵执行相对移位操作。 示例:
a00 a01 a02 0 a00 a01 a02 0 a00 a01 a02 0 a10 a02 0 0 a10 a11 a12 => 0 a10 a11 a12 => a02 0 a10 => a11 a12 0 => a11 a12 0 a20 a21 a22 0 a20 a21 a22 a11 a12 0 a20 a21 a22 a20 a21 a22
a20 a21 a22
在左侧追加一“列”零
将矩阵从 [3 x 4] 重塑为 [4 x 3]
移除第一“行”
遮罩上三角形(可选)
注意
请参阅以下材料以更好地理解:https://github.com/kimiyoung/transformer-xl/issues/8 https://arxiv.org/pdf/1901.02860.pdf (附录 B)
- Arguments:
x (
torch.Tensor): 输入张量,形状为 (cur_seq, full_seq, bs, head_num)。zero_upper (
bool): 如果为True,矩阵的右上三角形将被设置为零。
- Returns:
x (
torch.Tensor): 相对位移操作后的输入张量,形状为 (cur_seq, full_seq, bs, head_num)。
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(inputs: Tensor, pos_embedding: Tensor, full_input: Tensor, u: Parameter, v: Parameter, mask: Tensor | None = None) Tensor[source]¶
- Overview:
计算AttentionXL模块的前向传递。
- Arguments:
输入 (
torch.Tensor): 注意力输入,形状为 (cur_seq, bs, input_dim)。pos_embedding (
torch.Tensor): 位置嵌入,形状为 (full_seq, 1, full_seq)。full_input (
torch.Tensor): 连接的内存和输入张量,形状为 (full_seq, bs, input_dim)。u (
torch.nn.Parameter): 内容参数,形状为 (head_num, head_dim)。v (
torch.nn.Parameter): 形状为 (head_num, head_dim) 的位置参数。mask (
Optional[torch.Tensor]): 注意力掩码,形状为 (cur_seq, full_seq, 1)。如果为 None,则不应用掩码。
- Returns:
输出 (
torch.Tensor): 注意力机制的输出,形状为 (cur_seq, bs, input_dim)。
- training: bool¶
GatedTransformerXLLayer¶
- class ding.torch_utils.network.gtrxl.GatedTransformerXLLayer(input_dim: int, head_dim: int, hidden_dim: int, head_num: int, mlp_num: int, dropout: Module, activation: Module, gru_gating: bool = True, gru_bias: float = 2.0)[source]¶
- Overview:
该类实现了GTrXL(门控Transformer-XL)的注意力层。
- Interfaces:
__init__,forward
- __init__(input_dim: int, head_dim: int, hidden_dim: int, head_num: int, mlp_num: int, dropout: Module, activation: Module, gru_gating: bool = True, gru_bias: float = 2.0) None[源代码]¶
- Overview:
初始化GatedTransformerXLLayer。
- Arguments:
input_dim (
int): 输入张量的维度。head_dim (
int): 多头注意力中每个头的维度。hidden_dim (
int): MLP中隐藏层的维度。head_num (
int): 多头注意力机制中的头数。mlp_num (
int): 注意力层中MLP层的数量。dropout (
nn.Module): 用于MLP和注意力层的dropout模块。activation (
nn.Module): 在MLP层中使用的激活函数。gru_gating (
bool, 可选): 是否使用GRU门。如果为False,则用残差连接替换GRU门。默认为True。gru_bias (
float, 可选): GRU门的偏置。默认值为2。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(inputs: Tensor, pos_embedding: Tensor, u: Parameter, v: Parameter, memory: Tensor, mask: Tensor | None = None) Tensor[source]¶
- Overview:
计算GTrXL层的前向传递。
- Arguments:
输入 (
torch.Tensor): 注意力输入张量的形状为 (cur_seq, bs, input_dim)。pos_embedding (
torch.Tensor): 位置嵌入张量,形状为 (full_seq, 1, full_seq)。u (
torch.nn.Parameter): 形状为 (head_num, head_dim) 的内容参数张量。v (
torch.nn.Parameter): 形状为 (head_num, head_dim) 的位置参数张量。memory (
torch.Tensor): 形状为 (prev_seq, bs, input_dim) 的内存张量。- mask (
Optional[torch.Tensor]): The attention mask tensor of shape (cur_seq, full_seq, 1). 默认值为None。
- mask (
- Returns:
输出 (
torch.Tensor): 形状为 (cur_seq, bs, input_dim) 的层输出
- training: bool¶
GTrXL¶
- class ding.torch_utils.network.gtrxl.GTrXL(input_dim: int, head_dim: int = 128, embedding_dim: int = 256, head_num: int = 2, mlp_num: int = 2, layer_num: int = 3, memory_len: int = 64, dropout_ratio: float = 0.0, activation: Module = ReLU(), gru_gating: bool = True, gru_bias: float = 2.0, use_embedding_layer: bool = True)[源代码]¶
- Overview:
GTrXL Transformer 实现,如“Stabilizing Transformer for Reinforcement Learning”中所述 (https://arxiv.org/abs/1910.06764)。
- Interfaces:
__init__,forward,reset_memory,get_memory
- __init__(input_dim: int, head_dim: int = 128, embedding_dim: int = 256, head_num: int = 2, mlp_num: int = 2, layer_num: int = 3, memory_len: int = 64, dropout_ratio: float = 0.0, activation: Module = ReLU(), gru_gating: bool = True, gru_bias: float = 2.0, use_embedding_layer: bool = True) None[source]¶
- Overview:
初始化GTrXL模型。
- Arguments:
input_dim (
int): 输入观测的维度。head_dim (
int, 可选): 每个头的维度。默认值为128。embedding_dim (
int, 可选): 嵌入的维度。默认值为256。head_num (
int, 可选): 多头注意力机制中的头数。默认值为2。mlp_num (
int, 可选): 注意力层中MLP层的数量。默认值为2。层数 (
int, 可选): transformer层的数量。默认值为3。memory_len (
int, 可选): 内存的长度。默认值为64。dropout_ratio (
float, 可选): 丢弃率。默认值为0。activation (
nn.Module, 可选): 激活函数。默认为 nn.ReLU()。gru_gating (
bool, 可选): 如果为False,用残差连接替换GRU门。默认值为True。gru_bias (
float, 可选): GRU门的偏置。默认值为2.0。use_embedding_layer (
bool, 可选): 如果为False,则不使用输入嵌入层。默认为True。
- Raises:
AssertionError: 如果 embedding_dim 不是偶数。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor, batch_first: bool = False, return_mem: bool = True) Dict[str, Tensor][source]¶
- Overview:
在GTrXL上执行前向传递。
- Arguments:
x (
torch.Tensor): 输入张量,形状为 (seq_len, bs, input_size)。batch_first (
bool, 可选): 如果输入数据的形状为 (bs, seq_len, input_size),将此参数设置为 True 以沿第一和第二维度进行转置,并获得形状 (seq_len, bs, input_size)。这不会影响输出内存。默认值为 False。 - return_mem (bool, 可选): 如果为 False,则仅返回输出张量而不返回字典。默认值为 True。
- Returns:
x (
Dict[str, torch.Tensor]): 一个包含形状为 (seq_len, bs, embedding_size) 的transformer输出和形状为 (layer_num, seq_len, bs, embedding_size) 的memory的字典。
- get_memory()[source]¶
- Overview:
返回GTrXL的内存。
- Returns:
memory (
Optional[torch.Tensor]): 输出的memory,如果memory尚未初始化则为None。形状为(layer_num, memory_len, bs, embedding_dim)。
- reset_memory(batch_size: int | None = None, state: Tensor | None = None)[source]¶
- Overview:
清除或设置GTrXL的内存。
- Arguments:
batch_size (
Optional[int]): 批量大小。默认值为 None。状态 (
Optional[torch.Tensor]): 输入的记忆,形状为 (层数, 记忆长度, 批次大小, 嵌入维度)。默认值为 None。
- training: bool¶
network.gumbel_softmax¶
请参考 ding/torch_utils/network/gumbel_softmax 获取更多详细信息。
GumbelSoftmax¶
- class ding.torch_utils.network.gumbel_softmax.GumbelSoftmax[源代码]¶
- Overview:
一个计算GumbelSoftmax的nn.Module。
- Interfaces:
__init__,forward,gumbel_softmax_sample
注意
有关GumbelSoftmax的更多信息,请参阅论文[Categorical Reparameterization with Gumbel-Softmax](https://arxiv.org/abs/1611.01144)。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor, temperature: float = 1.0, hard: bool = False) Tensor[来源]¶
- Overview:
为GumbelSoftmax模块进行前向传递。
- Arguments:
x (
torch.Tensor): 未归一化的对数概率。温度 (
float): 控制分布锐度的非负标量。hard (
bool): 如果为True,则返回独热编码的标签。默认为False。
- Returns:
输出 (
torch.Tensor): 来自Gumbel-Softmax分布的样本。
- Shapes:
x: 它的形状是 \((B, N)\),其中 B 是批量大小,N 是类别数量。
y: 它的形状是 \((B, N)\),其中 B 是批量大小,N 是类别数量。
- gumbel_softmax_sample(x: Tensor, temperature: float, eps: float = 1e-08) Tensor[source]¶
- Overview:
从Gumbel-Softmax分布中抽取一个样本。
- Arguments:
x (
torch.Tensor): 输入张量。温度 (
float): 控制分布锐度的非负标量。eps (
float): 防止除以零的小数,默认值为 1e-8。
- Returns:
输出 (
torch.Tensor): 来自Gumbel-Softmax分布的样本。
- training: bool¶
network.merge¶
请参考 ding/torch_utils/network/merge 获取更多详细信息。
双线性通用¶
- class ding.torch_utils.network.merge.BilinearGeneral(in1_features: int, in2_features: int, out_features: int)[来源]¶
- Overview:
双线性实现如:乘法交互及其应用场景, ICLR 2020, https://openreview.net/forum?id=rylnK6VtDH.
- Interfaces:
__init__,forward
- __init__(in1_features: int, in2_features: int, out_features: int)[source]¶
- Overview:
初始化双线性层。
- Arguments:
in1_features (
int): 每个第一个输入样本的大小。in2_features (
int): 每个第二个输入样本的大小。out_features (
int): 每个输出样本的大小。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor, z: Tensor)[source]¶
- Overview:
计算双线性函数。
- Arguments:
x (
torch.Tensor): 第一个输入张量。z (
torch.Tensor): 第二个输入张量。
- training: bool¶
TorchBilinearCustomized¶
- class ding.torch_utils.network.merge.TorchBilinearCustomized(in1_features: int, in2_features: int, out_features: int)[source]¶
- Overview:
定制的Torch双线性实现。
- Interfaces:
__init__,forward
- __init__(in1_features: int, in2_features: int, out_features: int)[source]¶
- Overview:
初始化双线性层。
- Arguments:
in1_features (
int): 每个第一个输入样本的大小。in2_features (
int): 每个第二个输入样本的大小。out_features (
int): 每个输出样本的大小。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x, z)[source]¶
- Overview:
计算双线性函数。
- Arguments:
x (
torch.Tensor): 第一个输入张量。z (
torch.Tensor): 第二个输入张量。
- training: bool¶
FiLM¶
- class ding.torch_utils.network.merge.FiLM(feature_dim: int, context_dim: int)[源代码]¶
- Overview:
特征线性调制(FiLM)层。 该层基于上下文应用特征仿射变换。
- Interfaces:
__init__,forward
- __init__(feature_dim: int, context_dim: int)[源代码]¶
- Overview:
初始化FiLM层。
- Arguments:
feature_dim (
int). 输入特征向量的维度。context_dim (
int). 输入上下文向量的维度。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(feature: Tensor, context: Tensor)[source]¶
- Overview:
前向传播。
- Arguments:
特征 (
torch.Tensor). 输入特征,形状为 (batch_size, feature_dim).context (
torch.Tensor). 输入上下文,形状为 (batch_size, context_dim)。
- Returns:
conditioned_feature : torch.Tensor. FiLM 后的输出特征,形状为 (batch_size, feature_dim)。
- training: bool¶
门控类型¶
SumMerge¶
- class ding.torch_utils.network.merge.SumMerge(*args, **kwargs)[source]¶
- Overview:
一个PyTorch模块,通过计算它们的和来合并一系列张量。所有输入张量必须具有相同的大小。该模块可以处理任何类型的张量(向量、单位或视觉)。
- Interfaces:
__init__,forward
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(tensors: List[Tensor]) Tensor[源代码]¶
- Overview:
SumMerge模块的前向传递,它对输入张量进行求和。
- Arguments:
张量 (
List[Tensor]): 要相加的输入张量列表。所有张量必须具有相同的大小。
- Returns:
求和后的 (
Tensor): 所有输入张量求和后得到的张量。
- training: bool¶
向量合并¶
- class ding.torch_utils.network.merge.VectorMerge(input_sizes: Dict[str, int], output_size: int, gating_type: GatingType = GatingType.NONE, use_layer_norm: bool = True)[源代码]¶
- Overview:
合并多个向量流。流首先通过层归一化、relu和线性层进行转换,然后相加。它们不需要具有相同的大小。在求和之前也可以使用门控。
- Interfaces:
__init__,encode,_compute_gate,forward
注意
有关门控类型的更多详细信息,请参考 GatingType 枚举类。
- __init__(input_sizes: Dict[str, int], output_size: int, gating_type: GatingType = GatingType.NONE, use_layer_norm: bool = True)[source]¶
- Overview:
初始化VectorMerge模块。
- Arguments:
input_sizes (
Dict[str, int]): 一个将输入名称映射到其大小的字典。 对于1D输入,大小是一个整数,对于0D输入,大小是None。 如果输入大小为None,我们假设它是()。output_size (
int): 输出向量的大小。gating_type (
GatingType): 使用的门控机制类型。默认是 GatingType.NONE。use_layer_norm (
bool): 是否使用层归一化。默认值为 True。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _compute_gate(init_gate: List[Tensor]) List[Tensor][source]¶
- Overview:
根据初始门值计算门值。
- Arguments:
init_gate (
List[Tensor]): 初始门值。
- Returns:
gate (
List[Tensor]): 计算得到的门值。
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- encode(inputs: Dict[str, Tensor]) Tuple[List[Tensor], List[Tensor]][source]¶
- Overview:
使用层归一化、relu和线性变换对输入张量进行编码。
- Arguments:
输入 (
Dict[str, Tensor]): 输入张量。
- Returns:
gates (
List[Tensor]): 转换后的门张量。输出 (
List[Tensor]): 转换后的输出张量。
- forward(inputs: Dict[str, Tensor]) Tensor[source]¶
- Overview:
通过VectorMerge模块的前向传递。
- Arguments:
输入 (
Dict[str, Tensor]): 输入张量。
- Returns:
输出 (
Tensor): 通过模块后的输出张量。
- training: bool¶
network.nn_module¶
请参考 ding/torch_utils/network/nn_module 获取更多详细信息。
weight_init¶
- ding.torch_utils.network.nn_module.weight_init_(weight: Tensor, init_type: str = 'xavier', activation: str | None = None) None[源代码]¶
- Overview:
根据指定的类型初始化权重。
- Arguments:
权重 (
torch.Tensor): 需要初始化的权重。init_type (
str, 可选): 实现的初始化类型,支持 [“xavier”, “kaiming”, “orthogonal”]。activation (
str, 可选): 激活函数名称。建议仅使用 ['relu', 'leaky_relu']。
sequential_pack¶
conv1d_block¶
- ding.torch_utils.network.nn_module.conv1d_block(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, dilation: int = 1, groups: int = 1, activation: Module | None = None, norm_type: str | None = None) Sequential[source]¶
- Overview:
创建一个带有激活和归一化的一维卷积层。
- Arguments:
in_channels (
int): 输入张量中的通道数。out_channels (
int): 输出张量中的通道数。kernel_size (
int): 卷积核的大小。步幅 (
int, 可选): 卷积的步幅。默认值为1。填充 (
int, 可选): 添加到输入两侧的零填充。默认值为0。dilation (
int, 可选): 内核元素之间的间距。默认值为1。groups (
int, 可选): 从输入通道到输出通道的阻塞连接数。默认值为1。activation (
nn.Module, 可选): 可选的激活函数。norm_type (
str, 可选): 归一化的类型。
- Returns:
block (
nn.Sequential): 一个包含一维卷积层的torch层的顺序列表。
conv2d_block¶
- ding.torch_utils.network.nn_module.conv2d_block(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, dilation: int = 1, groups: int = 1, pad_type: str = 'zero', activation: Module | None = None, norm_type: str | None = None, num_groups_for_gn: int = 1, bias: bool = True) Sequential[来源]¶
- Overview:
创建一个带有激活和归一化的二维卷积层。
- Arguments:
in_channels (
int): 输入张量中的通道数。out_channels (
int): 输出张量中的通道数。kernel_size (
int): 卷积核的大小。步幅 (
int, 可选): 卷积的步幅。默认值为1。填充 (
int, 可选): 添加到输入两侧的零填充。默认值为0。dilation (
int): 内核元素之间的间距。groups (
int, 可选): 从输入通道到输出通道的阻塞连接数。默认值为1。pad_type (
str, 可选): 添加填充的方式,包括 [‘zero’, ‘reflect’, ‘replicate’]。默认是 ‘zero’。activation (
nn.Module): 可选的激活函数。norm_type (
str): 归一化的类型,目前支持 ['BN', 'LN', 'IN', 'GN', 'SyncBN'],默认设置为 None,表示不进行归一化。num_groups_for_gn (
int): GroupNorm的组数。偏差 (
bool): 是否向 nn.Conv2d 添加可学习的偏差。默认值为 True。
- Returns:
block (
nn.Sequential): 一个包含二维卷积层的torch层的顺序列表。
反卷积2d块¶
- ding.torch_utils.network.nn_module.deconv2d_block(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, output_padding: int = 0, groups: int = 1, activation: int | None = None, norm_type: int | None = None) Sequential[source]¶
- Overview:
创建一个带有激活和归一化的二维转置卷积层。
- Arguments:
in_channels (
int): 输入张量中的通道数。out_channels (
int): 输出张量中的通道数。kernel_size (
int): 卷积核的大小。步幅 (
int, 可选): 卷积的步幅。默认值为1。填充 (
int, 可选): 添加到输入两侧的零填充。默认值为0。output_padding (
int, 可选): 添加到输出形状一侧的额外大小。默认值为0。groups (
int, 可选): 从输入通道到输出通道的阻塞连接数。默认值为1。activation (
int, 可选): 可选的激活函数。norm_type (
int, 可选): 归一化的类型。
- Returns:
block (
nn.Sequential): 一个包含二维转置卷积层的torch层的顺序列表。
注意
ConvTranspose2d (https://pytorch.org/docs/master/generated/torch.nn.ConvTranspose2d.html)
fc_block¶
- ding.torch_utils.network.nn_module.fc_block(in_channels: int, out_channels: int, activation: Module | None = None, norm_type: str | None = None, use_dropout: bool = False, dropout_probability: float = 0.5) Sequential[来源]¶
- Overview:
创建一个具有激活、归一化和dropout的全连接块。 可选的归一化可以在维度1(跨通道)上进行。 x -> fc -> norm -> act -> dropout -> out
- Arguments:
in_channels (
int): 输入张量中的通道数。out_channels (
int): 输出张量中的通道数。activation (
nn.Module, 可选): 可选的激活函数。norm_type (
str, 可选): 归一化的类型。use_dropout (
bool, 可选): 是否在全连接块中使用dropout。默认值为False。dropout_probability (
float, 可选): 在dropout中元素被置零的概率。默认值为0.5。
- Returns:
块 (
nn.Sequential): 一个包含全连接块的torch层的顺序列表。
注意
你可以参考 nn.linear (https://pytorch.org/docs/master/generated/torch.nn.Linear.html).
归一化线性¶
- ding.torch_utils.network.nn_module.normed_linear(in_features: int, out_features: int, bias: bool = True, device=None, dtype=None, scale: float = 1.0) Linear[source]¶
- Overview:
创建一个 nn.Linear 模块,但使用归一化的 fan-in 初始化。
- Arguments:
in_features (
int): 输入张量中的特征数量。out_features (
int): 输出张量中的特征数量。偏差 (
bool, 可选): 是否向 nn.Linear 添加可学习的偏差。默认值为 True。设备 (
torch.device, 可选): 创建模块所在的设备。默认值为 None。dtype (
torch.dtype, 可选): 创建模块的期望数据类型。默认为 None。scale (
float, 可选): 初始化的比例因子。默认值为1.0。
- Returns:
输出 (
nn.Linear): 一个带有归一化fan-in初始化的nn.Linear模块。
归一化卷积2d¶
- ding.torch_utils.network.nn_module.normed_conv2d(in_channels: int, out_channels: int, kernel_size: int | Tuple[int, int], stride: int | Tuple[int, int] = 1, padding: int | Tuple[int, int] = 0, dilation: int | Tuple[int, int] = 1, groups: int = 1, bias: bool = True, padding_mode: str = 'zeros', device=None, dtype=None, scale: float = 1) Conv2d[source]¶
- Overview:
创建一个 nn.Conv2d 模块,但使用归一化的 fan-in 初始化。
- Arguments:
in_channels (
int): 输入张量中的通道数。out_channels (
int): 输出张量中的通道数。kernel_size (
Union[int, Tuple[int, int]]): 卷积核的大小。步幅 (
Union[int, Tuple[int, int]], 可选): 卷积的步幅。默认为1。填充 (
Union[int, Tuple[int, int]], 可选): 添加到输入两侧的零填充。默认值为0。dilation (:Union[int, Tuple[int, int]], 可选): 内核元素之间的间距。默认值为1。
groups (
int, 可选): 从输入通道到输出通道的阻塞连接数。默认值为1。偏差 (
bool, 可选): 是否向 nn.Conv2d 添加可学习的偏差。默认值为 True。padding_mode (
str, 可选): 使用的填充算法类型。默认为‘zeros’。设备 (
torch.device, 可选): 创建模块所在的设备。默认值为 None。dtype (
torch.dtype, 可选): 创建模块的期望数据类型。默认为 None。scale (
float, 可选): 初始化的比例因子。默认值为1。
- Returns:
输出 (
nn.Conv2d): 一个具有归一化fan-in初始化的nn.Conv2d模块。
多层感知器¶
- ding.torch_utils.network.nn_module.MLP(in_channels: int, hidden_channels: int, out_channels: int, layer_num: int, layer_fn: Callable | None = None, activation: Module | None = None, norm_type: str | None = None, use_dropout: bool = False, dropout_probability: float = 0.5, output_activation: bool = True, output_norm: bool = True, last_linear_layer_init_zero: bool = False)[源代码]¶
- Overview:
使用带有激活、归一化和dropout的全连接块创建一个多层感知器, 可以对维度1(跨通道)进行可选的归一化。 x -> fc -> norm -> act -> dropout -> out
- Arguments:
in_channels (
int): 输入张量中的通道数。hidden_channels (
int): 隐藏张量中的通道数。out_channels (
int): 输出张量中的通道数。层数 (
int): 层数。layer_fn (
Callable, 可选): 层函数。activation (
nn.Module, 可选): 可选的激活函数。norm_type (
str, 可选): 归一化的类型。use_dropout (
bool, 可选): 是否在全连接块中使用dropout。默认值为False。dropout_probability (
float, 可选): 在dropout中元素被置零的概率。默认值为0.5。output_activation (
bool, 可选): 是否在输出层使用激活函数。如果为True,我们将使用与前层相同的激活函数。默认为True。output_norm (
bool, 可选): 是否在输出层使用归一化。如果为True,我们将使用与前层相同的归一化。默认值为True。last_linear_layer_init_zero (
bool, 可选): 是否对最后一个线性层(包括w和b)使用零初始化,这可以在开始时提供稳定的零输出,通常用于RL设置中的策略网络。
- Returns:
block (
nn.Sequential): 一个包含多层感知器的torch层的顺序列表。
注意
你可以参考 nn.linear (https://pytorch.org/docs/master/generated/torch.nn.Linear.html).
通道混洗¶
- class ding.torch_utils.network.nn_module.ChannelShuffle(group_num: int)[源代码]¶
- Overview:
对输入张量应用通道洗牌。有关通道洗牌的更多详细信息,请参阅‘ShuffleNet’论文:https://arxiv.org/abs/1707.01083
- Interfaces:
__init__,forward
- __init__(group_num: int) None[源代码]¶
- Overview:
初始化ChannelShuffle类。
- Arguments:
group_num (
int): 要交换的组数。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor) Tensor[来源]¶
- Overview:
通过ChannelShuffle模块的前向传递。
- Arguments:
x (
torch.Tensor): 输入张量。
- Returns:
x (
torch.Tensor): 打乱后的输入张量。
- training: bool¶
one_hot¶
- ding.torch_utils.network.nn_module.one_hot(val: LongTensor, num: int, num_first: bool = False) FloatTensor[source]¶
- Overview:
将torch.LongTensor转换为one-hot编码。这个实现可能比
torch.nn.functional.one_hot稍微快一些。- Arguments:
val (
torch.LongTensor): 每个元素包含要编码的状态,范围应为 [0, num-1]num (
int): 独热编码的状态数量num_first (
bool, 可选): 如果为False,则独热编码将作为最后一个维度添加;否则,它将作为第一个维度添加。默认值为False。
- Returns:
one_hot (
torch.FloatTensor): 独热编码的张量。
- Example:
>>> one_hot(2*torch.ones([2,2]).long(),3) tensor([[[0., 0., 1.], [0., 0., 1.]], [[0., 0., 1.], [0., 0., 1.]]]) >>> one_hot(2*torch.ones([2,2]).long(),3,num_first=True) tensor([[[0., 0.], [1., 0.]], [[0., 1.], [0., 0.]], [[1., 0.], [0., 1.]]])
最近邻上采样¶
- class ding.torch_utils.network.nn_module.NearestUpsample(scale_factor: float | List[float])[source]¶
- Overview:
该模块使用最近邻模式将输入上采样到给定的scale_factor。
- Interfaces:
__init__,forward
- __init__(scale_factor: float | List[float]) None[源代码]¶
- Overview:
初始化 NearestUpsample 类。
- Arguments:
scale_factor (
Union[float, List[float]]): 空间大小的乘数。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor) Tensor[source]¶
- Overview:
返回上采样的输入张量。
- Arguments:
x (
torch.Tensor): 输入张量。
- Returns:
上采样(
torch.Tensor): 上采样后的输入张量。
- training: bool¶
双线性上采样¶
- class ding.torch_utils.network.nn_module.BilinearUpsample(scale_factor: float | List[float])[source]¶
- Overview:
该模块使用双线性模式将输入上采样到给定的scale_factor。
- Interfaces:
__init__,forward
- __init__(scale_factor: float | List[float]) None[源代码]¶
- Overview:
初始化BilinearUpsample类。
- Arguments:
scale_factor (
Union[float, List[float]]): 空间大小的乘数。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor) Tensor[来源]¶
- Overview:
返回上采样的输入张量。
- Arguments:
x (
torch.Tensor): 输入张量。
- Returns:
上采样(
torch.Tensor): 上采样后的输入张量。
- training: bool¶
二进制编码¶
- ding.torch_utils.network.nn_module.binary_encode(y: Tensor, max_val: Tensor) Tensor[源代码]¶
- Overview:
将张量中的元素转换为其二进制表示。
- Arguments:
y (
torch.Tensor): 要转换为其二进制表示的张量。max_val (
torch.Tensor): 张量中元素的最大值。
- Returns:
二进制 (
torch.Tensor): 输入张量的二进制表示。
- Example:
>>> binary_encode(torch.tensor([3,2]),torch.tensor(8)) tensor([[0, 0, 1, 1],[0, 0, 1, 0]])
噪声线性层¶
- class ding.torch_utils.network.nn_module.NoiseLinearLayer(in_channels: int, out_channels: int, sigma0: int = 0.4)[source]¶
- Overview:
这是一个带有随机噪声的线性层。
- Interfaces:
__init__,reset_noise,reset_parameters,forward
- __init__(in_channels: int, out_channels: int, sigma0: int = 0.4) None[来源]¶
- Overview:
初始化 NoiseLinearLayer 类。
- Arguments:
in_channels (
int): 输入张量中的通道数。out_channels (
int): 输出张量中的通道数。sigma0 (
int, 可选): 初始化 NoiseLinearLayer 时的默认噪声量。默认值为 0.4。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor)[source]¶
- Overview:
使用噪声执行前向传递。
- Arguments:
x (
torch.Tensor): 输入张量。
- Returns:
输出 (
torch.Tensor): 带有噪声的输出张量。
- training: bool¶
噪声块¶
- ding.torch_utils.network.nn_module.noise_block(in_channels: int, out_channels: int, activation: str | None = None, norm_type: str | None = None, use_dropout: bool = False, dropout_probability: float = 0.5, sigma0: float = 0.4)[source]¶
- Overview:
创建一个具有激活、归一化和丢弃的全连接噪声层。 可选的归一化可以在维度1(跨通道)上进行。
- Arguments:
in_channels (
int): 输入张量中的通道数。out_channels (
int): 输出张量中的通道数。activation (
str, 可选): 可选的激活函数。默认值为 None。norm_type (
str, 可选): 归一化类型。默认为 None。use_dropout (
bool, 可选): 是否在全连接块中使用 dropout。dropout_probability (
float, 可选): 在dropout中元素被置零的概率。默认值为0.5。sigma0 (
float, 可选): sigma0 是初始化 NoiseLinearLayer 时的默认噪声量。默认值为 0.4。
- Returns:
块 (
nn.Sequential): 一个包含全连接块的torch层的顺序列表。
NaiveFlatten¶
- class ding.torch_utils.network.nn_module.NaiveFlatten(start_dim: int = 1, end_dim: int = -1)[source]¶
- Overview:
该模块是展平操作的一个简单实现。
- Interfaces:
__init__,forward
- __init__(start_dim: int = 1, end_dim: int = -1) None[源代码]¶
- Overview:
初始化 NaiveFlatten 类。
- Arguments:
start_dim (
int, 可选): 第一个要展平的维度。默认值为1。end_dim (
int, 可选): 要展平的最后一个维度。默认值为-1。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor) Tensor[来源]¶
- Overview:
对输入张量执行展平操作。
- Arguments:
x (
torch.Tensor): 输入张量。
- Returns:
输出 (
torch.Tensor): 展平的输出张量。
- training: bool¶
网络归一化¶
请参考 ding/torch_utils/network/normalization 获取更多详细信息。
构建归一化¶
- ding.torch_utils.network.normalization.build_normalization(norm_type: str, dim: int | None = None) Module[源代码]¶
- Overview:
构建相应的归一化模块。对于初学者,可以参考[这篇文章](https://zhuanlan.zhihu.com/p/34879333)了解更多关于批量归一化的信息。
- Arguments:
norm_type (
str): 归一化的类型。目前支持 [‘BN’, ‘LN’, ‘IN’, ‘SyncBN’]。dim (
Optional[int]): 归一化的维度,当 norm_type 为 ['BN', 'IN'] 时适用。
- Returns:
norm_func (
nn.Module): 对应的批量归一化函数。
network.popart¶
请参考 ding/torch_utils/network/popart 获取更多详细信息。
波普艺术¶
- class ding.torch_utils.network.popart.PopArt(input_features: int | None = None, output_features: int | None = None, beta: float = 0.5)[source]¶
- Overview:
一个带有PopArt归一化的线性层。该类实现了一个线性变换,随后是PopArt归一化,这是一种在多任务学习中自动调整每个任务对代理更新贡献的方法,如论文<https://arxiv.org/abs/1809.04474>中所述。
- Interfaces:
__init__,reset_parameters,forward,update_parameters
- __init__(input_features: int | None = None, output_features: int | None = None, beta: float = 0.5) None[source]¶
- Overview:
使用输入特征、输出特征和beta参数初始化类。
- Arguments:
input_features (
Union[int, None]): 每个输入样本的大小。output_features (
Union[int, None]): 每个输出样本的大小。beta (
float): 移动平均的参数。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor) Dict[str, Tensor][source]¶
- Overview:
实现线性层的前向计算并返回该层的输出和归一化输出。
- Arguments:
x (
torch.Tensor): 输入张量,需要进行归一化。
- Returns:
输出 (
Dict[str, torch.Tensor]): 一个包含 'pred' 和 'unnormalized_pred' 的字典。
- training: bool¶
network.res_block¶
请参考 ding/torch_utils/network/res_block 获取更多详细信息。
ResBlock¶
- class ding.torch_utils.network.res_block.ResBlock(in_channels: int, activation: Module = ReLU(), norm_type: str = 'BN', res_type: str = 'basic', bias: bool = True, out_channels: int | None = None)[源代码]¶
- Overview:
- Residual Block with 2D convolution layers, including 3 types:
- basic block:
输入通道:C x -> 3*3*C -> 归一化 -> 激活 -> 3*3*C -> 归一化 -> 激活 -> 输出 __________________________________________/+
- bottleneck block:
x -> 1*1*(1/4*C) -> 归一化 -> 激活 -> 3*3*(1/4*C) -> 归一化 -> 激活 -> 1*1*C -> 归一化 -> 激活 -> 输出 _____________________________________________________________________________/+
- downsample block: used in EfficientZero
输入通道: C x -> 3*3*C -> 归一化 -> 激活 -> 3*3*C -> 归一化 -> 激活 -> 输出 __________________ 3*3*C ____________________/+
注意
您可以参考Deep Residual Learning for Image Recognition了解更多详细信息。
- Interfaces:
__init__,forward
- __init__(in_channels: int, activation: Module = ReLU(), norm_type: str = 'BN', res_type: str = 'basic', bias: bool = True, out_channels: int | None = None) None[source]¶
- Overview:
初始化2D卷积残差块。
- Arguments:
in_channels (
int): 输入张量中的通道数。activation (
nn.Module): 可选的激活函数。norm_type (
str): 归一化类型,默认设置为‘BN’(批量归一化),支持[‘BN’, ‘LN’, ‘IN’, ‘GN’, ‘SyncBN’, None]。res_type (
str): 残差块的类型,支持 [‘basic’, ‘bottleneck’, ‘downsample’]偏差 (
bool): 是否向conv2d_block添加可学习的偏差。默认设置为True。输出通道数 (
int): 输出张量中的通道数,默认设置为 None,这意味着 out_channels = in_channels。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor) Tensor[来源]¶
- Overview:
返回残差块的输出。
- Arguments:
x (
torch.Tensor): 输入张量。
- Returns:
x (
torch.Tensor): resblock 输出张量。
- training: bool¶
ResFCBlock¶
- class ding.torch_utils.network.res_block.ResFCBlock(in_channels: int, activation: Module = ReLU(), norm_type: str = 'BN', dropout: float | None = None)[源代码]¶
- Overview:
具有2个全连接层的残差块。 x -> fc1 -> norm -> act -> fc2 -> norm -> act -> out _____________________________________/+
- Interfaces:
__init__,forward
- __init__(in_channels: int, activation: Module = ReLU(), norm_type: str = 'BN', dropout: float | None = None)[源代码]¶
- Overview:
初始化全连接层残差块。
- Arguments:
in_channels (
int): 输入张量中的通道数。activation (
nn.Module): 可选的激活函数。norm_type (
str): 归一化的类型,默认设置为‘BN’。dropout (
float): 丢弃率,默认设置为None。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor) Tensor[来源]¶
- Overview:
返回残差块的输出。
- Arguments:
x (
torch.Tensor): 输入张量。
- Returns:
x (
torch.Tensor): 残差块的输出张量。
- training: bool¶
network.resnet¶
请参考 ding/torch_utils/network/resnet 获取更多详细信息。
to_2tuple¶
获取相同填充¶
pad_same¶
- ding.torch_utils.network.resnet.pad_same(x, k: List[int], s: List[int], d: List[int] = (1, 1), value: float = 0)[源代码]¶
- Overview:
使用指定的参数为卷积动态填充输入x,采用'SAME'填充方式。
- Arguments:
x (
Tensor): 输入张量。k (
List[int]): 内核的大小。s (
List[int]): 卷积的步幅。d (
List[int]): 卷积的膨胀。值 (
float): 用于填充的值。
- Returns:
(
Tensor): 填充后的张量。
avg_pool2d_same¶
- ding.torch_utils.network.resnet.avg_pool2d_same(x, kernel_size: List[int], stride: List[int], padding: List[int] = (0, 0), ceil_mode: bool = False, count_include_pad: bool = True)[source]¶
- Overview:
在输入张量上应用带有‘SAME’填充的平均池化。
- Arguments:
x (
Tensor): 输入张量。kernel_size (
List[int]): 内核的大小。步幅 (
List[int]): 卷积的步幅。填充 (
List[int]): 填充的大小。ceil_mode (
bool): 当为True时,将使用ceil而不是floor来计算输出形状。count_include_pad (
bool): 当为True时,将在平均计算中包括零填充。
- Returns:
(
Tensor): 平均池化后的张量。
AvgPool2dSame¶
- class ding.torch_utils.network.resnet.AvgPool2dSame(kernel_size: int, stride: Tuple[int, int] | None = None, padding: int = 0, ceil_mode: bool = False, count_include_pad: bool = True)[source]¶
- Overview:
用于2D平均池化的类似Tensorflow的'SAME'包装器。
- Interfaces:
__init__,forward
- __init__(kernel_size: int, stride: Tuple[int, int] | None = None, padding: int = 0, ceil_mode: bool = False, count_include_pad: bool = True) None[source]¶
- Overview:
使用给定的参数初始化AvgPool2dSame。
- Arguments:
kernel_size (
int): 用于计算平均值的窗口大小。步幅 (
Optional[Tuple[int, int]]): 窗口的步幅。如果为None,则默认为kernel_size。填充 (
int): 在两侧添加的隐式零填充。ceil_mode (
bool): 当为True时,将使用ceil而不是floor来计算输出形状。count_include_pad (
bool): 当为True时,将在平均计算中包括零填充。
- ceil_mode: bool¶
- count_include_pad: bool¶
- forward(x: Tensor) Tensor[source]¶
- Overview:
AvgPool2dSame的前向传递。
- Argument:
x (
torch.Tensor): 输入张量。
- Returns:
(
torch.Tensor): 平均池化后的输出张量。
- kernel_size: int | Tuple[int, int]¶
- padding: int | Tuple[int, int]¶
- stride: int | Tuple[int, int]¶
create_classifier¶
- ding.torch_utils.network.resnet.create_classifier(num_features: int, num_classes: int, pool_type: str = 'avg', use_conv: bool = False) Tuple[Module, Module][source]¶
- Overview:
创建一个带有全局池化层和全连接层的分类器。
- Arguments:
num_features (
int): 特征的数量。num_classes (
int): 最终分类的类别数量。pool_type (
str): 使用的池化类型;'avg' 表示平均池化。use_conv (
bool): 是否使用卷积。
- Returns:
global_pool (
nn.Module): 创建的全局池化层。fc (
nn.Module): 创建的完全连接层。
分类器头¶
- class ding.torch_utils.network.resnet.ClassifierHead(in_chs: int, num_classes: int, pool_type: str = 'avg', drop_rate: float = 0.0, use_conv: bool = False)[来源]¶
- Overview:
具有可配置全局池化和dropout的分类器头。
- Interfaces:
__init__,forward
- __init__(in_chs: int, num_classes: int, pool_type: str = 'avg', drop_rate: float = 0.0, use_conv: bool = False) None[源代码]¶
- Overview:
使用给定的参数初始化ClassifierHead。
- Arguments:
in_chs (
int): 输入通道的数量。num_classes (
int): 最终分类的类别数量。pool_type (
str): 使用的池化类型;'avg' 表示平均池化。drop_rate (
float): 丢弃率。use_conv (
bool): 是否使用卷积。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor) Tensor[source]¶
- Overview:
ClassifierHead的前向传递。
- Argument:
x (
torch.Tensor): 输入张量。
- Returns:
(
torch.Tensor): 分类后的输出张量。
- training: bool¶
create_attn¶
获取填充¶
基本块¶
- class ding.torch_utils.network.resnet.BasicBlock(inplanes: int, planes: int, stride: int = 1, downsample: ~typing.Callable | None = None, cardinality: int = 1, base_width: int = 64, reduce_first: int = 1, dilation: int = 1, first_dilation: int | None = None, act_layer: ~typing.Callable = <class 'torch.nn.modules.activation.ReLU'>, norm_layer: ~typing.Callable = <class 'torch.nn.modules.batchnorm.BatchNorm2d'>, attn_layer: ~typing.Callable | None = None, aa_layer: ~typing.Callable | None = None, drop_block: ~typing.Callable | None = None, drop_path: ~typing.Callable | None = None)[来源]¶
- Overview:
这是像ResNet这样的模型的基本构建块。这个类扩展了pytorch的Module类。 它代表了一个标准的层块,包括两个卷积、批量归一化、 一个可选的注意力机制和激活函数。
- Interfaces:
__init__,forward,zero_init_last_bn- Properties:
expansion (:obj:int): 指定卷积层平面的扩展因子。
- __init__(inplanes: int, planes: int, stride: int = 1, downsample: ~typing.Callable | None = None, cardinality: int = 1, base_width: int = 64, reduce_first: int = 1, dilation: int = 1, first_dilation: int | None = None, act_layer: ~typing.Callable = <class 'torch.nn.modules.activation.ReLU'>, norm_layer: ~typing.Callable = <class 'torch.nn.modules.batchnorm.BatchNorm2d'>, attn_layer: ~typing.Callable | None = None, aa_layer: ~typing.Callable | None = None, drop_block: ~typing.Callable | None = None, drop_path: ~typing.Callable | None = None) None[source]¶
- Overview:
使用给定的参数初始化BasicBlock。
- Arguments:
输入通道数 (
int): 输入通道的数量。planes (
int): 输出通道的数量。步幅 (
int): 卷积层的步幅。下采样 (
Callable): 用于对输入进行下采样的函数。基数 (
int): 分组卷积的组大小。base_width (
int): 卷积的基础宽度。reduce_first (
int): 每个块的第一个卷积的缩减因子。膨胀 (
int): 内核点之间的间距。first_dilation (
int): 第一个扩张值。act_layer (
Callable): 激活层的函数。norm_layer (
Callable): 用于归一化层的函数。attn_layer (
Callable): 用于注意力层的函数。aa_layer (
Callable): 用于抗锯齿层的函数。drop_block (
Callable): 用于删除块的方法。drop_path (
Callable): 用于删除路径的方法。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- expansion = 1¶
- forward(x: Tensor) Tensor[源代码]¶
- Overview:
定义每次调用时执行的计算。
- Arguments:
x (
torch.Tensor): 输入张量。
- Returns:
输出 (
torch.Tensor): 通过 BasicBlock 后的输出张量。
- training: bool¶
瓶颈¶
- class ding.torch_utils.network.resnet.Bottleneck(inplanes: int, planes: int, stride: int = 1, downsample: ~torch.nn.modules.module.Module | None = None, cardinality: int = 1, base_width: int = 64, reduce_first: int = 1, dilation: int = 1, first_dilation: int | None = None, act_layer: ~typing.Type[~torch.nn.modules.module.Module] = <class 'torch.nn.modules.activation.ReLU'>, norm_layer: ~typing.Type[~torch.nn.modules.module.Module] = <class 'torch.nn.modules.batchnorm.BatchNorm2d'>, attn_layer: ~typing.Type[~torch.nn.modules.module.Module] | None = None, aa_layer: ~typing.Type[~torch.nn.modules.module.Module] | None = None, drop_block: ~typing.Callable | None = None, drop_path: ~typing.Callable | None = None)[源代码]¶
- Overview:
Bottleneck 类是用于构建 ResNet 网络的基本模块。它是 PyTorch 中 ResNet 实现的一部分。该模块设计包含多个层,包括卷积层、归一化层、激活层、注意力层、抗锯齿层和 dropout 层。
- Interfaces:
__init__,forward,zero_init_last_bn- Properties:
扩展, 输入平面, 平面, 步幅, 下采样, 基数, 基础宽度, 首先减少, 扩张, 首次扩张, 激活层, 归一化层, 注意力层, AA层, 丢弃块, 丢弃路径
- __init__(inplanes: int, planes: int, stride: int = 1, downsample: ~torch.nn.modules.module.Module | None = None, cardinality: int = 1, base_width: int = 64, reduce_first: int = 1, dilation: int = 1, first_dilation: int | None = None, act_layer: ~typing.Type[~torch.nn.modules.module.Module] = <class 'torch.nn.modules.activation.ReLU'>, norm_layer: ~typing.Type[~torch.nn.modules.module.Module] = <class 'torch.nn.modules.batchnorm.BatchNorm2d'>, attn_layer: ~typing.Type[~torch.nn.modules.module.Module] | None = None, aa_layer: ~typing.Type[~torch.nn.modules.module.Module] | None = None, drop_block: ~typing.Callable | None = None, drop_path: ~typing.Callable | None = None) None[source]¶
- Overview:
使用各种参数初始化Bottleneck类。
- Arguments:
输入平面数 (
int): 输入平面的数量。planes (
int): 输出的平面数量。步幅 (
int, 可选): 步幅大小,默认为1。下采样 (
nn.Module, 可选): 下采样方法,默认为 None。基数 (
int, 可选): 组卷积的大小,默认为1。base_width (
int, 可选): 基础宽度,默认为64。reduce_first (
int, 可选): 第一个缩减因子,默认为1。dilation (
int, 可选): 扩张因子,默认为1。first_dilation (
int, 可选): 第一个扩张因子,默认为 None。act_layer (
Type[nn.Module], 可选): 激活层类型,默认为 nn.ReLU。norm_layer (
Type[nn.Module], 可选): 归一化层类型,默认为 nn.BatchNorm2d。attn_layer (
Type[nn.Module], 可选): 注意力层类型,默认为 None。aa_layer (
Type[nn.Module], 可选): 抗锯齿层类型,默认为 None。drop_block (
Callable): 丢弃块,默认为 None。drop_path (
Callable): 丢弃路径,默认为 None。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- expansion = 4¶
- forward(x: Tensor) Tensor[源代码]¶
- Overview:
定义每次调用时执行的计算。
- Arguments:
x (
Tensor): 输入张量。
- Returns:
x (
Tensor): 计算结果的输出张量。
- training: bool¶
下采样卷积¶
- ding.torch_utils.network.resnet.downsample_conv(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, dilation: int = 1, first_dilation: int | None = None, norm_layer: Type[Module] | None = None) Sequential[source]¶
- Overview:
创建一个用于下采样的顺序模块,其中包括一个卷积层和一个归一化层。
- Arguments:
in_channels (
int): 输入通道的数量。out_channels (
int): 输出通道的数量。kernel_size (
int): 内核的大小。步幅 (
int, 可选): 步幅大小,默认为1。dilation (
int, 可选): 扩张因子,默认为1。first_dilation (
int, 可选): 第一个扩张因子,默认为 None。norm_layer (
Type[nn.Module], 可选): 归一化层类型,默认为 nn.BatchNorm2d。
- Returns:
nn.Sequential: 通过卷积执行下采样的一系列层。
downsample_avg¶
- ding.torch_utils.network.resnet.downsample_avg(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, dilation: int = 1, first_dilation: int | None = None, norm_layer: Type[Module] | None = None) Sequential[源代码]¶
- Overview:
创建一个用于下采样的顺序模块,其中包括一个平均池化层、一个卷积层和一个归一化层。
- Arguments:
in_channels (
int): 输入通道的数量。输出通道数 (
int): 输出通道的数量。kernel_size (
int): 内核的大小。步幅 (
int, 可选): 步幅大小,默认为1。dilation (
int, 可选): 扩张因子,默认为1。first_dilation (
int, 可选): 第一个扩张因子,默认为 None。norm_layer (
Type[nn.Module], 可选): 归一化层类型,默认为 nn.BatchNorm2d。
- Returns:
nn.Sequential: 通过平均池化进行下采样的一系列层。
drop_blocks¶
make_blocks¶
- ding.torch_utils.network.resnet.make_blocks(block_fn: Type[Module], channels: List[int], block_repeats: List[int], inplanes: int, reduce_first: int = 1, output_stride: int = 32, down_kernel_size: int = 1, avg_down: bool = False, drop_block_rate: float = 0.0, drop_path_rate: float = 0.0, **kwargs) Tuple[List[Tuple[str, Module]], List[Dict[str, int | str]]][source]¶
- Overview:
为网络创建一个块列表,每个块都有给定的重复次数。同时,创建一个特征信息列表,其中包含每个块输出的信息。
- Arguments:
block_fn (
Type[nn.Module]): 要使用的块类型。channels (
List[int]): 每个块的输出通道列表。block_repeats (
List[int]): 每个块的重复次数列表。输入平面数 (
int): 输入平面的数量。reduce_first (
int, 可选): 第一个缩减因子,默认为1。output_stride (
int, 可选): 网络的总步幅,默认为32。down_kernel_size (
int, 可选): 下采样核的大小,默认为1。avg_down (
bool, 可选): 是否使用平均池化进行下采样,默认为 False。drop_block_rate (
float, 可选): 丢弃块率,默认为0。drop_path_rate (
float, 可选): 丢弃路径的比率,默认为0。
- Returns:
Tuple[List[Tuple[str, nn.Module]], List[Dict[str, Union[int, str]]]]: 一个包含网络块列表和特征信息列表的元组。
ResNet¶
- class ding.torch_utils.network.resnet.ResNet(block: ~torch.nn.modules.module.Module, layers: ~typing.List[int], num_classes: int = 1000, in_chans: int = 3, cardinality: int = 1, base_width: int = 64, stem_width: int = 64, stem_type: str = '', replace_stem_pool: bool = False, output_stride: int = 32, block_reduce_first: int = 1, down_kernel_size: int = 1, avg_down: bool = False, act_layer: ~torch.nn.modules.module.Module = <class 'torch.nn.modules.activation.ReLU'>, norm_layer: ~torch.nn.modules.module.Module = <class 'torch.nn.modules.batchnorm.BatchNorm2d'>, aa_layer: ~torch.nn.modules.module.Module | None = None, drop_rate: float = 0.0, drop_path_rate: float = 0.0, drop_block_rate: float = 0.0, global_pool: str = 'avg', zero_init_last_bn: bool = True, block_args: dict | None = None)[源代码]¶
- Overview:
实现了ResNet、ResNeXt、SE-ResNeXt和SENet模型。此实现支持基于MXNet Gluon ResNetV1b模型中包含的v1c、v1d、v1e和v1s变体的各种修改。有关变体和选项的更多详细信息,请参阅“Bag of Tricks”论文:https://arxiv.org/pdf/1812.01187。
- Interfaces:
__init__,forward,zero_init_last_bn,get_classifier
- __init__(block: ~torch.nn.modules.module.Module, layers: ~typing.List[int], num_classes: int = 1000, in_chans: int = 3, cardinality: int = 1, base_width: int = 64, stem_width: int = 64, stem_type: str = '', replace_stem_pool: bool = False, output_stride: int = 32, block_reduce_first: int = 1, down_kernel_size: int = 1, avg_down: bool = False, act_layer: ~torch.nn.modules.module.Module = <class 'torch.nn.modules.activation.ReLU'>, norm_layer: ~torch.nn.modules.module.Module = <class 'torch.nn.modules.batchnorm.BatchNorm2d'>, aa_layer: ~torch.nn.modules.module.Module | None = None, drop_rate: float = 0.0, drop_path_rate: float = 0.0, drop_block_rate: float = 0.0, global_pool: str = 'avg', zero_init_last_bn: bool = True, block_args: dict | None = None) None[源代码]¶
- Overview:
使用给定的块、层和其他配置选项初始化ResNet模型。
- Arguments:
block (
nn.Module): 残差块的类。层数 (
List[int]): 每个块中的层数。num_classes (
int, 可选): 分类类别数。默认为1000。in_chans (
int, 可选): 输入(颜色)通道的数量。默认值为3。基数 (
int, 可选): Bottleneck 中 3x3 卷积的卷积组数。默认为 1。base_width (
int, 可选): 决定瓶颈通道的因子。默认值为64。stem_width (
int, 可选): 在stem卷积中的通道数。默认值为64。stem_type (
str, 可选): 茎的类型。默认为‘’。replace_stem_pool (
bool, 可选): 是否替换词干池化。默认值为 False。output_stride (
int, 可选): 网络的输出步幅。默认值为32。block_reduce_first (
int, 可选): 用于残差块的第一个卷积输出宽度的缩减因子。默认值为1。down_kernel_size (
int, 可选): 残差块下采样路径的核大小。默认值为1。- avg_down (
bool, optional): Whether to use average pooling for projection skip connection between stages/downsample。默认值为False。
- avg_down (
act_layer (
nn.Module, 可选): 激活层。默认为 nn.ReLU。norm_layer (
nn.Module, 可选): 归一化层。默认为 nn.BatchNorm2d。aa_layer (
Optional[nn.Module], 可选): 抗锯齿层。默认值为 None。drop_rate (
float, 可选): 在分类器之前的Dropout概率,用于训练。默认值为0.0。drop_path_rate (
float, 可选): 丢弃路径率。默认值为0.0。drop_block_rate (
float, 可选): 丢弃块率。默认值为0.0。global_pool (
str, 可选): 全局池化类型。默认为 'avg'。zero_init_last_bn (
bool, 可选): 是否用零初始化最后的批量归一化。默认值为 True。block_args (
Optional[dict], 可选): 块的额外参数。默认为 None。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor) Tensor[来源]¶
- Overview:
模型的全前向传递。
- Arguments:
x (
torch.Tensor): 输入张量。
- Returns:
x (
torch.Tensor): 通过模型后的输出张量。
- forward_features(x: Tensor) Tensor[source]¶
- Overview:
通过模型的特征层进行前向传递。
- Arguments:
x (
torch.Tensor): 输入张量。
- Returns:
x (
torch.Tensor): 通过特征层后的输出张量。
- init_weights(zero_init_last_bn: bool = True) None[source]¶
- Overview:
初始化模型中的权重。
- Arguments:
- zero_init_last_bn (
bool, optional): Whether to initialize last batch normalization with zero. 默认值为True。
- zero_init_last_bn (
- reset_classifier(num_classes: int, global_pool: str = 'avg') None[source]¶
- Overview:
使用新的类别数量和池化类型重置分类器。
- Arguments:
num_classes (
int): 新的分类类别数。global_pool (
str, 可选): 新的全局池化类型。默认为‘avg’。
- training: bool¶
resnet18¶
network.rnn¶
请参考 ding/torch_utils/network/rnn 获取更多详细信息。
is_sequence¶
sequence_mask¶
- ding.torch_utils.network.rnn.sequence_mask(lengths: Tensor, max_len: int | None = None) BoolTensor[来源]¶
- Overview:
为一批长度不同的序列生成布尔掩码。
- Arguments:
长度 (
torch.Tensor): 一个包含每个序列长度的张量。形状可以是 (n, 1) 或 (n)。max_len (
int, 可选): 填充大小。如果 max_len 为 None,填充大小是序列的最大长度。
- Returns:
masks (
torch.BoolTensor): 一个布尔掩码张量。掩码与lengths具有相同的设备。
LSTM前向包装器¶
- class ding.torch_utils.network.rnn.LSTMForwardWrapper[源代码]¶
- Overview:
提供在LSTM forward方法前后使用的方法的类。 封装了LSTM forward方法。
- Interfaces:
_before_forward,_after_forward
- _after_forward(next_state: Tuple[Tensor], list_next_state: bool = False) List[Dict] | Dict[str, Tensor][source]¶
- Overview:
在LSTM forward 方法之后对next_state进行后处理。
- Arguments:
next_state (
Tuple[torch.Tensor]): 包含下一个状态 (h, c) 的元组。list_next_state (
bool, 可选): 确定返回的next_state的格式。如果为True,则以列表格式返回next_state。默认为False。
- Returns:
next_state(
Union[List[Dict], Dict[str, torch.Tensor]]): 经过处理后的下一个状态。
- _before_forward(inputs: Tensor, prev_state: None | List[Dict]) Tensor[来源]¶
- Overview:
在LSTM forward 方法之前预处理输入和先前的状态。
- Arguments:
输入 (
torch.Tensor): LSTM单元的输入向量。形状: [seq_len, batch_size, input_size]prev_state (
Union[None, List[Dict]]): 前一个状态张量。形状:[num_directions*num_layers, batch_size, hidden_size]。如果为None,prv_state将被初始化为全零。
- Returns:
prev_state (
torch.Tensor): LSTM批次的预处理前一个状态。
LSTM¶
- class ding.torch_utils.network.rnn.LSTM(input_size: int, hidden_size: int, num_layers: int, norm_type: str | None = None, dropout: float = 0.0)[来源]¶
- Overview:
实现一个带有层归一化(LN)的LSTM单元。
- Interfaces:
__init__,forward
注意
关于LSTM的入门,请参考https://zhuanlan.zhihu.com/p/32085405。
- __init__(input_size: int, hidden_size: int, num_layers: int, norm_type: str | None = None, dropout: float = 0.0) None[source]¶
- Overview:
初始化LSTM单元参数。
- Arguments:
input_size (
int): 输入向量的大小。hidden_size (
int): 隐藏状态向量的大小。num_layers (
int): LSTM层数。norm_type (
Optional[str]): 归一化类型,默认为 None。dropout (
float): 丢弃率,默认为0。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(inputs: Tensor, prev_state: Tensor, list_next_state: bool = True) Tuple[Tensor, Tensor | list][source]¶
- Overview:
根据前一个状态和输入计算输出和下一个状态。
- Arguments:
输入 (
torch.Tensor): 单元的输入向量,大小为 [seq_len, batch_size, input_size]。prev_state (
torch.Tensor): 前一个状态,大小为 [num_directions*num_layers, batch_size, hidden_size]。list_next_state (
bool): 是否以列表格式返回next_state,默认为True。
- Returns:
x (
torch.Tensor): LSTM的输出。next_state (
Union[torch.Tensor, list]): LSTM的隐藏状态。
- training: bool¶
PytorchLSTM¶
- class ding.torch_utils.network.rnn.PytorchLSTM(input_size: int, hidden_size: int, num_layers: int = 1, bias: bool = True, batch_first: bool = False, dropout: float = 0.0, bidirectional: bool = False, proj_size: int = 0, device=None, dtype=None)[源代码]¶
- class ding.torch_utils.network.rnn.PytorchLSTM(*args, **kwargs)
- Overview:
PyTorch的nn.LSTM的封装类,用于格式化输入和输出。有关nn.LSTM的更多详细信息,请参阅https://pytorch.org/docs/stable/generated/torch.nn.LSTM.html#torch.nn.LSTM
- Interfaces:
forward
- batch_first: bool¶
- bias: bool¶
- bidirectional: bool¶
- dropout: float¶
- forward(inputs: Tensor, prev_state: Tensor, list_next_state: bool = True) Tuple[Tensor, Tensor | list][source]¶
- Overview:
使用预处理输入执行 nn.LSTM.forward。
- Arguments:
输入 (
torch.Tensor): 单元的输入向量,大小为 [seq_len, batch_size, input_size]。prev_state (
torch.Tensor): 前一个状态,大小为 [num_directions*num_layers, batch_size, hidden_size]。list_next_state (
bool): 是否以列表格式返回next_state,默认为True。
- Returns:
输出 (
torch.Tensor): LSTM的输出。next_state (
Union[torch.Tensor, list]): LSTM的隐藏状态。
- input_size: int¶
- mode: str¶
- num_layers: int¶
- proj_size: int¶
GRU¶
- class ding.torch_utils.network.rnn.GRU(input_size: int, hidden_size: int, num_layers: int)[源代码]¶
- Overview:
这个类扩展了torch.nn.GRUCell和LSTMForwardWrapper类,并相应地格式化输入和输出。
- Interfaces:
__init__,forward- Properties:
隐藏层大小, 层数
注意
有关更多详细信息,请参阅官方 PyTorch 文档: <https://pytorch.org/docs/stable/generated/torch.nn.GRU.html#torch.nn.GRU>
- __init__(input_size: int, hidden_size: int, num_layers: int) None[源代码]¶
- Overview:
使用输入大小、隐藏大小和层数初始化GRU类。
- Arguments:
input_size (
int): 输入向量的大小。hidden_size (
int): 隐藏状态向量的大小。num_layers (
int): GRU层的数量。
- bias: bool¶
- forward(inputs: Tensor, prev_state: Tensor | None = None, list_next_state: bool = True) Tuple[Tensor, Tensor | List][source]¶
- Overview:
包装nn.GRU.forward方法。
- Arguments:
输入 (
torch.Tensor): 单元的输入向量,大小为 [seq_len, batch_size, input_size] 的张量。prev_state (
Optional[torch.Tensor]): 无或大小为 [num_directions*num_layers, batch_size, hidden_size] 的张量。list_next_state (
bool): 是否以列表格式返回next_state(默认为True)。
- Returns:
输出 (
torch.Tensor): GRU的输出。next_state (
torch.Tensor或list): GRU的隐藏状态。
- input_size: int¶
- weight_hh: Tensor¶
- weight_ih: Tensor¶
获取LSTM¶
- ding.torch_utils.network.rnn.get_lstm(lstm_type: str, input_size: int, hidden_size: int, num_layers: int = 1, norm_type: str = 'LN', dropout: float = 0.0, seq_len: int | None = None, batch_size: int | None = None) LSTM | PytorchLSTM[源代码]¶
- Overview:
根据提供的参数构建并返回相应的LSTM单元。
- Arguments:
lstm_type (
str): RNN单元的版本。支持的选项有['normal', 'pytorch', 'hpc', 'gru']。input_size (
int): 输入向量的大小。hidden_size (
int): 隐藏状态向量的大小。num_layers (
int): LSTM层数(默认为1)。norm_type (
str): 归一化类型(默认为‘LN’)。dropout (
float): 丢弃率(默认值为0.0)。seq_len (
Optional[int]): 序列长度(默认为None)。batch_size (
Optional[int]): 批量大小(默认为 None)。
- Returns:
lstm (
Union[LSTM, PytorchLSTM]): 对应的LSTM单元。
network.scatter_connection¶
请参考 ding/torch_utils/network/scatter_connection 获取更多详细信息。
shape_fn_scatter_connection¶
- ding.torch_utils.network.scatter_connection.shape_fn_scatter_connection(args, kwargs) List[int][source]¶
- Overview:
返回HPC的scatter_connection的形状。
- Arguments:
args (
Tuple): 传递给 scatter_connection 函数的参数。kwargs (
Dict): 传递给 scatter_connection 函数的关键字参数。
- Returns:
形状 (
List[int]): 一个表示scatter_connection形状的列表,形式为[B, M, N, H, W, scatter_type]。
散点连接¶
- class ding.torch_utils.network.scatter_connection.ScatterConnection(scatter_type: str)[源代码]¶
- Overview:
将特征分散到其对应的位置。在AlphaStar中,每个实体被嵌入到一个张量中,这些张量被分散到一个具有地图大小的特征图中。
- Interfaces:
__init__,forward,xy_forward
- __init__(scatter_type: str) None[源代码]¶
- Overview:
初始化ScatterConnection对象。
- Arguments:
scatter_type (
str): 散点类型,决定当两个实体具有相同位置时的行为。它可以是‘add’或‘cover’。如果是‘add’,第一个将被添加到第二个。如果是‘cover’,第一个将被第二个覆盖。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor, spatial_size: Tuple[int, int], location: Tensor) Tensor[来源]¶
- Overview:
将输入张量‘x’分散到空间特征图中。
- Arguments:
x (
torch.Tensor): 输入张量的形状为 (B, M, N),其中 B 是批次大小,M 是实体数量,N 是实体属性的维度。spatial_size (
Tuple[int, int]): 空间特征图的大小 (H, W),其中 H 是高度,W 是宽度。位置 (
torch.Tensor): 形状为 (B, M, 2) 的位置张量。每个位置应为 (y, x)。
- Returns:
输出 (
torch.Tensor): 形状为 (B, N, H, W) 的分散特征图。
- Note:
当位置有重叠时,‘cover’模式会导致信息丢失。 ‘add’模式被用作临时替代。
- training: bool¶
- xy_forward(x: Tensor, spatial_size: Tuple[int, int], coord_x: Tensor, coord_y) Tensor[来源]¶
- Overview:
使用单独的x和y坐标将输入张量‘x’分散到空间特征图中。
- Arguments:
x (
torch.Tensor): 输入张量的形状为 (B, M, N),其中 B 是批量大小,M 是实体数量,N 是实体属性的维度。spatial_size (
Tuple[int, int]): 空间特征图的大小 (H, W),其中 H 是高度,W 是宽度,'x' 将被分散到这个特征图中。coord_x (
torch.Tensor): 形状为 (B, M) 的 x 坐标张量。coord_y (
torch.Tensor): 形状为 (B, M) 的 y 坐标张量。
- Returns:
输出 (
torch.Tensor): 形状为 (B, N, H, W) 的分散特征图。
- Note:
当位置有重叠时,‘cover’模式会导致信息丢失。 ‘add’模式被用作临时替代。
network.soft_argmax¶
请参考 ding/torch_utils/network/soft_argmax 获取更多详细信息。
SoftArgmax¶
- class ding.torch_utils.network.soft_argmax.SoftArgmax[source]¶
- Overview:
一个神经网络模块,用于计算SoftArgmax操作(本质上是一个二维空间softmax),通常用于位置回归任务。它将特征图(如热图)转换为精确的坐标位置。
- Interfaces:
__init__,forward
注意
有关SoftArgmax的更多信息,您可以参考<https://en.wikipedia.org/wiki/Softmax_function> 以及论文<https://arxiv.org/pdf/1504.00702.pdf>。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor) Tensor[来源]¶
- Overview:
执行SoftArgmax操作的前向传递。
- Arguments:
x (
torch.Tensor): 输入张量,通常是一个表示预测位置的热图。
- Returns:
位置 (
torch.Tensor): SoftArgmax操作后预测的坐标。
- Shapes:
x: \((B, C, H, W)\), 其中 B 是批量大小, C 是通道数, H 和 W 分别代表高度和宽度.
位置:\((B, 2)\),其中B是批次大小,2代表坐标(高度,宽度)。
- training: bool¶
network.transformer¶
请参考 ding/torch_utils/network/transformer 获取更多详细信息。
注意¶
- class ding.torch_utils.network.transformer.Attention(input_dim: int, head_dim: int, output_dim: int, head_num: int, dropout: Module)[源代码]¶
- Overview:
对于每个条目嵌入,计算所有条目的单独注意力,将它们相加以获得输出注意力。
- Interfaces:
__init__,split,forward
- __init__(input_dim: int, head_dim: int, output_dim: int, head_num: int, dropout: Module) None[源代码]¶
- Overview:
使用提供的维度和dropout层初始化Attention模块。
- Arguments:
input_dim (
int): 输入的维度。head_dim (
int): 多头注意力机制中每个头的维度。output_dim (
int): 输出的维度。head_num (
int): 多头注意力机制中的头数。dropout (
nn.Module): 用于注意力机制中的dropout层。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor, mask: Tensor | None = None) Tensor[source]¶
- Overview:
从输入张量计算注意力。
- Arguments:
x (
torch.Tensor): 用于前向计算的输入张量。- mask (
Optional[torch.Tensor], optional): Optional mask to exclude invalid entries. 默认为 None。
- mask (
- Returns:
注意力 (
torch.Tensor): 计算得到的注意力张量。
- split(x: Tensor, T: bool = False) List[Tensor][来源]¶
- Overview:
将输入分割以获取多头查询、键和值。
- Arguments:
x (
torch.Tensor): 要分割的张量,可以是查询、键或值。T (
bool, 可选): 如果为True,则转置输出张量。默认为False。
- Returns:
x (
List[torch.Tensor]): 每个头的输出张量列表。
- training: bool¶
TransformerLayer¶
- class ding.torch_utils.network.transformer.TransformerLayer(input_dim: int, head_dim: int, hidden_dim: int, output_dim: int, head_num: int, mlp_num: int, dropout: Module, activation: Module)[源代码]¶
- Overview:
在变压器层中,首先计算条目的注意力并应用前馈层。
- Interfaces:
__init__,forward
- __init__(input_dim: int, head_dim: int, hidden_dim: int, output_dim: int, head_num: int, mlp_num: int, dropout: Module, activation: Module) None[源代码]¶
- Overview:
使用提供的维度、dropout层和激活函数初始化TransformerLayer。
- Arguments:
input_dim (
int): 输入的维度。head_dim (
int): 多头注意力机制中每个头的维度。hidden_dim (
int): MLP(多层感知器)中隐藏层的维度。output_dim (
int): 输出的维度。head_num (
int): 多头注意力机制中的头数。mlp_num (
int): MLP中的层数。dropout (
nn.Module): 用于注意力机制中的dropout层。activation (
nn.Module): 在MLP中使用的激活函数。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(inputs: Tuple[Tensor, Tensor]) Tuple[Tensor, Tensor][source]¶
- Overview:
计算通过Transformer层的前向传播。
- Arguments:
- inputs (
Tuple[torch.Tensor, torch.Tensor]): A tuple containing the input tensor x and 掩码张量。
- inputs (
- Returns:
- output (
Tuple[torch.Tensor, torch.Tensor]): A tuple containing the predicted value tensor and 掩码张量。
- output (
- training: bool¶
变压器¶
- class ding.torch_utils.network.transformer.Transformer(input_dim: int, head_dim: int = 128, hidden_dim: int = 1024, output_dim: int = 256, head_num: int = 2, mlp_num: int = 2, layer_num: int = 3, dropout_ratio: float = 0.0, activation: Module = ReLU())[源代码]¶
- Overview:
Transformer模型的实现。
注意
更多详情,请参考“Attention is All You Need”:http://arxiv.org/abs/1706.03762。
- Interfaces:
__init__,forward
- __init__(input_dim: int, head_dim: int = 128, hidden_dim: int = 1024, output_dim: int = 256, head_num: int = 2, mlp_num: int = 2, layer_num: int = 3, dropout_ratio: float = 0.0, activation: Module = ReLU())[源代码]¶
- Overview:
使用提供的维度、dropout层、激活函数和层数初始化Transformer。
- Arguments:
input_dim (
int): 输入的维度。head_dim (
int): 多头注意力机制中每个头的维度。hidden_dim (
int): MLP(多层感知器)中隐藏层的维度。output_dim (
int): 输出的维度。head_num (
int): 多头注意力机制中的头数。mlp_num (
int): MLP中的层数。层数 (
int): Transformer层的数量。dropout_ratio (
float): dropout层的dropout比例。activation (
nn.Module): 在MLP中使用的激活函数。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(x: Tensor, mask: Tensor | None = None) Tensor[来源]¶
- Overview:
执行Transformer的前向传递。
- Arguments:
x (
torch.Tensor): 输入张量,形状为 (B, N, C),其中 B 是批量大小,N 是条目数量,C 是特征维度。mask (
Optional[torch.Tensor], 可选): 掩码张量 (bool), 用于在注意力机制中屏蔽无效条目。它的形状为 (B, N), 其中 B 是批量大小,N 是条目数量。默认为 None。
- Returns:
x (
torch.Tensor): Transformer的输出张量。
- training: bool¶
缩放点积注意力¶
- class ding.torch_utils.network.transformer.ScaledDotProductAttention(d_k: int, dropout: float = 0.0)[来源]¶
- Overview:
实现缩放点积注意力,这是Transformer模型的一个关键组件。 此类执行查询、键和值张量的点积,使用键向量维度(d_k)的平方根进行缩放,并应用dropout进行正则化。
- Interfaces:
__init__,forward
- __init__(d_k: int, dropout: float = 0.0) None[source]¶
- Overview:
使用键向量的维度和丢弃率初始化ScaledDotProductAttention模块。
- Arguments:
d_k (
int): 键向量的维度。这将用于缩放查询和键的点积。dropout (
float, 可选): 在softmax操作后应用的dropout率。默认值为0.0。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- forward(q: Tensor, k: Tensor, v: Tensor, mask: Tensor | None = None) Tensor[来源]¶
- Overview:
对查询、键和值张量执行缩放点积注意力操作。
- Arguments:
q (
torch.Tensor): 查询张量。k (
torch.Tensor): 键张量。v (
torch.Tensor): 值张量。- mask (
Optional[torch.Tensor]): An optional mask tensor to be applied on the attention scores. 默认为 None。
- mask (
- Returns:
输出 (
torch.Tensor): 注意力操作后的输出张量。
- training: bool¶
后端助手¶
请参考 ding/torch_utils/backend_helper 获取更多详细信息。
enable_tf32¶
- ding.torch_utils.backend_helper.enable_tf32() None[源代码]¶
- Overview:
在matmul和cudnn上启用tf32以加快计算速度。这仅在Ampere GPU设备上有效。有关详细信息,请参阅:https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices。
checkpoint_helper¶
请参考 ding/torch_utils/checkpoint_helper 获取更多详细信息。
build_checkpoint_helper¶
- ding.torch_utils.checkpoint_helper.build_checkpoint_helper(cfg)[source]¶
- Overview:
使用配置来构建检查点助手。
- Arguments:
cfg (
dict): ckpt_helper 配置
- Returns:
(
CheckpointHelper): 由该函数创建的checkpoint_helper
CheckpointHelper¶
- class ding.torch_utils.checkpoint_helper.CheckpointHelper[source]¶
- Overview:
帮助通过提供参数保存或加载检查点。
- Interfaces:
__init__,save,load,_remove_prefix,_add_prefix,_load_matched_model_state_dict
- _add_prefix(state_dict: dict, prefix: str = 'module.') dict[source]¶
- Overview:
在state_dict中添加前缀
- Arguments:
state_dict (
dict): 模型的状态字典前缀 (
str): 这个前缀将被添加到键中
- Returns:
(
dict): 添加前缀后的新状态字典
- _load_matched_model_state_dict(model: Module, ckpt_state_dict: dict) None[source]¶
- Overview:
加载匹配的模型状态字典,并显示模型的状态字典与检查点的状态字典之间的不匹配键
- Arguments:
模型 (
torch.nn.Module): 模型ckpt_state_dict (
dict): 检查点的状态字典
- _remove_prefix(state_dict: dict, prefix: str = 'module.') dict[source]¶
- Overview:
移除state_dict中的前缀
- Arguments:
state_dict (
dict): 模型的状态字典prefix (
str): 这个前缀将在键中被移除
- Returns:
new_state_dict (
dict): 移除前缀后的新状态字典
- load(load_path: str, model: Module, optimizer: Optimizer = None, last_iter: CountVar = None, last_epoch: CountVar = None, last_frame: CountVar = None, lr_schduler: Scheduler = None, dataset: Dataset = None, collector_info: Module = None, prefix_op: str = None, prefix: str = None, strict: bool = True, logger_prefix: str = '', state_dict_mask: list = [])[来源]¶
- Overview:
通过给定路径加载检查点
- Arguments:
load_path (
str): 检查点的路径模型 (
torch.nn.Module): 模型定义优化器 (
torch.optim.Optimizer): 优化器对象last_iter (
CountVar): 迭代次数,默认为 Nonelast_epoch (
CountVar): 周期数,默认为 Nonelast_frame (
CountVar): 帧数,默认为 Nonelr_schduler (
Schduler): lr_schduler 对象数据集 (
torch.utils.data.Dataset): 数据集,应该是replaydatasetcollector_info (
torch.nn.Module): 检查点的属性,保存收集器信息prefix_op (
str): 应该是 ['remove', 'add'], 对 state_dict 进行处理prefix (
str): 在state_dict上处理的前缀strict (
bool): model.load_state_dict 的参数logger_prefix (
str): 日志记录器的前缀state_dict_mask (
list): 一个包含state_dict键的列表,这些键不应加载到模型中(在前缀操作之后)
注意
从load_path加载的检查点是一个字典,其格式类似于‘{‘state_dict’: OrderedDict(), …}’
- save(path: str, model: Module, optimizer: Optimizer | None = None, last_iter: CountVar | None = None, last_epoch: CountVar | None = None, last_frame: CountVar | None = None, dataset: Dataset | None = None, collector_info: Module | None = None, prefix_op: str | None = None, prefix: str | None = None) None[source]¶
- Overview:
根据给定的参数保存检查点
- Arguments:
路径 (
str): 保存检查点的路径模型 (
torch.nn.Module): 要保存的模型优化器 (
torch.optim.Optimizer): 优化器对象last_iter (
CountVar): 迭代次数,默认为 Nonelast_epoch (
CountVar): 周期数,默认为 Nonelast_frame (
CountVar): 帧数,默认为 None数据集 (
torch.utils.data.Dataset): 数据集,应该是replaydatasetcollector_info (
torch.nn.Module): 检查点的属性,保存收集器信息prefix_op (
str): 应该是 ['remove', 'add'], 对 state_dict 进行处理prefix (
str): 在state_dict上处理的前缀
计数变量¶
自动检查点¶
数据助手¶
请参考 ding/torch_utils/data_helper 获取更多详细信息。
to_device¶
- ding.torch_utils.data_helper.to_device(item: Any, device: str, ignore_keys: list = []) Any[source]¶
- Overview:
将数据传输到特定设备。
- Arguments:
项目 (
Any): 要传输的项目。设备 (
str): 所需的设备。ignore_keys (
list): 在传输中要忽略的键,默认设置为空。
- Returns:
项目 (
Any): 传输的项目。
- Examples:
>>> setup_data_dict['module'] = nn.Linear(3, 5) >>> device = 'cuda' >>> cuda_d = to_device(setup_data_dict, device, ignore_keys=['module']) >>> assert cuda_d['module'].weight.device == torch.device('cpu')
- Examples:
>>> setup_data_dict['module'] = nn.Linear(3, 5) >>> device = 'cuda' >>> cuda_d = to_device(setup_data_dict, device) >>> assert cuda_d['module'].weight.device == torch.device('cuda:0')
to_dtype¶
- ding.torch_utils.data_helper.to_dtype(item: Any, dtype: type) Any[来源]¶
- Overview:
将数据更改为特定数据类型。
- Arguments:
项目 (
Any): 用于更改数据类型的项目。dtype (
type): 所需的类型。
- Returns:
项目 (
object): 更改了数据类型的项目。
- Examples (tensor):
>>> t = torch.randint(0, 10, (3, 5)) >>> tfloat = to_dtype(t, torch.float) >>> assert tfloat.dtype == torch.float
- Examples (list):
>>> tlist = [torch.randint(0, 10, (3, 5))] >>> tlfloat = to_dtype(tlist, torch.float) >>> assert tlfloat[0].dtype == torch.float
- Examples (dict):
>>> tdict = {'t': torch.randint(0, 10, (3, 5))} >>> tdictf = to_dtype(tdict, torch.float) >>> assert tdictf['t'].dtype == torch.float
to_tensor¶
- ding.torch_utils.data_helper.to_tensor(item: Any, dtype: dtype | None = None, ignore_keys: list = [], transform_scalar: bool = True) Any[source]¶
- Overview:
将
numpy.ndarray对象转换为torch.Tensor。- Arguments:
item (
Any): 要转换的numpy.ndarray对象。它可以是一个numpy.ndarray对象,或者是一个包含多个numpy.ndarray对象的容器(列表、元组或字典)。dtype (
torch.dtype): 期望的张量类型。如果设置为None,其数据类型将保持不变。ignore_keys (
list): 如果item是一个字典,键在ignore_keys中的值将不会被转换。transform_scalar (
bool): 如果设置为True,标量也将被转换为张量对象。
- Returns:
项目 (
Any): 转换后的张量。
- Examples (scalar):
>>> i = 10 >>> t = to_tensor(i) >>> assert t.item() == i
- Examples (dict):
>>> d = {'i': i} >>> dt = to_tensor(d, torch.int) >>> assert dt['i'].item() == i
- Examples (named tuple):
>>> data_type = namedtuple('data_type', ['x', 'y']) >>> inputs = data_type(np.random.random(3), 4) >>> outputs = to_tensor(inputs, torch.float32) >>> assert type(outputs) == data_type >>> assert isinstance(outputs.x, torch.Tensor) >>> assert isinstance(outputs.y, torch.Tensor) >>> assert outputs.x.dtype == torch.float32 >>> assert outputs.y.dtype == torch.float32
to_ndarray¶
- ding.torch_utils.data_helper.to_ndarray(item: Any, dtype: dtype | None = None) Any[来源]¶
- Overview:
将
torch.Tensor转换为numpy.ndarray。- Arguments:
item (
Any): 要转换的torch.Tensor对象。它可以是一个torch.Tensor对象,或者是一个包含多个torch.Tensor对象的容器(列表、元组或字典)。dtype (
np.dtype): 所需数组的类型。如果设置为None,其数据类型将保持不变。
- Returns:
项目 (
object): 更改后的数组。
- Examples (ndarray):
>>> t = torch.randn(3, 5) >>> tarray1 = to_ndarray(t) >>> assert tarray1.shape == (3, 5) >>> assert isinstance(tarray1, np.ndarray)
- Examples (list):
>>> t = [torch.randn(5, ) for i in range(3)] >>> tarray1 = to_ndarray(t, np.float32) >>> assert isinstance(tarray1, list) >>> assert tarray1[0].shape == (5, ) >>> assert isinstance(tarray1[0], np.ndarray)
to_list¶
- ding.torch_utils.data_helper.to_list(item: Any) Any[source]¶
- Overview:
将
torch.Tensor、numpy.ndarray对象转换为list对象,并保持其数据类型不变。- Arguments:
项目 (
Any): 要转换的项目。
- Returns:
项目 (
Any): 转换后的列表。
- Examples:
>>> data = { 'tensor': torch.randn(4), 'list': [True, False, False], 'tuple': (4, 5, 6), 'bool': True, 'int': 10, 'float': 10., 'array': np.random.randn(4), 'str': "asdf", 'none': None, } >>> transformed_data = to_list(data)
注意
现在支持的项目类型:
torch.Tensor,numpy.ndarray,dict,list,tuple和None.
tensor_to_list¶
- ding.torch_utils.data_helper.tensor_to_list(item: Any) Any[source]¶
- Overview:
将
torch.Tensor对象转换为list,并保持其数据类型不变。- Arguments:
项目 (
Any): 要转换的项目。
- Returns:
项目 (
Any): 转换后的列表。
- Examples (2d-tensor):
>>> t = torch.randn(3, 5) >>> tlist1 = tensor_to_list(t) >>> assert len(tlist1) == 3 >>> assert len(tlist1[0]) == 5
- Examples (1d-tensor):
>>> t = torch.randn(3, ) >>> tlist1 = tensor_to_list(t) >>> assert len(tlist1) == 3
- Examples (list)
>>> t = [torch.randn(5, ) for i in range(3)] >>> tlist1 = tensor_to_list(t) >>> assert len(tlist1) == 3 >>> assert len(tlist1[0]) == 5
- Examples (dict):
>>> td = {'t': torch.randn(3, 5)} >>> tdlist1 = tensor_to_list(td) >>> assert len(tdlist1['t']) == 3 >>> assert len(tdlist1['t'][0]) == 5
注意
现在支持的项目类型:
torch.Tensor,dict,list,tuple和None。
to_item¶
- ding.torch_utils.data_helper.to_item(data: Any, ignore_error: bool = True) Any[source]¶
- Overview:
将数据转换为Python原生标量(即数据项),并保持其数据类型不变。
- Arguments:
数据 (
Any): 需要转换的数据。ignore_error (
bool): 当数据类型不受支持时是否忽略错误。也就是说,只有可以转换为Python原生标量的数据才会被返回。
- Returns:
data (
Any): 转换后的数据。
- Examples:
>>>> data = { ‘tensor’: torch.randn(1), ‘list’: [True, False, torch.randn(1)], ‘tuple’: (4, 5, 6), ‘bool’: True, ‘int’: 10, ‘float’: 10., ‘array’: np.random.randn(1), ‘str’: “asdf”, ‘none’: None, } >>>> new_data = to_item(data) >>>> assert np.isscalar(new_data[‘tensor’]) >>>> assert np.isscalar(new_data[‘array’]) >>>> assert np.isscalar(new_data[‘list’][-1])
注意
现在支持的项目类型:
torch.Tensor,torch.Tensor,ttorch.Tensor,bool,str,dict,list,tuple和None。
相同形状¶
- ding.torch_utils.data_helper.same_shape(data: list) bool[source]¶
- Overview:
判断列表中的所有数据元素是否具有相同的形状。
- Arguments:
数据 (
list): 数据列表。
- Returns:
same (
bool): 数据列表是否都具有相同的形状。
- Examples:
>>> tlist = [torch.randn(3, 5) for i in range(5)] >>> assert same_shape(tlist) >>> tlist = [torch.randn(3, 5), torch.randn(4, 5)] >>> assert not same_shape(tlist)
日志字典¶
构建日志缓冲区¶
- ding.torch_utils.data_helper.build_log_buffer() LogDict[source]¶
- Overview:
构建日志缓冲区,dict的一个子类,可以将输入数据转换为日志格式。
- Returns:
log_buffer (
LogDict): 日志缓冲区字典。
- Examples:
>>> log_buffer = build_log_buffer() >>> log_buffer['not_tensor'] = torch.randn(3) >>> assert isinstance(log_buffer['not_tensor'], list) >>> assert len(log_buffer['not_tensor']) == 3 >>> log_buffer.update({'not_tensor': 4, 'a': 5}) >>> assert log_buffer['not_tensor'] == 4
CudaFetcher¶
- class ding.torch_utils.data_helper.CudaFetcher(data_source: Iterable, device: str, queue_size: int = 4, sleep: float = 0.1)[源代码]¶
- Overview:
从源获取数据,并将其传输到指定设备。
- Interfaces:
__init__,__next__,run,close.
获取张量数据¶
- ding.torch_utils.data_helper.get_tensor_data(data: Any) Any[源代码]¶
- Overview:
从给定数据中获取纯张量数据(不干扰梯度计算图)。
- Arguments:
数据 (
Any): 原始数据。它可以是一个张量或一个容器(序列或字典)。
- Returns:
输出 (
Any): 输出数据。
- Examples:
>>> a = { 'tensor': torch.tensor([1, 2, 3.], requires_grad=True), 'list': [torch.tensor([1, 2, 3.], requires_grad=True) for _ in range(2)], 'none': None } >>> tensor_a = get_tensor_data(a) >>> assert not tensor_a['tensor'].requires_grad >>> for t in tensor_a['list']: >>> assert not t.requires_grad
unsqueeze¶
- ding.torch_utils.data_helper.unsqueeze(data: Any, dim: int = 0) Any[source]¶
- Overview:
对张量数据进行解压缩。
- Arguments:
数据 (
Any): 原始数据。它可以是一个张量或一个容器(序列或字典)。dim (
int): 要解压的维度。
- Returns:
输出 (
Any): 输出数据。
- Examples (tensor):
>>> t = torch.randn(3, 3) >>> tt = unsqueeze(t, dim=0) >>> assert tt.shape == torch.Shape([1, 3, 3])
- Examples (list):
>>> t = [torch.randn(3, 3)] >>> tt = unsqueeze(t, dim=0) >>> assert tt[0].shape == torch.Shape([1, 3, 3])
- Examples (dict):
>>> t = {"t": torch.randn(3, 3)} >>> tt = unsqueeze(t, dim=0) >>> assert tt["t"].shape == torch.Shape([1, 3, 3])
压缩¶
- ding.torch_utils.data_helper.squeeze(data: Any, dim: int = 0) Any[source]¶
- Overview:
压缩张量数据。
- Arguments:
数据 (
Any): 原始数据。它可以是一个张量或一个容器(序列或字典)。dim (
int): 要被压缩的维度。
- Returns:
输出 (
Any): 输出数据。
- Examples (tensor):
>>> t = torch.randn(1, 3, 3) >>> tt = squeeze(t, dim=0) >>> assert tt.shape == torch.Shape([3, 3])
- Examples (list):
>>> t = [torch.randn(1, 3, 3)] >>> tt = squeeze(t, dim=0) >>> assert tt[0].shape == torch.Shape([3, 3])
- Examples (dict):
>>> t = {"t": torch.randn(1, 3, 3)} >>> tt = squeeze(t, dim=0) >>> assert tt["t"].shape == torch.Shape([3, 3])
获取空数据¶
- ding.torch_utils.data_helper.get_null_data(template: Any, num: int) List[Any][source]¶
- Overview:
根据输入模板获取空数据。
- Arguments:
模板 (
Any): 模板数据。num (
int): 要生成的空数据项的数量。
- Returns:
输出 (
List[Any]): 生成的空数据。
- Examples:
>>> temp = {'obs': [1, 2, 3], 'action': 1, 'done': False, 'reward': torch.tensor(1.)} >>> null_data = get_null_data(temp, 2) >>> assert len(null_data) ==2 >>> assert null_data[0]['null'] and null_data[0]['done']
zeros_like¶
- ding.torch_utils.data_helper.zeros_like(h: Any) Any[source]¶
- Overview:
生成与输入数据相似的零张量。
- Arguments:
h (
Any): 原始数据。它可以是一个张量或一个容器(序列或字典)。
- Returns:
输出 (
Any): 输出的零张量。
- Examples (tensor):
>>> t = torch.randn(3, 3) >>> tt = zeros_like(t) >>> assert tt.shape == torch.Shape([3, 3]) >>> assert torch.sum(torch.abs(tt)) < 1e-8
- Examples (list):
>>> t = [torch.randn(3, 3)] >>> tt = zeros_like(t) >>> assert tt[0].shape == torch.Shape([3, 3]) >>> assert torch.sum(torch.abs(tt[0])) < 1e-8
- Examples (dict):
>>> t = {"t": torch.randn(3, 3)} >>> tt = zeros_like(t) >>> assert tt["t"].shape == torch.Shape([3, 3]) >>> assert torch.sum(torch.abs(tt["t"])) < 1e-8
数据并行¶
请参考 ding/torch_utils/dataparallel 获取更多详细信息。
DataParallel¶
- class ding.torch_utils.dataparallel.DataParallel(module, device_ids=None, output_device=None, dim=0)[source]¶
- Overview:
nn.DataParallel 的包装类。
- Interfaces:
__init__,parameters
- __init__(module, device_ids=None, output_device=None, dim=0)[源代码]¶
- Overview:
初始化DataParallel对象。
- Arguments:
模块 (
nn.Module): 需要并行化的模块。device_ids (
list): GPU ID 列表。output_device (
int): 输出的GPU ID。dim (
int): 要并行化的维度。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- parameters(recurse: bool = True)[source]¶
- Overview:
返回模块的参数。
- Arguments:
recurse (
bool): 是否返回子模块的参数。
- Returns:
参数 (
generator): 参数的生成器。
- training: bool¶
分布¶
请参考 ding/torch_utils/distribution 获取更多详细信息。
Pd¶
- class ding.torch_utils.distribution.Pd[source]¶
- Overview:
用于参数化概率分布和采样函数的抽象类。
- Interfaces:
neglogp,entropy,noise_mode,mode,sample
提示
在派生类中,logits 应该是存储在类中的属性成员。
- entropy() Tensor[来源]¶
- Overview:
计算logits的softmax熵
- Arguments:
reduction (
str): 支持 [None, ‘mean’], 默认设置为 ‘mean’
- Returns:
熵 (
torch.Tensor): 计算得到的熵
分类Pd¶
- class ding.torch_utils.distribution.CategoricalPd(logits: Tensor | None = None)[source]¶
- Overview:
分类概率分布采样器
- Interfaces:
__init__,neglogp,entropy,noise_mode,mode,sample
- __init__(logits: Tensor | None = None) None[source]¶
- Overview:
使用logits初始化Pd
- Arguments:
logits (:obj:torch.Tensor): 从中采样的logits
- entropy(reduction: str = 'mean') Tensor[来源]¶
- Overview:
计算logits的softmax熵
- Arguments:
reduction (
str): 支持 [None, ‘mean’], 默认设置为 mean
- Returns:
熵 (
torch.Tensor): 计算得到的熵
- mode(viz: bool = False) Tuple[Tensor, Dict[str, ndarray]][source]¶
- Overview:
返回logits的argmax结果
- Arguments:
- viz (
bool): Whether to return numpy from of logits, noise and noise_logits; 是
visualize的缩写。(因为张量类型无法在tb或文本日志中可视化)
- viz (
- Returns:
结果 (
torch.Tensor): logits argmax 的结果viz_feature (
Dict[str, np.ndarray]): 用于可视化的ndarray类型数据。
- neglogp(x, reduction: str = 'mean') Tensor[来源]¶
- Overview:
计算输入x和logits之间的交叉熵
- Arguments:
x (
torch.Tensor): 输入张量reduction (
str): 支持 [None, ‘mean’], 默认设置为 mean
- Return:
cross_entropy (
torch.Tensor): 返回的交叉熵损失
- noise_mode(viz: bool = False) Tuple[Tensor, Dict[str, ndarray]][source]¶
- Overview:
向logits添加噪声
- Arguments:
viz (
bool): 是否返回logits、noise和noise_logits的numpy形式;visualize的缩写。(因为tensor类型无法在tb或文本日志中可视化)
- Returns:
结果 (
torch.Tensor): 噪声化的对数几率viz_feature (
Dict[str, np.ndarray]): 用于可视化的ndarray类型数据。
- sample(viz: bool = False) Tuple[Tensor, Dict[str, ndarray]][source]¶
- Overview:
通过使用softmax从logits的分布中采样
- Arguments:
viz (
bool): 是否返回logits、noise和noise_logits的numpy形式;visualize的缩写。(因为tensor类型无法在tb或文本日志中可视化)
- Returns:
结果 (
torch.Tensor): 采样的logits结果viz_feature (
Dict[str, np.ndarray]): 用于可视化的ndarray类型数据。
CategoricalPdPytorch¶
- class ding.torch_utils.distribution.CategoricalPdPytorch(probs: Tensor | None = None)[来源]¶
- Overview:
封装的
torch.distributions.Categorical- Interfaces:
__init__,update_logits,update_probs,sample,neglogp,mode,entropy
- __init__(probs: Tensor | None = None) None[来源]¶
- Overview:
初始化 CategoricalPdPytorch 对象。
- Arguments:
probs (
torch.Tensor): 概率的张量。
- entropy(reduction: str | None = None) Tensor[source]¶
- Overview:
计算logits的softmax熵
- Arguments:
reduction (
str): 支持 [None, ‘mean’], 默认设置为 mean
- Returns:
熵 (
torch.Tensor): 计算得到的熵
- neglogp(actions: Tensor, reduction: str = 'mean') Tensor[source]¶
- Overview:
计算输入x和logits之间的交叉熵
- Arguments:
动作 (
torch.Tensor): 输入的动作张量reduction (
str): 支持 [None, ‘mean’], 默认设置为 mean
- Return:
cross_entropy (
torch.Tensor): 返回的交叉熵损失
学习率调度器¶
请参考 ding/torch_utils/lr_scheduler 了解更多详情。
获取学习率比率¶
- ding.torch_utils.lr_scheduler.get_lr_ratio(epoch: int, warmup_epochs: int, learning_rate: float, lr_decay_epochs: int, min_lr: float) float[source]¶
- Overview:
获取每个epoch的学习率比率。
- Arguments:
epoch (
int): 当前周期。warmup_epochs (
int): 预热周期。learning_rate (
float): 学习率。lr_decay_epochs (
int): 学习率衰减周期。min_lr (
float): 最小学习率。
cos_lr_scheduler¶
- ding.torch_utils.lr_scheduler.cos_lr_scheduler(optimizer: Optimizer, learning_rate: float, warmup_epochs: float = 5, lr_decay_epochs: float = 100, min_lr: float = 6e-05) LambdaLR[source]¶
- Overview:
余弦学习率调度器。
- Arguments:
优化器 (
torch.optim.Optimizer): 优化器。learning_rate (
float): 学习率。warmup_epochs (
float): 预热周期。lr_decay_epochs (
float): 学习率衰减周期。min_lr (
float): 最小学习率。
数学助手¶
请参考 ding/torch_utils/math_helper 获取更多详细信息。
协方差¶
- ding.torch_utils.math_helper.cov(x: Tensor, rowvar: bool = False, bias: bool = False, ddof: int | None = None, aweights: Tensor | None = None) Tensor[source]¶
- Overview:
估计协方差矩阵,类似于
numpy.cov。- Arguments:
x (
torch.Tensor): 一个包含多个变量和观测值的1-D或2-D张量。x的每一行代表一个变量,每一列代表所有这些变量的单个观测值。rowvar (
bool): 如果rowvar默认为 True,则每列是所有变量的单个观察值。否则,每列代表一个变量,而行包含观察值。偏差 (
bool): 默认归一化 (False) 是通过除以N - 1来实现的,其中N是给定的观测次数(无偏估计)。如果bias是True,则归一化是通过N来实现的。ddof (
Optional[int]): 如果ddof不是None,则意味着参数bias被覆盖。请注意,ddof=1将返回无偏估计(等于bias=False),而ddof=0将返回有偏估计(等于bias=True)。aweights (
Optional[torch.Tensor]): 1-D 张量的观测向量权重。这些相对权重通常对于被认为是“重要”的观测值较大,而对于被认为不那么“重要”的观测值较小。如果ddof=0,权重张量可用于为观测向量分配权重。
- Returns:
cov_mat (
torch.Tensor): 计算得到的协方差矩阵。
指标¶
请参考 ding/torch_utils/metric 获取更多详细信息。
levenshtein_distance¶
- ding.torch_utils.metric.levenshtein_distance(pred: LongTensor, target: LongTensor, pred_extra: Tensor | None = None, target_extra: Tensor | None = None, extra_fn: Callable | None = None) FloatTensor[来源]¶
- Overview:
Levenshtein距离,即编辑距离。
- Arguments:
pred (
torch.LongTensor): 用于计算距离的第一个张量,形状: (N1, ) (N1 >= 0).目标 (
torch.LongTensor): 用于计算距离的第二个张量,形状: (N2, ) (N2 >= 0)。pred_extra (
Optional[torch.Tensor]): 用于计算距离的额外张量,仅在extra_fn不为None时有效。target_extra (
Optional[torch.Tensor]): 用于计算距离的额外张量,仅在extra_fn不为None时有效。extra_fn (
Optional[Callable]): 用于pred_extra和target_extra的距离函数。如果设置为None,则不会考虑此距离。
- Returns:
距离 (
torch.FloatTensor): 距离(标量), 形状: (1, ).
汉明距离¶
- ding.torch_utils.metric.hamming_distance(pred: LongTensor, target: LongTensor, weight=1.0) LongTensor[source]¶
- Overview:
汉明距离。
- Arguments:
pred (
torch.LongTensor): 预测输入,布尔向量(0或1)。目标 (
torch.LongTensor): 目标输入,布尔向量(0 或 1)。权重 (
torch.LongTensor): 要相乘的权重。
- Returns:
距离(
torch.LongTensor): 距离(标量),形状 (1, )。
- Shapes:
pred & target (
torch.LongTensor): 形状 \((B, N)\), 其中 B 是批量大小,N 是维度
model_helper¶
请参考 ding/torch_utils/model_helper 获取更多详细信息。
获取参数数量¶
nn_test_helper¶
请参考 ding/torch_utils/nn_test_helper 获取更多详细信息。
is_differentiable¶
- ding.torch_utils.nn_test_helper.is_differentiable(loss: Tensor, model: Module | List[Module], print_instead: bool = False) None[source]¶
- Overview:
判断模型/模型是否可微分。首先检查模块的grad是否为None,然后进行损失的反向传播,最后检查模块的grad是否为torch.Tensor。
- Arguments:
损失 (
torch.Tensor): 模型的损失张量模型 (
Union[torch.nn.Module, List[torch.nn.Module]]): 要检查的模型或模型列表print_instead (
bool): 是否打印模块的最终梯度结果,而不是断言。默认设置为False。
优化器助手¶
请参考 ding/torch_utils/optimizer_helper 获取更多详细信息。
计算梯度范数¶
计算不带偏置的二范数梯度范数¶
grad_ignore_norm¶
grad_ignore_value¶
Adam¶
- class ding.torch_utils.optimizer_helper.Adam(params: Iterable, lr: float = 0.001, betas: Tuple[float, float] = (0.9, 0.999), eps: float = 1e-08, weight_decay: float = 0, amsgrad: bool = False, optim_type: str = 'adam', grad_clip_type: str | None = None, clip_value: float | None = None, clip_coef: float = 5, clip_norm_type: float = 2.0, clip_momentum_timestep: int = 100, grad_norm_type: str | None = None, grad_ignore_type: str | None = None, ignore_value: float | None = None, ignore_coef: float = 5, ignore_norm_type: float = 2.0, ignore_momentum_timestep: int = 100)[source]¶
- Overview:
重写了Adam优化器以支持更多功能。
- Interfaces:
__init__,step,_state_init,get_grad
- __init__(params: Iterable, lr: float = 0.001, betas: Tuple[float, float] = (0.9, 0.999), eps: float = 1e-08, weight_decay: float = 0, amsgrad: bool = False, optim_type: str = 'adam', grad_clip_type: str | None = None, clip_value: float | None = None, clip_coef: float = 5, clip_norm_type: float = 2.0, clip_momentum_timestep: int = 100, grad_norm_type: str | None = None, grad_ignore_type: str | None = None, ignore_value: float | None = None, ignore_coef: float = 5, ignore_norm_type: float = 2.0, ignore_momentum_timestep: int = 100)[源代码]¶
- Overview:
重构后的Adam类的init方法
- Arguments:
参数 (
iterable): – 一个包含 torch.Tensor 或 dict 的可迭代对象。指定应该优化哪些张量。lr (
float): 学习率,默认设置为1e-3betas (
Tuple[float, float]): 用于计算梯度及其平方的运行平均值的系数,默认设置为 (0.9, 0.999))eps (
float): 添加到分母以提高数值稳定性的项,默认设置为1e-8weight_decay (
float): 权重衰减系数,默认设置为0amsgrad (
bool): 是否使用论文《On the Convergence of Adam and Beyond》中的AMSGrad变体算法 <https://arxiv.org/abs/1904.09237>optim_type (:obj:str): 支持 [“adam”, “adamw”]
grad_clip_type (
str): 支持 [None, ‘clip_momentum’, ‘clip_value’, ‘clip_norm’, ‘clip_momentum_norm’]clip_value (
float): 开始剪裁的值clip_coef (
float): 裁剪系数clip_norm_type (
float): 2.0 表示使用 norm2 进行裁剪clip_momentum_timestep (
int): 在多少步之后我们应该开始动量裁剪grad_ignore_type (
str): 支持 [None, ‘ignore_momentum’, ‘ignore_value’, ‘ignore_norm’, ‘ignore_momentum_norm’]ignore_value (
float): 开始忽略的值ignore_coef (
float): 忽略系数ignore_norm_type (
float): 2.0 表示使用 norm2 来忽略ignore_momentum_timestep (
int): 在多少步之后我们应该开始忽略动量
- _optimizer_load_state_dict_post_hooks: OrderedDict[int, Callable[['Optimizer'], None]]¶
- _optimizer_load_state_dict_pre_hooks: OrderedDict[int, Callable[['Optimizer', StateDict], StateDict | None]]¶
- _optimizer_state_dict_post_hooks: OrderedDict[int, Callable[['Optimizer', StateDict], StateDict | None]]¶
- _optimizer_state_dict_pre_hooks: OrderedDict[int, Callable[['Optimizer'], None]]¶
- _optimizer_step_post_hooks: Dict[int, Callable[[Self, Tuple[Any, ...], Dict[str, Any]], None]]¶
- _optimizer_step_pre_hooks: Dict[int, Callable[[Self, Tuple[Any, ...], Dict[str, Any]], Tuple[Tuple[Any, ...], Dict[str, Any]] | None]]¶
- _state_init(p, amsgrad)[来源]¶
- Overview:
初始化优化器的状态
- Arguments:
p (
torch.Tensor): 需要优化的参数amsgrad (
bool): 是否使用论文《On the Convergence of Adam and Beyond》中的AMSGrad变体算法 <https://arxiv.org/abs/1904.09237>
RMSprop¶
- class ding.torch_utils.optimizer_helper.RMSprop(params: Iterable, lr: float = 0.01, alpha: float = 0.99, eps: float = 1e-08, weight_decay: float = 0, momentum: float = 0, centered: bool = False, grad_clip_type: str | None = None, clip_value: float | None = None, clip_coef: float = 5, clip_norm_type: float = 2.0, clip_momentum_timestep: int = 100, grad_norm_type: str | None = None, grad_ignore_type: str | None = None, ignore_value: float | None = None, ignore_coef: float = 5, ignore_norm_type: float = 2.0, ignore_momentum_timestep: int = 100)[源代码]¶
- Overview:
重写了RMSprop优化器以支持更多功能。
- Interfaces:
__init__,step,_state_init,get_grad
- __init__(params: Iterable, lr: float = 0.01, alpha: float = 0.99, eps: float = 1e-08, weight_decay: float = 0, momentum: float = 0, centered: bool = False, grad_clip_type: str | None = None, clip_value: float | None = None, clip_coef: float = 5, clip_norm_type: float = 2.0, clip_momentum_timestep: int = 100, grad_norm_type: str | None = None, grad_ignore_type: str | None = None, ignore_value: float | None = None, ignore_coef: float = 5, ignore_norm_type: float = 2.0, ignore_momentum_timestep: int = 100)[源代码]¶
- Overview:
重构后的Adam类的init方法
- Arguments:
参数 (
iterable): – 一个包含 torch.Tensor 或 dict 的可迭代对象。指定应该优化哪些张量。lr (
float): 学习率,默认设置为1e-3alpha (
float): 平滑常数,默认设置为0.99eps (
float): 添加到分母以提高数值稳定性的项,默认设置为1e-8weight_decay (
float): 权重衰减系数,默认设置为0居中 (
bool): 如果为True,计算居中的RMSprop,梯度通过其方差的估计进行归一化grad_clip_type (
str): 支持 [None, ‘clip_momentum’, ‘clip_value’, ‘clip_norm’, ‘clip_momentum_norm’]clip_value (
float): 开始剪裁的值clip_coef (
float): 裁剪系数clip_norm_type (
float): 2.0 表示使用 norm2 进行裁剪clip_momentum_timestep (
int): 在多少步之后我们应该开始动量裁剪grad_ignore_type (
str): 支持 [None, ‘ignore_momentum’, ‘ignore_value’, ‘ignore_norm’, ‘ignore_momentum_norm’]ignore_value (
float): 开始忽略的值ignore_coef (
float): 忽略系数ignore_norm_type (
float): 2.0 表示使用 norm2 来忽略ignore_momentum_timestep (
int): 在多少步之后我们应该开始忽略动量
- _optimizer_load_state_dict_post_hooks: OrderedDict[int, Callable[['Optimizer'], None]]¶
- _optimizer_load_state_dict_pre_hooks: OrderedDict[int, Callable[['Optimizer', StateDict], StateDict | None]]¶
- _optimizer_state_dict_post_hooks: OrderedDict[int, Callable[['Optimizer', StateDict], StateDict | None]]¶
- _optimizer_state_dict_pre_hooks: OrderedDict[int, Callable[['Optimizer'], None]]¶
- _optimizer_step_post_hooks: Dict[int, Callable[[Self, Tuple[Any, ...], Dict[str, Any]], None]]¶
- _optimizer_step_pre_hooks: Dict[int, Callable[[Self, Tuple[Any, ...], Dict[str, Any]], Tuple[Tuple[Any, ...], Dict[str, Any]] | None]]¶
PCGrad¶
- class ding.torch_utils.optimizer_helper.PCGrad(optimizer, reduction='mean')[源代码]¶
- Overview:
PCGrad优化器支持多任务。 您可以通过以下链接查看论文 https://arxiv.org/pdf/2001.06782.pdf
- Interfaces:
__init__,zero_grad,step,pc_backward- Properties:
优化器 (
torch.optim): 要使用的优化器
- __init__(optimizer, reduction='mean')[来源]¶
- Overview:
PCGrad优化器的初始化
- Arguments:
优化器 (
torch.optim): 要使用的优化器reduction (
str): 缩减方法,支持 [‘mean’, ‘sum’]
- _flatten_grad(grads, shapes)[source]¶
- Overview:
展平网络参数的梯度
- Arguments:
grads (
list): 参数梯度的列表形状 (
list): 参数形状的列表
- _pack_grad(objectives)[source]¶
- Overview:
为每个目标打包网络参数的梯度
- Arguments:
目标:一个目标列表
- Returns:
grad: 参数梯度的列表
shape: 参数形状的列表
has_grad: 一个掩码列表,表示参数是否有梯度
- _project_conflicting(grads, has_grads, shapes=None)[source]¶
- Overview:
将冲突梯度投影到正交空间
- Arguments:
grads (
list): 参数梯度的列表has_grads (
list): 一个表示参数是否有梯度的掩码列表形状 (
list): 参数形状的列表
- _retrieve_grad()[source]¶
- Overview:
获取具有特定目标的网络参数的梯度
- Returns:
grad: 参数梯度的列表
shape: 参数形状的列表
has_grad: 一个掩码列表,表示参数是否有梯度
- _unflatten_grad(grads, shapes)[source]¶
- Overview:
展开网络参数的梯度
- Arguments:
grads (
list): 参数梯度的列表形状 (
list): 参数形状的列表
- property optimizer¶
- Overview:
获取优化器
配置权重衰减¶
参数¶
请参考 ding/torch_utils/parameter 获取更多详细信息。
非负参数¶
- class ding.torch_utils.parameter.NonegativeParameter(data: Tensor | None = None, requires_grad: bool = True, delta: float = 1e-08)[来源]¶
- Overview:
该模块在前向过程中将输出一个非负参数。
- Interfaces:
__init__,forward,set_data.
- __init__(data: Tensor | None = None, requires_grad: bool = True, delta: float = 1e-08)[来源]¶
- Overview:
使用给定的参数初始化NonegativeParameter对象。
- Arguments:
data (
Optional[torch.Tensor]): 生成参数的初始值。如果设置为None,默认值为 0。requires_grad (
bool): 此参数是否需要梯度。delta (
Any): 对数函数的delta。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- training: bool¶
TanhParameter¶
- class ding.torch_utils.parameter.TanhParameter(data: Tensor | None = None, requires_grad: bool = True)[source]¶
- Overview:
该模块在前向过程中将输出一个tanh参数。
- Interfaces:
__init__,forward,set_data.
- __init__(data: Tensor | None = None, requires_grad: bool = True)[源代码]¶
- Overview:
使用给定的参数初始化TanhParameter对象。
- Arguments:
data (
Optional[torch.Tensor]): 生成参数的初始值。如果设置为None,默认值为1。requires_grad (
bool): 此参数是否需要梯度。
- _backward_hooks: Dict[int, Callable]¶
- _backward_pre_hooks: Dict[int, Callable]¶
- _buffers: Dict[str, Tensor | None]¶
- _forward_hooks: Dict[int, Callable]¶
- _forward_hooks_always_called: Dict[int, bool]¶
- _forward_hooks_with_kwargs: Dict[int, bool]¶
- _forward_pre_hooks: Dict[int, Callable]¶
- _forward_pre_hooks_with_kwargs: Dict[int, bool]¶
- _is_full_backward_hook: bool | None¶
- _load_state_dict_post_hooks: Dict[int, Callable]¶
- _load_state_dict_pre_hooks: Dict[int, Callable]¶
- _modules: Dict[str, Module | None]¶
- _non_persistent_buffers_set: Set[str]¶
- _parameters: Dict[str, Parameter | None]¶
- _state_dict_hooks: Dict[int, Callable]¶
- _state_dict_pre_hooks: Dict[int, Callable]¶
- training: bool¶
reshape_helper¶
请参考 ding/torch_utils/reshape_helper 获取更多详细信息。
fold_batch¶
- ding.torch_utils.reshape_helper.fold_batch(x: Tensor, nonbatch_ndims: int = 1) Tuple[Tensor, Size][source]¶
- Overview:
\((T, B, X) \leftarrow (T*B, X)\) 将张量的前(ndim - nonbatch_ndims)维度折叠为批次维度。 此操作类似于torch.flatten,但提供了一个逆函数 unfold_batch来恢复折叠的维度。
- Arguments:
x (
torch.Tensor): 要折叠的张量- nonbatch_ndims (
int): the number of dimensions that is not folded as 批次维度。
- nonbatch_ndims (
- Returns:
x (
torch.Tensor): 折叠的张量- batch_dims: the folded dimensions of the original tensor, which can be used to
反转操作
- Examples:
>>> x = torch.ones(10, 20, 5, 4, 8) >>> x, batch_dim = fold_batch(x, 2) >>> x.shape == (1000, 4, 8) >>> batch_dim == (10, 20, 5)
unfold_batch¶
- ding.torch_utils.reshape_helper.unfold_batch(x: Tensor, batch_dims: Size | Tuple) Tensor[source]¶
- Overview:
展开张量的批次维度。
- Arguments:
x (
torch.Tensor): 要展开的张量batch_dims (
torch.Size): 被折叠的维度
- Returns:
x (
torch.Tensor): 原始的展开张量
- Examples:
>>> x = torch.ones(10, 20, 5, 4, 8) >>> x, batch_dim = fold_batch(x, 2) >>> x.shape == (1000, 4, 8) >>> batch_dim == (10, 20, 5) >>> x = unfold_batch(x, batch_dim) >>> x.shape == (10, 20, 5, 4, 8)
unsqueeze_repeat¶
- ding.torch_utils.reshape_helper.unsqueeze_repeat(x: Tensor, repeat_times: int, unsqueeze_dim: int = 0) Tensor[source]¶
- Overview:
在unsqueeze_dim上压缩张量,然后在该维度上重复repeat_times次。这对于预处理模型集成的输入非常有用。
- Arguments:
x (
torch.Tensor): 要压缩和重复的张量repeat_times (
int): 张量重复的次数unsqueeze_dim (
int): 需要扩展的维度
- Returns:
x (
torch.Tensor): 未压缩并重复的张量
- Examples:
>>> x = torch.ones(64, 6) >>> x = unsqueeze_repeat(x, 4) >>> x.shape == (4, 64, 6)
>>> x = torch.ones(64, 6) >>> x = unsqueeze_repeat(x, 4, -1) >>> x.shape == (64, 6, 4)