评价此页

torch.nn.utils.prune.random_structured#

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

通过沿着指定的维度随机移除通道来剪枝张量。

通过在指定的 dim 上随机选择指定的 amount 的(当前未剪枝的)通道来剪枝 module 中名为 name 的参数。通过以下方式就地修改模块(并返回修改后的模块):

  1. 将一个名为 name+'_mask' 的缓冲区添加到剪枝方法所应用的参数 name 上。

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

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

  • 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