评价此页

torch.nn.utils.prune.random_structured#

torch.nn.utils.prune.random_structured(module, name, amount, dim)[source]#

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

通过随机选择的指定 dim 维度,移除 module 中名为 name 的参数对应的张量,移除指定的 amount 量(当前未被修剪的)通道。通过以下方式就地修改模块(并返回修改后的模块):

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

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

参数
  • module (nn.Module) – module containing the tensor to prune

  • name (str) – 在 module 中执行剪枝操作的参数名称。

  • amount (intfloat) – 要剪枝的参数数量。如果是 float,则应介于 0.0 和 1.0 之间,表示要剪枝的参数的比例。如果是 int,则表示要剪枝的参数的绝对数量。

  • dim (int) – 定义要修剪通道的维度的索引。

返回

模块的修改(即剪枝)后的版本

返回类型

module (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