torch.nn.utils.prune.random_structured

torch.nn.utils.prune.random_structured(module, name, amount, dim)[源代码]

通过在指定维度上随机删除通道来修剪张量。

通过在指定维度dim上随机移除指定数量的(当前未被修剪的)通道,来修剪module中名为name的参数张量。此操作会就地修改模块,并返回修改后的模块。

  1. 添加一个名为name+'_mask'的缓冲区,该缓冲区对应于剪枝方法应用于参数name的二进制掩码。

  2. 将参数 name 替换为其修剪后的版本,而原始(未修剪)参数则存储在一个名为 name+'_orig' 的新参数中。

参数
  • module (nn.Module) – 包含待修剪张量的模块

  • name (str) – 进行剪枝操作的模块中的参数名称。

  • amount (intfloat) – 参数的修剪数量。如果为float类型,应在0.0到1.0之间,并表示要修剪的参数比例。如果是int类型,则表示要绝对修剪的具体参数数量。

  • dim (int) – 表示要裁剪通道的维度的索引。

返回值

输入模块的修改版(即简化后的版本)

返回类型

模块(nn.Module

示例

>>> m = prune.random_structured(
...     nn.Linear(5, 3), 'weight', amount=3, dim=1
... )
>>> columns_pruned = int(sum(torch.sum(m.weight, dim=0) == 0))
>>> print(columns_pruned)
3
本页目录